You are on page 1of 23

Page 1 of 23

Chapter 2. Language Fundamentals


Exam Objectives

package import

main

!
java.lang.Runnable "

java.lang.Runnable

# !
main

$ % ! & ' !
" ! % !

# % !

# String

! "#

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 2 of 23

Supplementary Objectives

# !

2.1 Basic Language Elements


% $
!

Lexical Tokens
!( " )
%
% ( %

Identifiers
*

$ " !
_ $ $
¢ ¥ £ +! # $
! , -.

$ price Price !

Examples of Legal Identifiers:

number, Number, sum_$, bingo, $$_100, mål, grüß

Examples of Illegal Identifiers:

48chevy, all@hands, grand-sum

48chevy ! @
all@hands
! ! -
+! grand-sum
! !

Keywords

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 3 of 23

% &
* % ! !

/ ! -0
' null true
false -- / ! -.*
!
! % !

abstract default implements protected throw


assert do import public throws
boolean double instanceof return transient
break else int short try
byte extends interface static void
case final long strictfp volatile
catch finally native super while
char float new switch

class for package synchronized

continue if private this

null true false

const goto

Literals

*
(
null

!
2000 0 -7

1 ( 3.14 -3.14 .5 0.5

2 'a' 'A' '0' ':' '-' ')'


true false

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 4 of 23

# "abba" "3.14" "for" "a piece of the action"

Integer Literals
! ' int long byte short
# --

! int long
L l 3 long 2000L 0l
! int ! short byte

$ 8
$ 16 4 ! 0
0x 0X 5 !
-6& 0 7 O
a f ! A F &
-90 -
-0132 -0X5A &
* 8 $

" !# $ %&$ % ' $


# $ &$ ' $
8 010 0x8
10L 012L 0XaL
16 020 0x10
27 033 0x1B
90L 0132L 0x5aL
-90 -0132 -0X5A

2147483647 -.0-1 017777777777 0x7fffffff

-2147483648 (-.0 -020000000000 -0x80000000

1125899906842624L -69 040000000000000000L 0x4000000000000L

Floating-point Literals
1 ( ! ' float double

( double
D d * (
float F f

1 ( ! E e
'" 1 double 194.9E-2
194.9*10-2 1.949

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 5 of 23

Examples of double Literals

0.0 0.0d 0D
0.49 .49 .49D
49.0 49. 49D
4.9E+1 4.9E+1D 4.9e1d 4900e-2 .49E2

Examples of float Literals

0.0F 0f
0.49F .49F
49.0F 49.F 49F
4.9E+1F 4900e-2f .49E2F

&

Boolean Literals
boolean (
true false

Character Literals
* " (" ' *
char

2 $ 0:( , ! ;(
#4( (0 <( *#2 -: 0 9 (
A Z ! ( a z , * ,
( 0: ! \u

( ! $ )
* $ * $ + $ * $
' ' '\u0020' Space
'0' '\u0030' 0
'1' '\u0031' 1
'9' '\u0039' 9
'A' '\u0041' A
'B' '\u0042' B
'Z' '\u005a' Z
'a' '\u0061' a
'b' '\u0062' b
'z' '\u007a' z
'Ñ' '\u0084' Ñ

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 6 of 23

'å' '\u008c' å
'ß' '\u00a7' ß

Escape Sequences

2 ( ! -<
" (" 1
'\t' '\u0009' " +! =
\u000a' =
\u000d'
!
( ! 4
" '\n' '\r'

, $ -. $
$ -. $ $ ) * $
\b \u0008 % #
\t \u0009 + 7 + *
\n \u000a 1 % &! &
\f \u000c 1 11
\r \u000d 2 2@
\' \u0027 * ("
\" \u0022 A %
\\ \u005c %

3 " \ddd !
d 0>7 ! -;
! \377 -6:
!

/ ! $ -. $ \ddd
$ -. $ \ddd * $
'\141' 'a'
'\46' '&'
'\60' '0'

String Literals
* " ! " " % !
* ) String # 096
?9<

5 " ! , '

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 7 of 23

"Here comes a tab.\t And here comes another one\u0009! (1)


"What's on the menu?" (2)
"\"String literals are double-quoted.\"" (3)
"Left!\nRight!" (4)

0 " ,
- !
'\'' . ? !
" \n ! B ! !
'

Here comes a tab. And here comes another one !


What's on the menu?
"String literals are double-quoted."
Left!
Right!

4 "\n" "\r"
C
\u000a" C
\u000d"

White Spaces
*& " $
! ( ! "

*$ ( " )
% % #
% ! 1
classRoom ! % !
% ! class Room.

3 %
! %

Comments
*

$ '

* ( ' // ... to the end of the line

* ( ' /* ... */

* $ ' /** ... */

Single-line Comment

* ( " //
*

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 8 of 23

// This comment ends at the end of this line.


int age; // From comment-start sequence to the end of the line is a comment.

Multiple-line Comment

* * #
! /* ! */

/* A comment
on several
lines.
*/

( " // /* /** !
! (
! '

/* Formula for alchemy.


gold = wizard.makeGold(stone);
/* But it only works on Sundays. */
*/

( " /*
" */ !

Documentation Comment

* ( !
javadoc +D
E
8
# ! /**
! */'

/**
* This class implements a gizmo.
* @author K.A.M.
* @version 2.0
*/

1 javadoc $ - #E/

Review Questions

3 ! F

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 9 of 23

# !

a2z

ödipus

$ 52pickup

_class

ca$h

! total#

3 F

# !

new delete % ! $

try catch thrown % ! $

$ static unsigned long % ! $

exit class while % ! $

return goto default % ! $

! for while next % ! $

/* // */

# !

& % /* ... */) (


// ...

//

$ ! )

2.2 Primitive Data Types


1 -0 ! $

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 10 of 23

0+ 1 #

B $ '

+ G byte short int long


char

* float double G

, boolean G

B ) 5

2 .

5 &
) 3 # 09.

Integer Types

2 + !3 + )
# 4 *5 6 7 7389): 7 7:;9):
byte 8 -2< (-128) 2<-1 (+127)
short 16 -206 (-32768) 206-1 (+32767)
int 32 -2.0 (-2147483648) 2.0-1 (+2147483647)
long 64 -2:. (-9223372036854775808L) 2:.-1 (+9223372036854775807L)

byte short int long -H


-= # 8? 6H;

Character Type

< + ! * $ )
# 4 *5 6 7 $ 7 $

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 11 of 23

char 16 0x0 (\u0000) 0xffff (\uffff)

2 char - 09
0:
:66.: - 0:( ,

0-; , 0-; <( *#2


-6: , -6:
;( #4 (0

Floating-point Types

+ !0 += )
# 4 * 7 1 ) 7 1 )
5 6 7389): 7:;9):
float 32 1.401298464324817E-45f 3.402823476638528860e+38f
double 64 4.94065645841246544e-324 1.79769313486231570e+308

1 ( float double

1 ( 555<6?(0H;6 ( - 00
! ( "
( ! '-' I 0.0 -0.0

# 7 (
1 (1.0/3.0)

Boolean Type

> )
# 4 * ) 0 )
boolean true false

boolean ! true false


- 0-

# .H # .0-
$ # .00 !

- 0. 7 ' ! 7
! " J
! J
!

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 12 of 23

- !1 #
# 4 * 7 ) %7 ) 4
5 6
boolean true false Boolean

byte 8 (-< -<(0 Byte

short 16 (-06 -06(0 Short

char 16 0x0 0xffff Character

int 32 (-.0 -.0(0 Integer

long 64 (-:. -:.(0 Long

float 32 K1.40129846432481707e-45f Float


K3.402823476638528860e+38f
double 64 L
=04.94065645841246544e-324 Double
L
=01.79769313486231570e+308

Review Questions

3 ! $ F

# ! !

"t"

'k'

$ 50.5F

"hello"

false

" 3 ! F

# !

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 13 of 23

boolean

byte

$ float

short

double

( 3 $ (2147483648 (-2.0)
.0
2147483647 (2 -1) F

# !

byte

short

$ int

long

char

2.3 Variable Declarations


*- $ *
! $ )
M ) - $

Declaring and Initializing Variables

M
3 !
'

char a, b, c; // a, b and c are character variables.


double area; // area is a floating-point variable.
boolean flag; // flag is a boolean variable.

" ! '

char a;
char b;
char c;

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 14 of 23

* 7
'

int i = 10, // i is an int variable with initial value 10.


j = 101; // j is an int variable with initial value 101.
long big = 2147483648L; // big is a long variable with specified initial value.

Object Reference Variables


* $. ) $ #
)

*
) *

! )
! ) 7 !
)

Pizza yummyPizza; // Variable yummyPizza can reference objects of class Pizza.


Hamburger bigOne, // Variable bigOne can reference objects of class Hamburger,
smallOne; // and so can variable smallOne.

) Pizza
Hamburger )

* 7 ) !
'

Pizza yummyPizza = new Pizza("Hot&Spicy"); // Declaration with initializer.

yummyPizza ) Pizza % ! new


! Pizza("Hot&Spicy") ) Pizza
) yummyPizza ! )
Pizza !

+ ) 7 ) # ;-

@ # ?0

Lifetime of Variables

! 3 ! '

+ - $ G )
! ) ! ! !
) )
)

- $ G )

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 15 of 23

# ?09 0?? !

/ - $ - $ G %
%*
% (final

2.4 Initial Values for Variables


Default Values for Fields
E - 0?

# ! )
# # ! )
boolean false
char '\u0000'

byte short int long 0L long 0

1 ( float double 0.0F 0.0D

@ null

7 7
% # ;- ..: 7 ! !

# 7
7 % # ;- ..; 7 !
!

! 7 ! null 7

5 -0 7 & 7 !
7 - )
Light

# ! ) ! 0

public class Light {


// Static variable
static int counter; // Default value 0 when class is loaded.

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 16 of 23

// Instance variables
int noOfWatts = 100; // Explicitly set to 100.
boolean indicator; // Implicitly set to default value false.
String location; // Implicitly set to default value null.

public static void main(String[] args) {


Light bulb = new Light();
System.out.println("Static variable counter: " + Light.counter);
System.out.println("Instance variable noOfWatts: " + bulb.noOfWatts);
System.out.println("Instance variable indicator: " + bulb.indicator);
System.out.println("Instance variable location: " + bulb.location);
return;
}
}

4 '

Static variable counter: 0


Instance variable noOfWatts: 100
Instance variable indicator: false
Instance variable location: null

Initializing Local Variables of Primitive Data Types


7 ! !
7
! 7

0 ++ + ? $ ) !1 #

public class TooSmartClass {


public static void main(String[] args) {
int weight = 10, thePrice; // Local variables

if (weight < 10) thePrice = 1000;


if (weight > 50) thePrice = 5000;
if (weight >= 10) thePrice = weight*10; // Always executed.
System.out.println("The price is: " + thePrice); // (1)
}
}

5 -- thePrice println
0 7 +!
thePrice ! 100 if( println

true
! 7
@ 5 --
! ! '

int weight = 10, thePrice = 0; // Both local variables initialized.

Initializing Local Reference Variables


7

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 17 of 23

0 ++ + ? $ ! $ )

public class VerySmartClass {


public static void main(String[] args) {
String importantMessage; // Local reference variable

System.out.println("The message length is: " + importantMessage.length());


}
}

5 -. importantMessage
println 7 importantMessage
null ! +! NullPointerException ! !
importantMessage ! )
! )
null !
! ! !
importantMessage'

String importantMessage = "Initialize before use!";

* # ?0 090

Review Questions

, 3 ! F

# !

char a = '\u0061';

char 'a' = 'a';

$ char \u0061 = 'a';

ch\u0061r a = 'a';

ch'a'r a = 'a';

/ 8 ! ! ! F

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 18 of 23

int a, b;
b = 5;

# !

$ a 7

b 7

b 7

2 ! ! 7
7 F

# !

E int

E float

$ E float

E Object

E int[]

2.5 Java Source File Structure


% $ 1 - - *$
! ! '

* package % B %
# ?:

I import # import
import
# ?:

* * - #
% - ! %

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 19 of 23

2
% !

$ - #E/ public
public public
public NewApp NewApp.java

2 # ?- # :?

0+ - $ 0 - $

& package import


& !

Review Questions

< 3 ! F

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 20 of 23

import java.util.*;

package com.acme.toolkit;

public class AClass {


public Other anInstance;
}
class Other {
int value;
}

# !

! Other
! AClass

! import

$ ! package
import

! Other
Other.java

! Other public

! ! !

* !

2.6 The main() Method


$ $ - #E/
# 0 09 $ main
* main() main()
$ $

main() public #
?H 0.; ) "
# ?09 0??
void # 6? 0<: ! String )

# .-. H6 * ! main() '

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 21 of 23

public static void main(String[] args) {


// ...
}

" # ?09
0?? throws # 6H -90 main()
% 00: $ main()
!

Review Questions

3 main()
$ F

# ! !

static void main(String[] args) { /* ... */ }

public static int main(String[] args) { /* ... */ }

$ public static void main(String args) { /* ... */ }

final public static void main(String[] arguments) { /* ... */ }

public int main(Strings[] args, int argc) { /* ... */ }

! static public void main(String args[]) { /* ... */ }

3 ! % ! F

# !

public

static

$ void

main

String

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 22 of 23

! args

Chapter Summary

! ! '

' % ! !

' (

main() !

Programming Exercises

! D !
! # # ?: 0-H
%

import java.util.*;

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006


Page 23 of 23

package com.acme;

public class Exercise1 {


int counter;

void main(String[] args) {


Exercise1 instance = new Exercise1();
instance.go();
}

public void go() {


int sum;
int i = 0;
while (i<100) {
if (i == 0) sum = 100;
sum = sum + i;
i++;
}
System.out.println(sum);
}
}

! D !
!

// Filename: Temperature.java
PUBLIC CLASS temperature {
PUBLIC void main(string args) {
double fahrenheit = 62.5;
*/ Convert /*
double celsius = f2c(fahrenheit);
System.out.println(fahrenheit + 'F = ' + celsius + 'C');
}

double f2c(float fahr) {


RETURN (fahr - 32) * 5 / 9;
}
}

file://C:\Documents and Settings\cdot\Local Settings\Temp\~hhBEF2.htm 6/16/2006

You might also like