©
2002,
2003
by
Daniel
Berleant
Let's
contrast
C/C++
and
Java...
Note
similarities
and
differences
//
If
a
"/"
followed
immediately
by
//
another
"/"
occurs,
then
the
rest
//
of
that
line
is
a
comment.
//=========================================
//
We
start
by
bringing
in
some
standard
//
libraries.
Libraries
contain
useful
//
modules
predefined
by
someone
else.
//
"Standard"
libraries
are
libraries
//
which
come
as
part
of
the
language.
import
java.applet.*;
//
The
applet
library
is
needed
whenever
//
your
Java
program
is
an
applet.
//
"import"
is
for
bringing
in
libraries.
//
Standard
library
modules
have
names
//
"java.something"
where
"something"
//
specifies
which
library.
//
E.g.:
java.applet
//
The
asterisk
means
load
*everything*
//
in
the
java.applet
library.
//
(We
could
be
more
specific...
//
import
java.applet.Applet;
//
...thus
not
loading
//
e.g.
java.applet.BorderLayout
//
The
other
standard
java
libraries
are:
//
java.lang,
java.util,
java.io,
//
java.net,
java.awt,
and
javax.swing
//
(note
the
x)
//
IMPORTANT:
note
the
3rd
(4th)
thing
when
importing:
import
java.io.*;
//=============================================
//
Let's
define
the
"helloWorld"
module...
public
class
HelloWorld
//
"public"
here
means
that
this
//
class
(module)
can
be
seen
(&
used)
by
//
other
classes,
including
those
//
in
the
hidden
machinery
in
the
JRE
//
(JRE=Java
Runtime
Environment)
//
"hello
world"
program.
//
A
class
is
a
module
containing
//
related
data
and
methods
which
are
//
bundled
together.
//
A
class
is
like
a
"struct"
type
in
C
//
except
it
has
functions
as
well
as
data.
//
The
name
we
give
our
class
is
//
"helloWorld".
//
Note
the
naming
custom
in
Java:
//
"helloWorld"
not
"hello_world"
//
(use
a
capital
letter
instead
of
an
"_")
//
It's
easier!
//==============================================
//Now,
we
start
the
body
of
the
class,
//which
will
contain
all
//the
data
and
executable
methods
//
(called
functions
in
C)
in
it.
//Just
like
in
C/C++,
chunks
of
code
//are
delimited
with
curly
//braces
("{"
and
"}")
{
public
static
void
main(String[
]
args)
{
//"public"
makes
main(
)
accessible
//anywhere
in
the
program
(in
this
case,
//
the
JRE
needs
access
to
it).
Methods
//and
data
in
Java
programs
may
also
//be
declared
as
"private",
which
//hides
them
from
visibility
to
code
in
//other
classes
while
making
them
//locally
visible
in
the
class
in
which
//they
are
declared.
//"void"
means
that
main()
does
not
//return
any
value.
A
method
can
//also
be
declared
"int,"
"float,"
etc.
//which
means
that
the
method
//returns
a
value
of
the
declared
type
//at
the
time
it
finishes
running.
//"("
begins
the
list
of
parameters
//
to
the
method.
//"String"
indicates
the
class
to
//which
the
argument,
args,
is
in.
//Note
that
"String"
begins
with
a
capital
//letter.
In
Java,
a
capital
letter
is
a
completely
different
//character
from
the
lower
case
version
of
the
same
letter.
//")"
closes
the
list
of
arguments
to
method main(
)
//which
in
this
case
//happens
to
contain
one
argument.
//Next,
"{"
begins
the
block
of
code
defining
//the
main()
method
{
int
count=5;
//"int"
declares
that
the
variable
"count"
//contains
an
integer.
//"="
in
Java
is
for
assigning
values,
in
this
//case
the
integer
5.
//An
assignment
at
the
time
a
variable
is
//declared
gives
it
an
//initial
(starting)
value,
so
"count"
has
//now
been
declared
to
//hold
an
integer
and
to
start
out
by
//holding
the
integer
5.
//Next,
declare
and
initialize
a
64
bit
//(that
is,
a
"double
//precision")
floating
point
variable
double
decimalNum=5.5;
//Next,
declare
and
initialize
a
16
bit
//character
variable
char
ch='t';
//Next,
print
System.out.println(
"Hello
world!"+(count+decimalNum));
//Prints:
Hello
world!10.5
//+
can
mean
different
things
in
C
//
(it
is
overloaded)
//
.
.
.
and
even
more
different
things
in
Java
//
(it
is
more
overloaded)
//
It
makes
more
sense
to
//convert
an
integer
to
a
string
than
a
//string
to
an
integer,
//and
both
arguments
of
a
"+"
do
need
to
//be
of
the
same
type
//for
the
concept
of
summing
//to
make
sense.
//Similarly,
the
sum
of
count
and
decimalNum
//is
automatically converted
to
a
string
//
which
is
then
printed
//Next,
a
for
loop
is
given,
which
happens
//to
use
exactly
the
//same
syntax
as
in
for
loops
in
C.
for
(count=1
;
count<10;
count++)
//Print
each
value
System.out.println(count);
//Note
there
are
several
System.out.println(
)s!!
//Compare:
Cs
printf
ONLY
takes
strings
//C++s
cout
can
take
different
arg
types
//Next,
we
close
the
block
of
code
for
main(
)
which
had
//earlier
been
opened
with
"{,"
with
a
"}."
}
//...and
next
we
also
must
close
the
"{"
//that
began
the
class
//definition.
While
classes
in
general
//can
contain
various
//variables
and
methods,
this
particular
//elementary
class
//contains
only
one
method.
}
/**
This
is
a
special
kind
of
comment
*/
/**
It
is
extracted
by
a
documentation
program
*/
#include
(C)
is
like
________
(Java)?
Java
has
char,
int,
double,
float
Like
C
Unlike
C:
sizeof(char),
etc.,
are
defined
Unlike
C:
2
byte
char's
support
Unicode
char
alpha='\u0391';
Java
also
has
byte,
String,
and
boolean
...thereby
improving
on
C
boolean
vars
have
value
true
or
false
A
zoo
of
operators
+
–
*
/
!
>
<
[
]
;
__?__:__
.
=
++
–
–
==
<=
>=
!=
+=
–
=
*=
/=
%=
||
&&
etc....
(similar
to
C)...
do
you
know
what
they
do?
Other
things
about
C
and
Java...
Variable
name
rules
are
similar
Variable
name
conventions
are
different
this_is_a_C_variable
thisIsAJavaVariable
Loops:
Java
is
like
C
-
no
more
to
say!
if,
if...else,
and
switch
-
like
C
too
Strings
-
"don't
exist"
in
C,
do
in
Java
String
S
=
new
String("IamAstring");
...
this
is
better!
no
'\0'
characters
to
confuse
length,
get
overwritten
-
In
Java,
you
can
+
strings
-
System.out.println("hello"+"goodbye"+10);
Arrays
-
int
myIntegers[]=new
int[10];
Note
2-stage
declaration
+
allocation
int
arrays
are
objects,
not
just
sequences
if
ints
...they
have
methods,
vars
bundled
in
...
if
(myIntegers.length==10)
...Java
arrays
do
range
checking
(C
doesn't)
Command
line
args:
C:
void
main(int
argc,
char
*
argv[])
Java:
public
void
main(String
args[])
Java
has
NO
-
preprocessor
(
)
-
goto
-
struct,
typedef
(objects
do
same
and
more)
-
pointer
operators
(&,
*)
(Java
has
pointers
but
you
don't
have
to
worry
about
them
much)
-
variable
numbers
of
arguments