Comp401sp14lec02Basic Java Syntax
Comp401sp14lec02Basic Java Syntax
Java
Syntax
COMP
401,
Spring
2014
Lecture
2
1/14/2014
Comments
Single
line
comments:
// This is a comment.!
Reference
Types
String
Array
Objects
typed
by
their
class
Classes
themselves
Value
Types
Integers
Real
Numbers
float, double!
Dierence
is
in
precision.
Characters
char!
Characters
in
Java
are
16-bit
Unicode
values
Literals
use
single-quote:
c
Non-printable
escape
sequence:
\u####
where
#
is
hex
digit.
Example:
\u00F1
for
Logical
boolean!
Literals
are
true
and
false
package lec02.ex01;!
All
classes
dened
in
the
le
belong
to
that
package.
Method
Signature
Almost
everything
you
need
to
know
about
a
method
is
in
its
signature.
5
parts
to
a
method
signature
Access
modier
Method type
Return type
Method name
Parameter list
Inside
a
method
The
body
of
a
method
is
a
sequence
of
statements.
A
statement
ends
in
a
semi-colon
Types
of
statements:
Variable
declaraBon
Assignment
CondiBonal
Loop
Method
call
Return
statement
Blocks
Inside
a
method
The
body
of
a
method
is
a
sequence
of
statements.
A
statement
ends
in
a
semi-colon
Types
of
statements:
Variable
declaraBon
Assignment
CondiBonal
Loop
Method
call
Return
statement
Blocks
Variable
declaraBon
Syntax:
type name;!
type name1, name2, name3;!
type name = value;!
Variable
Names
Variable
names
should
start
with
a
leher
and
can
contain
lehers,
digits,
$,
or
_
Can
not
start
with
digit
Can
not
contain
whitespace
or
punctuaBon
other
than
$
or
_
In
general,
use
of
punctuaBon
in
variable
names
is
discouraged.
Case
sensiBve
Can
not
be
a
keyword
Legal:
Illegal:
Inside
a
method
The
body
of
a
method
is
a
sequence
of
statements.
A
statement
ends
in
a
semi-colon
Types
of
statements:
Variable
declaraBon
Assignment
CondiBonal
Loop
Method
call
Blocks
Inside
a
method
The
body
of
a
method
is
a
sequence
of
statements.
A
statement
ends
in
a
semi-colon
Types
of
statements:
Variable
declaraBon
Assignment
CondiBonal
Loop
Method
call
Return
statement
Blocks
Assignment
Syntax:
variable = expression;!
Expressions
A
sequence
of
symbols
that
can
be
evaluated
to
produce
a
value.
Can
be
used
wherever
a
value
is
expected.
Types
of
expressions:
Literal
values:
123
Variable
names:
a_variable
Public
class/object
elds:
Math.PI
Value
as
a
result
of
a
method
call:
foo()
Expressions
combined
by
operators:
4+3*2
Operators
Unary:
+,
-,
!,
++,
--
ArithmeBc:
+,
-,
/,
*,
%
RelaBonal:
==,
!=,
>,
>=,
<,
<=
Boolean:
&&,
||
Ternary:
?:
expression
?
if_true
:
if_false
Be aware of context
Inside
a
method
The
body
of
a
method
is
a
sequence
of
statements.
A
statement
ends
in
a
semi-colon
Types
of
statements:
Variable
declaraBon
Assignment
CondiBonal
Loop
Method
call
Return
statement
Blocks
20
if
example
if (score > 90) {!
!System.out.println(You
} else if (score > 80) {!
!System.out.println(You
} else if (score > 70) {!
!System.out.println(You
} else if (score > 60) {!
!System.out.println(You
} else {!
!System.out.println(You
}!
!
got an A!);!
got a B.);!
got a C?);!
got a D :-();!
failed);!
Switch
Example
switch (c) {!
!case 'a': !
!case 'e': !
!case 'i':!
!case 'o': !
!case 'u': !
!!System.out.println("Vowel");!
!!break;!
!default: !
!!System.out.println("Consonant");!
}!
!
Inside
a
method
The
body
of
a
method
is
a
sequence
of
statements.
A
statement
ends
in
a
semi-colon
Types
of
statements:
Variable
declaraBon
Assignment
CondiBonal
Loop
Method
call
Return
statement
Blocks
Loops:
while
while (expression) {!
!block!
}!
!
do {!
!block!
} while (expression);!
while
example
int sum = 0;!
int n = 1;!
while (n < 11) {!
sum += n;!
n++;!
}!
System.out.println(The sum of 1 to 10 is: + sum);!
Loops:
for
for (init; test; update) {!
!block!
}!
for
example
int sum = 0;!
for(int n=1; n<11; n++) {!
sum += n;!
}!
System.out.println(The sum of 1 to 10 is: + sum);!
!
Note
that
variable
n
is
declared
as
part
of
init
expression
in
for
loop.
This
is
a
common
programming
idiom
if
loop
variable
is
only
needed
for
the
loop.
Scope
of
variable
is
limited
to
the
loop
block.
Inside
a
method
The
body
of
a
method
is
a
sequence
of
statements.
A
statement
ends
in
a
semi-colon
Types
of
statements:
Variable
declaraBon
Assignment
CondiBonal
Loop
Method
call
Return
statement
Blocks
Calling
Methods
Calling
a
class
method:
ClassName.methodName(parameter values);!
Inside
a
method
The
body
of
a
method
is
a
sequence
of
statements.
A
statement
ends
in
a
semi-colon
Types
of
statements:
Variable
declaraBon
Assignment
CondiBonal
Loop
Method
call
Return
statement
Blocks
Return
Syntax:
return expression;!
Starts
o
in
the
main
class
method
dened
in
whatever
class
you
told
the
program
to
start
with.
ExecuBon
context
includes:
Variable
names
that
are
in
scope.
Parameter
names.
Method
parameter
names
are
mapped
to
values
provided
when
method
was
called.
As
method
executes,
declared
variables
become
part
of
this
new
context.
Strings
String
class
Sequence
of
chars
equals(Object
obj)
Be
careful
about
using
==
a = A String ;!
a.length();!
!9!
a.charAt(3);!
!t
a.concat(am I);!
!A
String
am
I
a + am I;!
Same
as
above
a.equals(B string);!
!false
a.substring(2,5);!
Stri
a.trim();!
A
String
!
Arrays
int[] iarray;!
!Declared, but not created.!
iarray = new int[10];!
!Now its created, but
uninitialized.!
len = iarray.length;!
!Length available as public
field. Note, not a method.!
iarray[0] = 3;!
!0th item set!
iarray[-1];!
!Illegal index!
iarray[10];!
!Index out of bounds!
int[] iarray = {1,2,(2+1)};!
!Declared and items
initialized all in one.!