shhgs@wgqqh.com
a0 1
a1 a2 42 a1
2002 7
chinapub ! " # $ % ! & ’ ( ) * +,-,&/ 0 (1 2
3 4 5 (
6 7,8 9 3,); < =,> 8 (? @ A B 3 C D E
Bruce EckelFG (H I J K ( L ( M N O P A ! Q
$ $ R S T T U 3
1000V W ( X Y Z R S ( @ 4 [ \ ] ^ _‘ a b,c ! Q,d (e f 9 g
7 h i j kl,D m (n o 4 cp q r s t ( u * v w $ x y 1 (Uz { !,(| }
~ / 3 3 c e * e X (
$ ( * u c,> m i * 3 ; i = ( 1 3 e = X? ¢ £? ¥ Q $ X 3?,
§ currency1 3 ' (,' (,( D E V %? fi fl
( –? X AF? ·? fl,? … 3C ¥‰ Q $?,
` m
Bruce Eckel? (′?,?,1000V W? ˉ? ˙? = G ¨ X D E? m 3 m ˇ( 8? Y — (?
The
genesis of the computer revolution was in a machine,The genesis of our
programming languages thus tends to look like that machine.ˇ 3 w Z | ′,; $ X O
Bruce% ( ˉ 3 K ( — 1 F – J ( 1 (?; a B,V ` J J? ' ( X X? ‰ ( f o ( M % (r? F V,X ; F K c o ; K? ^ ( X ^ X b,(,
( Q? g
10? - 7? u ` K mC % k l,N O? 3 C a F K E 3? K 3?
www.wgqqh.com/shhgs/tij.html? ˉ#? (a y 1 ( a? ˉ? X Y
v w #,”? (? 1 ( 7 ; ^ ! 3 J — (
shhgs
2003 9 8
shhgs@wgqqh.com
a0 2
a1 a2 42 a1
,(?X cˇ(? ·? ˙
( %,c ! " # $f %
& ¢ e x ’ ( V
( ’ ) *,¥ +,f % - 9 (,g 3 / X 0 1 w? f % 2 3 X 0 1 %,f % ( 4 5?,& ¢ 3 6 7 (! " + ^ u,”
R S P 3?*,? ’ 8 9,;,Y < = D > (? @? @,A B C D
b? ( E F
- t,G H I ( J 3 ; F¥K L M ( M N (
[ O P,T ‰ ¥)Y ` (¥K A m 3,Qk § D > E F (R S T U V? W X,f % & ¢ ! " Y ' £,
Z ¢ e ! " (
E? [? m 3
( \ ] ^,A F¥K u N X _ ( Z T ( / ‘ a% N
4 (b c E ^ d e 3 B G HI
(6 7 Q,f [ ¥K? ; g f %? ˉ m G H I Y Z
,F¥K g g L M / Q X h g ( M N Y G H I? f % m,i,
j kQ (! "? m e l? m 3 \ X ;
( ) ( \ > n o ( )G H I (? VN (? Vp q % ; 0 1 w N r s 9
(£ N t s u v w ¢ Y Z
x,T yG H I( \ ( \ ) f % ( M ( N z¢?,
k? 3 { G H I(u v ( y
//,c04:SimpleConstructor.java
// Demonstration of a simple constructor,
import com.bruceeckel.simpletest.*;
class Rock {
Rock() { // This is the constructor
System.out.println("Creating Rock");
}
}
public class SimpleConstructor {
static Test monitor = new Test();
public static void main(String[] args) {
shhgs@wgqqh.com
a0 3
a1 a2 42 a1
for(int i = 0; i < 10; i++)
new Rock();
monitor.expect(new String[] {
"Creating Rock",
"Creating Rock",
"Creating Rock",
"Creating Rock",
"Creating Rock",
"Creating Rock",
"Creating Rock",
"Creating Rock",
"Creating Rock",
"Creating Rock"
});
}
} ///:~
F L M ¥K ( M y
new Rock();
X % E F Y % N G H I ^ m,i ( F; g ¥K u A? B | } r f %, ~? % ( (~ \ %? (% X G H I
G H I( \ p q ( \ c > § G 3? G H I[ E m I ; # Aw?,
f %,? ’? ^ u v r u [ ; ˙ A(G H Ij Iy
//,c04:SimpleConstructor2.java
// Constructors can have arguments,
import com.bruceeckel.simpletest.*;
class Rock2 {
Rock2(int i) {
System.out.println("Creating Rock number " + i);
}
}
public class SimpleConstructor2 {
static Test monitor = new Test();
public static void main(String[] args) {
for(int i = 0; i < 10; i++)
new Rock2(i);
monitor.expect(new String[] {
"Creating Rock number 0",
"Creating Rock number 1",
"Creating Rock number 2",
"Creating Rock number 3",
shhgs@wgqqh.com
a0 4
a1 a2 42 a1
"Creating Rock number 4",
"Creating Rock number 5",
"Creating Rock number 6",
"Creating Rock number 7",
"Creating Rock number 8",
"Creating Rock number 9"
});
}
} ///:~
G H I( I 3 ; ˙? ¥¥K (f % d e I( 8 $
m 3? I1 I(G H I? I ^ ( z Y Z? E? Q L M 3
¥K y
Tree t = new Tree(12); // 12-foot tree
ˉ
m (G H I Y Z? VX,˙? ` § A L M
¥K,G H I£ N,3 ′ ! " d,( 8? FJ (
* X,N } F J J ¥K (L M > (
(¥K L M f % 3 J? X ; % X % Y
G H I 3 6 7 ( A m T 3 C
( m,J (
( Z? X T? N O (? [ E ˙ A T Z G H I Z? X T Y?
·?,T L M (¥K ( G H I p X T
ˉG H I; m T Y? E Y Z? V % !,Av w? Z T
\ ¢? (3 x % 6 L M ¥K ( M?,?E F? 4
9 \ 1 ( \ ^ \,; R S r ¢ £ ’,} F? ¢ § £( %? ¥
\ Q ^ ¥K X ¥?,( \ ; ˙
9 Q u v FP,? § g? ( M,,g,3 currency1 ',3
v E V X (? m [ $? B?,,g? m m? X? 1 (B ( M 5?,?” $?fi fl ˇ?fi – ˇ fi · ˇ? ˉ?,% ˙
shhgs@wgqqh.com
a0 5
a1 a2 42 a1
X 1 ( FY?$? fi fl ( ·fi fl ˇ? fi – ( ·fi – ˇ fi · ( · fi · ˇ Y?,? ′ V I(,? m? o D E,c? … i \?
[ ; 0 1 G F' Z c X % ‰ D m (e? c E Jk 1 ! `
′ V I(?
6? %? [ H I? 9 3 ′ 3 ( ˉ D E? X ; ˙ 3
˙ ] ¨? I ˙ 3? ˙ ] ¨? C I [ H I? % 3? 3 ( \
E h? m 3 ˙ c X X ¥,? ( yG H I ( )G H I( \? ( N O,G H I ; m 3 \ % i X ( Q L M ¥K ( Y w Z?
b? % L M? 3 As ; E? ( ·,f % ; currency1? ˇ — E,f %? m e G H I 3 X % I(
,(G H I a3 a4 a5 a6[ I G H I
3 % 3 1 I(G H I
f % D ( currency1 ( currency1 e? G H I D E Ac 3 \ ( \ ),; ˙ > \ ( X ( I (?
fl? %,? X ¥G H IQ $ p q Y [ 3 ( ; ) (
k? ’ s ^,G H I(? [ ^,(? y
//,c04:Overloading.java
// Demonstration of both constructor
// and ordinary method overloading,
import com.bruceeckel.simpletest.*;
import java.util.*;
class Tree {
int height;
Tree() {
System.out.println("Planting a seedling");
height = 0;
}
Tree(int i) {
System.out.println("Creating new Tree that is "
+ i + " feet tall");
height = i;
}
void info() {
System.out.println("Tree is " + height + " feet
tall");
}
void info(String s) {
System.out.println(s + ",Tree is "
+ height + " feet tall");
}
}
shhgs@wgqqh.com
a0 6
a1 a2 42 a1
public class Overloading {
static Test monitor = new Test();
public static void main(String[] args) {
for(int i = 0; i < 5; i++) {
Tree t = new Tree(i);
t.info();
t.info("overloaded method");
}
// Overloaded constructor,
new Tree();
monitor.expect(new String[] {
"Creating new Tree that is 0 feet tall",
"Tree is 0 feet tall",
"overloaded method,Tree is 0 feet tall",
"Creating new Tree that is 1 feet tall",
"Tree is 1 feet tall",
"overloaded method,Tree is 1 feet tall",
"Creating new Tree that is 2 feet tall",
"Tree is 2 feet tall",
"overloaded method,Tree is 2 feet tall",
"Creating new Tree that is 3 feet tall",
"Tree is 3 feet tall",
"overloaded method,Tree is 3 feet tall",
"Creating new Tree that is 4 feet tall",
"Tree is 4 feet tall",
"overloaded method,Tree is 4 feet tall",
"Planting a seedling"
});
}
} ///:~
L M
¥K ( M s E X I [ E 3 ^ z ( I ^ A 3 ^ A?,,; d e ; ’ m 3 (G H I E h 3 % m z 1
I(G H I | V % i N
( 8? | V ] ¨ ( M? E
Z? X | $ ( M E 3 J % e \ Y O,D _ ( m, E ˙ e 3 \
ˉ ( \ > Y Z
w? `? % r ( m3 u v ( y[ 3? ^? (? p q m? 3 ( I (?
%? | 3 k?,bw 5 ¢ ( ˉX I(
’ ) Z Q e m,> \ (
shhgs@wgqqh.com
a0 7
a1 a2 42 a1
” Iz’ (a [? E e Q y X ^? 5 k?,0 U E (
//,c04:OverloadingOrder.java
// Overloading based on the order of the arguments,
import com.bruceeckel.simpletest.*;
public class OverloadingOrder {
static Test monitor = new Test();
static void print(String s,int i) {
System.out.println("String," + s + ",int," +
i);
}
static void print(int i,String s) {
System.out.println("int," + i + ",String," +
s);
}
public static void main(String[] args) {
print("String first",11);
print(99,"Int first");
monitor.expect(new String[] {
"String,String first,int,11",
"int,99,String,Int first"
});
}
} ///:~
e
m,> ( I z’ X Ac? 3 X (r
,r? ( g? ′ (? ˉ? ( M,g ! " Y m C ˙,',,k? ’ ^,o?
( M,0 Z y
//,c04:PrimitiveOverloading.java
// Promotion of primitives and overloading,
import com.bruceeckel.simpletest.*;
public class PrimitiveOverloading {
static Test monitor = new Test();
void f1(char x) { System.out.println("f1(char)"); }
void f1(byte x) { System.out.println("f1(byte)"); }
void f1(short x)
{ System.out.println("f1(short)"); }
void f1(int x) { System.out.println("f1(int)"); }
void f1(long x) { System.out.println("f1(long)"); }
void f1(float x)
{ System.out.println("f1(float)"); }
void f1(double x)
{ System.out.println("f1(double)"); }
void f2(byte x) { System.out.println("f2(byte)"); }
shhgs@wgqqh.com
a0 8
a1 a2 42 a1
void f2(short x)
{ System.out.println("f2(short)"); }
void f2(int x) { System.out.println("f2(int)"); }
void f2(long x) { System.out.println("f2(long)"); }
void f2(float x)
{ System.out.println("f2(float)"); }
void f2(double x)
{ System.out.println("f2(double)"); }
void f3(short x)
{ System.out.println("f3(short)"); }
void f3(int x) { System.out.println("f3(int)"); }
void f3(long x) { System.out.println("f3(long)"); }
void f3(float x)
{ System.out.println("f3(float)"); }
void f3(double x)
{ System.out.println("f3(double)"); }
void f4(int x) { System.out.println("f4(int)"); }
void f4(long x) { System.out.println("f4(long)"); }
void f4(float x)
{ System.out.println("f4(float)"); }
void f4(double x)
{ System.out.println("f4(double)"); }
void f5(long x) { System.out.println("f5(long)"); }
void f5(float x)
{ System.out.println("f5(float)"); }
void f5(double x)
{ System.out.println("f5(double)"); }
void f6(float x)
{ System.out.println("f6(float)"); }
void f6(double x)
{ System.out.println("f6(double)"); }
void f7(double x)
{ System.out.println("f7(double)"); }
void testConstVal() {
System.out.println("Testing with 5");
f1(5);f2(5);f3(5);f4(5);f5(5);f6(5);f7(5);
}
void testChar() {
char x = 'x';
System.out.println("char argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testByte() {
byte x = 0;
System.out.println("byte argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testShort() {
short x = 0;
System.out.println("short argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testInt() {
int x = 0;
System.out.println("int argument:");
shhgs@wgqqh.com
a0 9
a1 a2 42 a1
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testLong() {
long x = 0;
System.out.println("long argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testFloat() {
float x = 0;
System.out.println("float argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
void testDouble() {
double x = 0;
System.out.println("double argument:");
f1(x);f2(x);f3(x);f4(x);f5(x);f6(x);f7(x);
}
public static void main(String[] args) {
PrimitiveOverloading p =
new PrimitiveOverloading();
p.testConstVal();
p.testChar();
p.testByte();
p.testShort();
p.testInt();
p.testLong();
p.testFloat();
p.testDouble();
monitor.expect(new String[] {
"Testing with 5",
"f1(int)",
"f2(int)",
"f3(int)",
"f4(int)",
"f5(long)",
"f6(float)",
"f7(double)",
"char argument:",
"f1(char)",
"f2(int)",
"f3(int)",
"f4(int)",
"f5(long)",
"f6(float)",
"f7(double)",
"byte argument:",
"f1(byte)",
"f2(byte)",
"f3(short)",
"f4(int)",
"f5(long)",
"f6(float)",
"f7(double)",
"short argument:",
"f1(short)",
"f2(short)",
"f3(short)",
"f4(int)",
"f5(long)",
"f6(float)",
"f7(double)",
shhgs@wgqqh.com
a0 10
a1 a2 42 a1
"int argument:",
"f1(int)",
"f2(int)",
"f3(int)",
"f4(int)",
"f5(long)",
"f6(float)",
"f7(double)",
"long argument:",
"f1(long)",
"f2(long)",
"f3(long)",
"f4(long)",
"f5(long)",
"f6(float)",
"f7(double)",
"float argument:",
"f1(float)",
"f2(float)",
"f3(float)",
"f4(float)",
"f5(float)",
"f6(float)",
"f7(double)",
"double argument:",
"f1(double)",
"f2(double)",
"f3(double)",
"f4(double)",
"f5(double)",
"f6(double)",
"f7(double)"
});
}
} ///:~
,? +
,B 1 D E? ˉ? (?m 1 I( A” F§ G 5 k? ˉ?
y N (Y I
( 8 y (O?* D? ( I ( Y Z I,/ d?
(?,m X? ˉ? ˉ Xg? (
( A” B d? g? ˉ? 8 ′ Y, c 3 kJ? Y ’ ˙ A
Q #? t y
//,c04:Demotion.java
// Demotion of primitives and overloading,
import com.bruceeckel.simpletest.*;
public class Demotion {
static Test monitor = new Test();
void f1(char x) { System.out.println("f1(char)"); }
void f1(byte x) { System.out.println("f1(byte)"); }
void f1(short x)
{ System.out.println("f1(short)"); }
shhgs@wgqqh.com
a0 11
a1 a2 42 a1
void f1(int x) { System.out.println("f1(int)"); }
void f1(long x) { System.out.println("f1(long)"); }
void f1(float x)
{ System.out.println("f1(float)"); }
void f1(double x)
{ System.out.println("f1(double)"); }
void f2(char x) { System.out.println("f2(char)"); }
void f2(byte x) { System.out.println("f2(byte)"); }
void f2(short x)
{ System.out.println("f2(short)"); }
void f2(int x) { System.out.println("f2(int)"); }
void f2(long x) { System.out.println("f2(long)"); }
void f2(float x)
{ System.out.println("f2(float)"); }
void f3(char x) { System.out.println("f3(char)"); }
void f3(byte x) { System.out.println("f3(byte)"); }
void f3(short x)
{ System.out.println("f3(short)"); }
void f3(int x) { System.out.println("f3(int)"); }
void f3(long x) { System.out.println("f3(long)"); }
void f4(char x) { System.out.println("f4(char)"); }
void f4(byte x) { System.out.println("f4(byte)"); }
void f4(short x)
{ System.out.println("f4(short)"); }
void f4(int x) { System.out.println("f4(int)"); }
void f5(char x) { System.out.println("f5(char)"); }
void f5(byte x) { System.out.println("f5(byte)"); }
void f5(short x)
{ System.out.println("f5(short)"); }
void f6(char x) { System.out.println("f6(char)"); }
void f6(byte x) { System.out.println("f6(byte)"); }
void f7(char x) { System.out.println("f7(char)"); }
void testDouble() {
double x = 0;
System.out.println("double argument:");
f1(x);f2((float)x);f3((long)x);f4((int)x);
f5((short)x);f6((byte)x);f7((char)x);
}
public static void main(String[] args) {
Demotion p = new Demotion();
p.testDouble();
monitor.expect(new String[] {
"double argument:",
"f1(double)",
"f2(float)",
"f3(long)",
"f4(int)",
"f5(short)",
"f6(byte)",
"f7(char)"
});
}
} ///:~
shhgs@wgqqh.com
a0 12
a1 a2 42 a1
% ( 3 (
(? ˉ? ( I8A′ Y Z? ; P A? ( P ( \ k g $,,X Z T? V,
v w 0 1 3
[ $? ^ *,6 3 — Z? V%? Z T (,A% #? 3
9 c,!? Z m ( \ ( I Z X
T ( Q ˇ8? %? e m,> ( \ I( R S (y
void f() {}
int f() {}
F
5 k T,(? V E J k? r 1 ! ` N ( M E T ( ( )? X q N D T ( | § 1?
B? § 1 N
ˇ D E? ˉ N y
f();
Y Z
w? O v w N r (,w? Q ` X ; T Q? (
c? d g ^ ( (G H I
[? Iˇ(G H I 3 )L M? ¥K ˇ( I(G H I? ˉ,3 mG H I( Y Z? V”? L M 3 (G H I? y
//,c04:DefaultConstructor.java
class Bird {
int i;
}
public class DefaultConstructor {
public static void main(String[] args) {
Bird nc = new Bird(); // Default!
shhgs@wgqqh.com
a0 13
a1 a2 42 a1
}
} ///:~
,
new Bird();
D m? O?^ AN,Y (G H I Y L M,3
(¥K m A c G M ¥K ( M N % O?,G H I
X { X { I? V X,` w (G HI,y
class Hat {
Hat(int i) {}
Hat(double d) {}
}
F? ˉ? ` $ y
new Hat();
V, $ A g? (G H I }? ˉ? X k
G H I? V” $ O % G H I( D E 3 ˇ? ˉ,3 G H I? V $,3 G H I Y $ 0 1? FT Z s 9? X O? (G H I
$ X % ˇ
ˉm e > (¥K;,| w? N e ¥K(
class Banana { void f(int i) { /*,.,*/ } }
Banana a = new Banana(),b = new Banana();
a.f(1);
b.f(2);
ˉ m 3
( Y Z A w? ` ¥K FN A
shhgs@wgqqh.com
a0 14
a1 a2 42 a1
,; ˙¥K ˇ u (? o ¥K ( Q ’? V] * T,V 5? D (Y ¥K (
,B 1 3 fl? % ( I?
e N,y
Banana.f(a,1);
Banana.f(b,2);
3 E · D E? X ; ’? V[ X,j A; #? 0,Z b? | F? ¥K (
( ) (? V ( A m ˉ
,3? y; ) E A? T N (¥K(
E P ¥K ( 1 ¥K (
‘ a? ˉ? | F ( (?N ( Y Z q
j N,¥K (,r )§ A? E Z $ y
class Apricot {
void pick() { /*,.,*/ }
void pit() { pick(); /*,.,*/ }
}
E F
p % a3 a7 a8 a6? V” r? 1 currency1 (
v )Y % ¥K (
(6 7 5 Q $ A? B ) E T ¥K (
//,c04:Leaf.java
// Simple use of the "this" keyword,
import com.bruceeckel.simpletest.*;
public class Leaf {
static Test monitor = new Test();
int i = 0;
Leaf increment() {
i++;
return this;
}
void print() {
System.out.println("i = " + i);
}
public static void main(String[] args) {
Leaf x = new Leaf();
x.increment().increment().increment().print();
monitor.expect(new String[] {
"i = 3"
});
}
shhgs@wgqqh.com
a0 15
a1 a2 42 a1
} ///:~
( )
^? T,¥K ( ¥ 3 ¥K,V currency1 1,
3? V G H I( M,,F3
G H I?N 3 G H I? E ^? Q,N
5 k? $
( M A ^? ¥K ˇ ¥K ˇ(? m
V £ Q A ; T ¥K ( FG H IA3 I( M
m X (?,A”? r N? I(G H I )? m,3 ;,r N § AG H I(,y
//,c04:Flower.java
// Calling constructors with "this."
import com.bruceeckel.simpletest.*;
public class Flower {
static Test monitor = new Test();
int petalCount = 0;
String s = new String("null");
Flower(int petals) {
petalCount = petals;
System.out.println(
"Constructor w/ int arg only,petalCount= "
+ petalCount);
}
Flower(String ss) {
System.out.println(
"Constructor w/ String arg only,s=" + ss);
s = ss;
}
Flower(String s,int petals) {
this(petals);
//! this(s); // Can't call two!
this.s = s; // Another use of "this"
System.out.println("String & int args");
}
Flower() {
this("hi",47);
System.out.println("default constructor (no
args)");
}
void print() {
//! this(11); // Not inside non-constructor!
System.out.println(
"petalCount = " + petalCount + " s = "+ s);
}
public static void main(String[] args) {
shhgs@wgqqh.com
a0 16
a1 a2 42 a1
Flower x = new Flower();
x.print();
monitor.expect(new String[] {
"Constructor w/ int arg only,petalCount= 47",
"String & int args",
"default constructor (no args)",
"petalCount = 47 s = hi"
});
}
} ///:~
G H I
# c E N 3 G H I X ; N e ‰? F ’ (b? N G H I !? V,
’ ',3
( I ( \ (I ) ( \ > ) 0,e E
Q £ N ! " A ^? % (I ) F
’ Y [V currency1 g,
E F
< g? VX,˙? F?N G H I fl A G H I
" #,
u? ; c¢ £ (?,A(? m m
X ; F?N fl (
a3 a7a9a4 a6
$ 9 ^ Q E (? % E X ^ ¥K j ¥ N
& J (? ’?( ( c) H I3?,9
X * m c) H I ( ; +! § A
I ) [ m,$
m c) H I(? m AX? o ¥K (?X ;
o ¥K A m,- * (? ˉ?,V
Y Z? ′ m 3 k?(b,,
Y m?,fl? % A Ac X? (
ˇ v w ;?¢ / 0 c X /? & J
(? ˇT ( 1
’ ) c? 0 1 f % ( % G c, * ‘ & ¢ ( % 2
m 3,& ¢ 3 ¥ 4 Q $ u v (? 8 ˇ X 4 c( 9
m R S T U V A E T U ` (¥KD > (E F F 5 | 3 k? 3 6 7 5 y b ¥K D > (
shhgs@wgqqh.com
a0 17
a1 a2 42 a1
3?6 7 (ˇ X (E F R S T U V 0 1 w? Qk
(E F D E AX 0 1 w? Qk?6 7 ˇ(E F,Z ¢ 5
d e,3 (? E O? ( k? c 6 | (A(y 1 · R S T U V?Qk ¥K D > (E F ( M A” / N
m g k37 R S T U V8,Qk E F D E? ˉ?
Y Z R S T U ( M? E,3 % (& ¢ y 1,
3 9 F(?,; m ’ ) 6
( ’ ) g ( M,
(< G H I
D = < G H I ¥K & ¢ ( M p q N (H I?3 O % & >
( b ’ m (¥K 4,g & ¢ (
¥K X 4,B T U (? u y
! ¥K X 3 O,B R S T U VT U
" ! R S T U X < G ‘ a ; … V ',A(? m? ˉ¥K FB l u p
q,3 1 (? p q @ Q T 1 m < G H I? A ( J? ; L M 3 Q,& ¢ B Q$ b ¥K FL M (^ * P C g,D E J % F G }
( ’ A H I [ X,B …? ˉ? P F G } ( ; T F
Y Z R S T U ( M 3? X ;,f R S T U V3 O,J
,/ N ) G },/ D E J 6? ˉRS T U VX J Y Z G } H I ; FJ?;,m ¥K (E F 3 B Qk ’ m
E F X K? ˉ ’ L,M N R S T U V m Qk ^ E F Y Z ’ _! ( M E F,c 1 O P T fl? R ST U m Q (? ˉA; X J Y Z Q ; R C
Y Z? ˉ
X ; (& ¢ Q Y A m Z Z
% ‘ a ( L C y
# ! R S T U E F m [ $ R S T U V(? 3 £( % Y X ; ` ’ D (E
F S ! Q D E R S T U > (3 T B # $ (
p q E F E h E F (Qk >
shhgs@wgqqh.com
a0 18
a1 a2 42 a1
X $? ˉ¥K #,3 (¥K v w? r P Y ¥K [?Qk, U X V X / ¥K E Z ·L M ( EF Qk? ( R S T U V? (? < Q m F3 fl? 6 7 (W w
8,% g W w E F E L M ¥K E ‰ ( ·B?¥K M ;? [? g,
(3 T? ¥K D E Z ;
< Q % m
( ’ ) E X (3 X 3 A
( Q E F % ( - ( 3 F
’?N fl ’ ( $
F (Y Z %* ( & ’ ( m A([ ‰ [ E
! % ) ! k? $ £ d e,
(\ ] Ac E N (? & J? E N Ffl
’ ;,N ( 3 ^ (H I fl? N
! AX,Qk D (E F ),E F _ ‘ 9
(H I? F
Q N A < g ′ [?,%
(,X V a3 a7 a7 a6¥,XJ (& ¢ B v w 0 (r Y ZJ (&¢ B v w Fr?,
! " # $ ! " # $ ! " # $ ! " # $
% | & ¢
¥K / p q F& ¢ ( M N 3 & ¢ J u v %
(< G H IF J J m 3 n o (¥K?,B &
*? $ ¥K? p q B &? ˉ (¥K E r
( ·L M ( [ $ L M Fa? X ; (T g (
Y Z $? b 1 c ( M ¥K B &,? ˉ¥K
L M ( 3? Y Z ’ ) N (
L ˉ ( M m,N ¥K (< G H I,?ˉ
( ’ ) *,N Y Z < G H I H I [ X,B N? ; k,3 E F (‘ d ‰ ¥K (§ o [ X,g & ¢,
U e ( A[ ˙ ( ’ ) o (3 % (
>
X *? L M r ¥K +? p q
m Qk ¥K (? ˇ e N R S TU V” f? Qk E F D E u % ! " (g z Q '? E m,R ST U Vu
X ` % < G H I,9?,( h?,R S T U V m cˇ < G H I
` J? X ; j N
F ! " m P 3 (£ N t? ˉ? %,3 Qk E F E ‰ (& ¢ B Y ; N
,(< G H I3?,3 C i? > X g
shhgs@wgqqh.com
a0 19
a1 a2 42 a1
‘ a R S T U? j X a ( %,( g A % CD E F (r A X,k l 4 Q T U R S E S E F (
% & ’ (% & ’ (% & ’ (% & ’ (
4 u? X ; 7
6 r L M?& ¢ ˇ Y r N? < Q m F,m (E F & ¢ ( M
8 n J W? ′ V I( ’ ) X g A
m 3 m o ( p A X % [ currency1? B N g,q r ¥K (* + s currency1
a3 a7 a10 a6? ¥t ¥K X ` u v o ( M? %,& ¢ ( M ¥K v
w Z ); cQk § E F (w x Q $? ˉ¥K 3 ] ( currency1 Y Z FR S T U u v w / b Y currency1? ˉ¥K m y z,& ¢ ( Y Z ’ 9 { k,3 U e (; |
(-,” X [ currency1? ; B N A[ E 1 b 3 1 } Q q ~ 5 % m 3 currency1
-,J,? ; r g 8? q (
k? 3 u v ( #? w? y
//,c04:TerminationCondition.java
// Using finalize() to detect an object that
// hasn't been properly cleaned up,
import com.bruceeckel.simpletest.*;
class Book {
boolean checkedOut = false;
Book(boolean checkOut) {
checkedOut = checkOut;
}
void checkIn() {
checkedOut = false;
}
public void finalize() {
if(checkedOut)
System.out.println("Error,checked out");
}
}
public class TerminationCondition {
static Test monitor = new Test();
public static void main(String[] args) {
Book novel = new Book(true);
// Proper cleanup,
novel.checkIn();
// Drop the reference,forget to clean up,
new Book(true);
// Force garbage collection & finalization,
System.gc();
monitor.expect(new String[] {
"Error,checked out"},Test.WAIT);
}
} ///:~
shhgs@wgqqh.com
a0 20
a1 a2 42 a1
* + s currency1 D m (
! ¥K FB 1 R S T U u v w /? T Q
’ ) F? ( M,? m 3 T Q? ˉX
q r * + s currency1 %,U

" Q,-? E F k ^ *? Q N 5
,X Z T? ^ L,[ m ;,b,B ! Y - 9? (
! b ’,? K V (EF ) J,R S T U V
) * +,-,/ 0 1 ) * +,-,/ 0 1) * +,-,/ 0 1) * +,-,/ 0 1
ˉ? m ^ 0 1 P ¥K k F? 8? ( Y Z? 9,
P D m (? k F?( T [ 3 O (?J R S T U V E ′ (d L M ¥K ( z 3 currency1 g $ ( M? ;,m E F (Qk Z,¥E F ( m
,( (y 1 · Y F? E F ( z E § G Fa? E F ( z >
Q $? E P
( ¢ £ 3 W ([ ¥K? %? ¢ A Y r ^ 3 4 r,B? ! Q Y s t F3
,( ( m X,A } 3 s [ currency1 ¥K ( M,X ` o ( { ¥K E F,fl? A? ˇ u v r o? (Z r D E
& J (a ( 3? (,9 m 3 C ‰ (‘ ( Q C Q E F? 4 > 8 X 3 d,
F? ; [,? & J X 3 s {? ˉ ( Z T (
fl? r J E F ( * ¥ ;( ′ (
C D O P? @ R S T U V AT UR S ( M,P (¥K c & J P? ˇ { ( o ˙ AI W? ` l
R S T U V” ‰ E F,3 ( m (? 4 e (E F
% ¢ £ R S T U V
u,? y 1 (? /,£ b t ( ¢
3 u v ( %8? (( t A(? m [ ¥K? % # 3
IV [ currency1 m
g ¥K ( M IV,1 [ currency1 m
1 c? b ( M IV,1? ¢
IV,’ L,@ 4 (3 x X′ %? ¢ (?,R S T U V£? 3 ¥ D m (¥K g
IV? (¥K Qk § E F t m,C Y
shhgs@wgqqh.com
a0 21
a1 a2 42 a1
ˉ3 R S ¥K > § - Y 3 currency1 ( Y Ac (
IV X,?,% ˙ R S T U V ! - (¥K % V (a '
’ I? B )£ QR S T U ( – A j m r
,( O P,b F3 (b t? R S T U X )
I( > § b m | D m,(¥K? v w ; b,g AFa? F?x F(
O? E fi i l ¥K ˉ a? x F? r ¥ D m (; ! c ( B,(¥K g 3
u? ’ g AD ¥K? 9 ’,¥K (
! AD (¥K E fl g? + ¥? H? D ^ ([ ¥K v w? B,(? F m? - (¥K –
/ ˇ(! ",Ac X.,R S,
,( D (? v ( ˇR S T U t,m V g ¥K u R S T U V” § · |? Q NO w? Z ¢ ¥K § * m 3?
ˇ( $ )t? ( ’ p q /? kQ
X 3 mT U t
9 P B,(¥K 3 g 3 /Y R S,‰ ¥K B g ( M 3? 3 ( ) B,,
Y? !,(? 4 9 ¥K 3 r g 3 r ( M D m o A
& J
( p q 1 F x F? ( E … J ; m 3 %?‰ ˇ8 ;,g (
[ o ¥K
,F,g ( M ` 1 E b | F r? u 4 m 3? §
m e ! " D = ( ·T U V
* ˇ( ` X 3? m e 9 F e?Q T r ′ (E F m
,( ^ ′ E F Y j P 3 E F g 3 E F Q £ N ! "
! " 3 ˉ ’? O,A,0 ˙ ( 2 3
X 0 R S ·T U V % P 3 E F?( c g 3 E F k l,,¨? 5 ( 0 m
,(,Fq ~ g ’ X ` 0 (R S ( M T? g 3 y 1 ·k
D = (? v ˇ,? 3 y 1 ·ˇ D = ( & £
ˇ · @ (,( D ( t F3 X 5 k & £ ˇ >? 0 1 ’ 0 ˙ 2 3 X 0 R S ( M A A,
& £ ˇ(m [ a? x F? ! ’? D m (
9 ! B,(¥K [ currency1 A g 3 B,(¥K (
shhgs@wgqqh.com
a0 22
a1 a2 42 a1
M A” F¥K b 3? ‘ X ^ AX,… J T U ¥K m? ‘ ^ cM N u A8,& ¢ & ¢ ( M,Qk,(¥K AX, D E? ˉR S T U V%,?,? (
( A ; ‰ ¥K, + ˇ(? m ·(R S T U X F m,> T
U R S ( M ’ p q c? kQ F (,V $ R S T U V 3 R? /? ( m < 9 Q A X ( 3 ˙ @ (
,( X > (R S T U V”FE F X K ( M J ‰ & £ ˇ[ % ’ /? kQ
c / d g ^ (
,(,′ ′ r E F ′ ¥K,g 3 ˇ )A (E F —( + ˇ% FQk u / P B,(¥K g % > V E F m,
u T U ( M E? l ( ¥K,[? m 3 ‘ Z A ! B,(? IV
ˇ? 5 k m J currency1 R S T U u 8 L M ( 8,B,§ A? ˉF Z r B - ^ (,F IVJ ! Q? ; v V (
¥K,h (& ¢,O @,′ ¥K X,” F IVJ T
,F ¥K (,B,,(,R S TU ( `? ˉD m ¥K O R S T U,3 k l ( A,T? g & £ ˇ · ¢
,( [, & £ ˇ( `? ˉ? X,A T? T + ˇ D =(? v ˇ,D E t } (? v ( (? +
& £ ˇ
,( m V ( ; d ` (r b % ( Y? Vm ( B
0 1? V(,0 1? V” r?c r ’ q
,( (£ Q,; L,A? ( M
5 k 3 currency1 L M Y (¥K ( M
’,/ g currency1 9 \ * E F E j
0 1? VQ?,Am e,C y3 % 3 C 4 r ¢ F ’ Z,9 Q ‰ A,,’ ( z
\? % 8 h ( 0 1 V;,? E F ( ¥ ` ( —,m 3 D = (?B,a
ˇ(? X g p %( M X 1
0 1? b & 2 ( 3 – O,A ( [ currency1 L,( M? 1 3 C? %?,V A( z [ A
shhgs@wgqqh.com
a0 23
a1 a2 42 a1
,,+ F u?,,f % T % h? ˉ + F E O?() +,i? (? — D E ’ y
void f() {
int i;
i++; // Error -- i not initialized
}
,g 3? — #?; m f % 9? V E?
3,? < J } ’ ) (,,? u,P,’ ) d e f % ( } F
ˉ
( ) I Y Z 5 m X,()? ; f %? g I % / F I u ¥A,(f % X Y Z?,9 P R S ;?A[
X c( D E ( (I? ;,3 f Fk? (?y
//,c04:InitialValues.java
// Shows default initial values,
import com.bruceeckel.simpletest.*;
public class InitialValues {
static Test monitor = new Test();
boolean t;
char c;
byte b;
short s;
int i;
long l;
float f;
double d;
void print(String s) { System.out.println(s); }
void printInitialValues() {
print("Data type Initial value");
print("boolean " + t);
print("char [" + c + "]");
print("byte " + b);
print("short " + s);
print("int " + i);
print("long " + l);
print("float " + f);
print("double " + d);
}
public static void main(String[] args) {
InitialValues iv = new InitialValues();
iv.printInitialValues();
/* You could also say,
new InitialValues().printInitialValues();
*/
monitor.expect(new String[] {
shhgs@wgqqh.com
a0 24
a1 a2 42 a1
"Data type Initial value",
"boolean false",
"char [" + (char)0 + "]",
"byte 0",
"short 0",
"int 0",
"long 0",
"float 0.0",
"double 0.0"
});
}
} ///:~; < g D m,? Ac? r f %,
(
4 ] ¨ ! Q D E 3 ˙? X,m % z? f % ( Q y 1 (
E?,< g? ˉF?O?,3 ¥K (
% m ¥A,f % Y Z Y
,B? J 3 6 7 ( [
(? 2 3 4 5 62 3 4 5 62 3 4 5 62 3 4 5 6
ˉ%? +? J f % Y w? T 3 u v ( FO
( + ( M j?A X ; F? Z T X ^ V
( o X — k? # $ ( ) I (O? c 1,˙ AFO?I ( M d e f % ( y
class InitialValues {
boolean b = true;
char c = 'x';
byte B = 47;
short s = 0xff;
int i = 999;
long l = 1;
float f = 3.14f;
double d = 3.14159;
//.,,
[ E? ¥fl
(¥K,f %? ˉ% 3 Y Z? E? L M + Y,f % y
class Measurement {
Depth d = new Depth();
//,,,
shhgs@wgqqh.com
a0 25
a1 a2 42 a1
ˉ f,,Y Z,g 3 B?
' (L, 2 3 E N Q ˇ f % ( y
class CInit {
int i = f();
//..,
}
9 [ E m I IX ; (§ Ay z f %
( )? E? T y
class CInit {
int i = f();
int j = g(i);
//..,
}
X ;? T y
class CInit {
int j = g(i);
int i = f();
//..,
}
¥? VQ $ F r ¥o -
^ X w ( f % (z’ m Y ’ [ X (
Q,f % s u v m j Am )
# $ (D m ¥K?,g 3 f % m?| % ( V M? % ′ (? B
4 5 6 4 5 6 4 5 6 4 5 6
E G H IQ,f % ; { Q ′ (? B
E N Y FL,,1 E O f % ( m 3currency1 % ‘ a y? m ‰ (f % AFG H IL,u?,D E Q $? ˉ ’ y
class Counter {
int i;
Counter() { i = 7; }
//,,,
shhgs@wgqqh.com
a0 26
a1 a2 42 a1
/,B f % 4 9 8 5 ¥) ¥K ( 2 3 O?(?f ( ) I [ X ‰ ( 0? VX,? ; FG H I( Z r,f %? F
u 4,f % f %? m,,i a3 a7 a11 a6
¥ f % (z’ ( + F (O (z’ D N O ( +
(O? ;,¢ F O?( r Y (O?> § + (f %,/ ) 2 3 G H I(N y
//,c04:OrderOfInitialization.java
// Demonstrates initialization order,
import com.bruceeckel.simpletest.*;
// When the constructor is called to create a
// Tag object,you'll see a message,
class Tag {
Tag(int marker) {
System.out.println("Tag(" + marker + ")");
}
}
class Card {
Tag t1 = new Tag(1); // Before constructor
Card() {
// Indicate we're in the constructor,
System.out.println("Card()");
t3 = new Tag(33); // Reinitialize t3
}
Tag t2 = new Tag(2); // After constructor
void f() {
System.out.println("f()");
}
Tag t3 = new Tag(3); // At end
}
public class OrderOfInitialization {
static Test monitor = new Test();
public static void main(String[] args) {
Card t = new Card();
t.f(); // Shows that construction is done
monitor.expect(new String[] {
"Tag(1)",
"Tag(2)",
"Tag(3)",
"Card()",
"Tag(33)",
"f()"
});
}
} ///:~
shhgs@wgqqh.com
a0 27
a1 a2 42 a1
c? P ¥K (O? ¢ g & ( r | E f? f %,FJ G H I? 0 ( Z u ‰ G H I ¥
’,,3 f %? E ’ ( ! * < g
’ ( B f %,e currency1 y3 currency1 FG H IN u
3 ¥K B? …,” B 1 R S TU
T ( ` } X Z? % ;,f,(f % 5 | 3 k? ˉ,3 X ¥
’,f % (G H I O?
’ ( M m d e?,? ˇ(f % Y,

I Q $ > (y? ˉA 3? m f % Y Z A” g 3? (
(f %? ˉA 3 ¥K (
Y Z fl? L M 3 (¥K P g Y ¥K ! A
ˉ FO?( M f % Y fl
I Z e?,/L M V ˙ ¥K
I ; m 3 % ¥ I,f % ( M ! " Q,c ’ Q P ! " ' & > y
//,c04:StaticInitialization.java
// Specifying initial values in a class definition,
import com.bruceeckel.simpletest.*;
class Bowl {
Bowl(int marker) {
System.out.println("Bowl(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}
class Table {
static Bowl b1 = new Bowl(1);
Table() {
System.out.println("Table()");
b2.f(1);
}
void f2(int marker) {
System.out.println("f2(" + marker + ")");
}
static Bowl b2 = new Bowl(2);
}
class Cupboard {
Bowl b3 = new Bowl(3);
static Bowl b4 = new Bowl(4);
Cupboard() {
System.out.println("Cupboard()");
b4.f(2);
}
void f3(int marker) {
shhgs@wgqqh.com
a0 28
a1 a2 42 a1
System.out.println("f3(" + marker + ")");
}
static Bowl b5 = new Bowl(5);
}
public class StaticInitialization {
static Test monitor = new Test();
public static void main(String[] args) {
System.out.println("Creating new Cupboard() in
main");
new Cupboard();
System.out.println("Creating new Cupboard() in
main");
new Cupboard();
t2.f2(1);
t3.f3(1);
monitor.expect(new String[] {
"Bowl(1)",
"Bowl(2)",
"Table()",
"f(1)",
"Bowl(4)",
"Bowl(5)",
"Bowl(3)",
"Cupboard()",
"f(2)",
"Creating new Cupboard() in main",
"Bowl(3)",
"Cupboard()",
"f(2)",
"Creating new Cupboard() in main",
"Bowl(3)",
"Cupboard()",
"f(2)",
"f2(1)",
"f3(1)"
});
}
static Table t2 = new Table();
static Cupboard t3 = new Cupboard();
} ///:~
! ; ˙? < g (L M & F§ (O ¢ L M,
(! ( )? & FO?
( ) u / L M,3 fl (! ’? E ’ ( ! < !
),F % ( M,f %? ˉ? m L M
¥K? H I X ; g (?
) [ X,L M (! ( ) m L M,3
¥K u? 3 currency1 + ! ) ( M Ac 8,B f %
¥K X,` 1 f %,
shhgs@wgqqh.com
a0 29
a1 a2 42 a1
ˉ/ m L M ^ ¥K § ( ) y z f % ( f %,/ Z ¢ §
) ` Z ¢ fl (¥K !? f?,3 C
¥L M ¥K ( 1 3 4 M m? (
% y
3 currency1 L M (¥K G H I? & J? 3 currency1+ !
( (?\ ( M £ QV” %
g a12
,u c E,? g L M,¥K u,¥D m (
I,f %? 3? ¥K ( M,/,
) (f %
L M ¥K ( M ¥K (G M,/ F? ¥K? K (E F
E F / B & r P ¥K ( ( )? J,? (
¥)I\ > v (
b
,O? ) I D 1 (f %
6.,G H I F? < g ( ;, g > V (B 6 m? ( M; ˙? 6 7 (? ˇP § A(? x f % c? – 9 Q
m B A( ·? (y
class Spoon {
static int i;
static {
i = 47;
}
//,,,
A< J } 3? & J 3 ’ F
( (
(f % 3?,,3 currency1 y 3 currency1 L M (¥K? 3 currency1 + ! (
( ) ( M,? L M ^ Y (¥K
Q $ y
//,c04:ExplicitStatic.java
// Explicit static initialization with the "static"
clause,
import com.bruceeckel.simpletest.*;
class Cup {
Cup(int marker) {
System.out.println("Cup(" + marker + ")");
}
void f(int marker) {
shhgs@wgqqh.com
a0 30
a1 a2 42 a1
System.out.println("f(" + marker + ")");
}
}
class Cups {
static Cup c1;
static Cup c2;
static {
c1 = new Cup(1);
c2 = new Cup(2);
}
Cups() {
System.out.println("Cups()");
}
}
public class ExplicitStatic {
static Test monitor = new Test();
public static void main(String[] args) {
System.out.println("Inside main()");
Cups.c1.f(99); // (1)
monitor.expect(new String[] {
"Inside main()",
"Cup(1)",
"Cup(2)",
"f(99)"
});
}
// static Cups x = new Cups(); // (2)
// static Cups y = new Cups(); // (2)
} ///:~
/ ^?
(Y,’ + ! ( (¥K P Q… ˙ A L,?
" (Y,& ( f % ’?,g,? ˉP
" Q… & ( f % X,0,‰ B 3,e,
"? & J? O? x f % L,3 currency1
d e,3 A ( Q f % ¥K (fl ( + k? ^ y
//,c04:Mugs.java
// Java "Instance Initialization."
import com.bruceeckel.simpletest.*;
class Mug {
Mug(int marker) {
System.out.println("Mug(" + marker + ")");
}
void f(int marker) {
System.out.println("f(" + marker + ")");
}
}
shhgs@wgqqh.com
a0 31
a1 a2 42 a1
public class Mugs {
static Test monitor = new Test();
Mug c1;
Mug c2;
{
c1 = new Mug(1);
c2 = new Mug(2);
System.out.println("c1 & c2 initialized");
}
Mugs() {
System.out.println("Mugs()");
}
public static void main(String[] args) {
System.out.println("Inside main()");
Mugs x = new Mugs();
monitor.expect(new String[] {
"Inside main()",
"Mug(1)",
"Mug(2)",
"c1 & c2 initialized",
"Mugs()"
});
}
} ///:~
E < g? f %
- ( y
{
c1 = new Mug(1);
c2 = new Mug(2);
System.out.println("c1 & c2 initialized");
}
,m
A? x f % Z e? E
(f % d e,p X ˙ (\ ]
F
f % I– 3 currency1 s? R S ! ( D = (? w f %
ˇ′ ′,^ ( c
a13 a14 a15 a16( )
(3 T? ¥K A m (? w
ˇ A[ m I– Y d e,I– (f % I– 3? F 3 ˉ k(u v ’? ’? s E
( [ E 3 (¥K ( I– ^ ( $? * + $9 Q (I– k?
Q O? ( % O?3 I– % j F ( \? J 3 ¥? ( $?,,y
shhgs@wgqqh.com
a0 32
a1 a2 42 a1
int[] a1;
[ E $? k g ˉ ( T (? m > (y
int a1[];
’ ) c D? | ( 3? ; 3 AF$?3
(I– ˇ O P,?
VX *? # AI– m V ′ ˙ c T g,?
ˇ " F? D g ( 3 I– (
I– (E F % I– L M F 4? p q,f % ¥I– Q$ f % E F ’ ( r,? [ E 3 6 7 (f
% ˙ AFL M I– ( M,f % 6 7 (f % 3¥ $? P $9 Q F 5 k E F,(? V
- ( Q Z ¢,? y
int[] a1 = { 1,2,3,4,5 };
Y Z Z % F m I– (5 k O?3 I– (

int[] a2;

E 3 I– 3? E $
a2 = a1;
FT (
D ^ (y
//,c04:Arrays.java
// Arrays of primitives,
import com.bruceeckel.simpletest.*;
public class Arrays {
static Test monitor = new Test();
public static void main(String[] args) {
int[] a1 = { 1,2,3,4,5 };
int[] a2;
a2 = a1;
for(int i = 0; i < a2.length; i++)
shhgs@wgqqh.com
a0 33
a1 a2 42 a1
a2[i]++;
for(int i = 0; i < a1.length; i++)
System.out.println(
"a1[" + i + "] = " + a1[i]);
monitor.expect(new String[] {
"a1[0] = 2",
"a1[1] = 3",
"a1[2] = 4",
"a1[3] = 5",
"a1[4] = 6"
});
}
} ///:~
E < g
(?,3 – f % ( ) m Q ¥ ),,? F? ‰ 3 I–
m C ( yD m I–
X / A ( ¥K (? m3 e r (E )? X ; A” #? I– mV ˙ < = )
( ) (I– (3 (? ; + ! (b′ (I– k?
,
(? ˉ (I–,? ^,A[, r j ˙ k? g E F? 3 ' V — (
( 0 (; ˙? ! " ( 3 ˉ? !,” - 3 L,?
( " 9 [ currency1 + !I– ( M? % 4 q r Y [,? P A,[ $? ˉ ` fl? % Y Z + ! I– ;,’ `
(3 =,0 ( c ’ ) (? ` (b c - (
ˉ? ’ ( M X 0 1 I– % m V ˙ < = Y w Z E
j Q L M I– (< = 2 3 ; L M (I–
X ; )L M fl I– ( y
//,c04:ArrayNew.java
// Creating arrays with new,
import com.bruceeckel.simpletest.*;
import java.util.*;
public class ArrayNew {
static Test monitor = new Test();
static Random rand = new Random();
public static void main(String[] args) {
int[] a;
a = new int[rand.nextInt(20)];
System.out.println("length of a = " + a.length);
for(int i = 0; i < a.length; i++)
System.out.println("a[" + i + "] = " + a[i]);
monitor.expect(new Object[] {
"%% length of a = \\d+",
shhgs@wgqqh.com
a0 34
a1 a2 42 a1
new TestExpression("%% a\\[\\d+\\] = 0",
a.length)
});
}
} ///:~
(
m y -
- ¥K,j 3 · As E ( · [ E ·?D ^ ( ‰ A % 3 ^ ·” (currency1 I(
- X ; ˙ X p % ( Y ; FL,` § (currency1 I }
I– (′ (
,#? (,T 3? g Y Iu 4 ( ( )A? 0 ( & > I–? & J FL,L M ( ‰ ’ ( ! [ f?,
(I– < =,B f % ˇ
¥)I\? ¥)
9 I– (O? f % [ E w 3 y
int[] a = new int[rand.nextInt(20)];
ˉ E ( v w? / (
ˉ? % Z ¢ (X
I– ¥K I– Y? p q
,(! " Q,? L M ( 3
(I– | 3 k # A X
y
//,c04:ArrayClassObj.java
// Creating an array of nonprimitive objects,
import com.bruceeckel.simpletest.*;
import java.util.*;
public class ArrayClassObj {
static Test monitor = new Test();
static Random rand = new Random();
public static void main(String[] args) {
Integer[] a = new Integer[rand.nextInt(20)];
System.out.println("length of a = " + a.length);
for(int i = 0; i < a.length; i++) {
a[i] = new Integer(rand.nextInt(500));
System.out.println("a[" + i + "] = " + a[i]);
}
monitor.expect(new Object[] {
"%% length of a = \\d+",
new TestExpression("%% a\\[\\d+\\] = \\d+",
a.length)
shhgs@wgqqh.com
a0 35
a1 a2 42 a1
});
}
} ///:~
I– (L M 2 3 E k F
(? y
Integer[] a = new Integer[rand.nextInt(20)];
3
(I– D E % m L M (# ¥KQ f %
f % (^ m M N y
a[i] = new Integer(rand.nextInt(500));
9? ˉ? *,L M ¥K Y Z ’ L,( M? % g I– (?
( M,g 3? c Q < < ] ¨ (
¥K (G?,< g # ¥K (
,B r? ^ ( [ E 3 ( $? $9 Q (¥K? Q f % ¥K I– Am e
·y
//,c04:ArrayInit.java
// Array initialization,
public class ArrayInit {
public static void main(String[] args) {
Integer[] a = {
new Integer(1),
new Integer(2),
new Integer(3),
};
Integer[] b = new Integer[] {
new Integer(1),
new Integer(2),
new Integer(3),
};
}
} ///:~
3 ·; ) V W w A()? ′ I– (′ F?
( M? O,? (b 3? ( 6 ;? ( y 1 u v 3
shhgs@wgqqh.com
a0 36
a1 a2 42 a1
·d e,3 u ( ; g ( I?
[ B (? ˇ ( ˉ( L M N ( As ; ) II+ z 0 (W w [ ; ) I z 0 (Ww ( )D m (? @? K (
/ 0?,( h?,¥ " m (¢ £
E L M 3 / 0 I– 1 I( 9? N y
//,c04:VarArgs.java
// Using array syntax to create variable argument
lists,
import com.bruceeckel.simpletest.*;
class A { int i; }
public class VarArgs {
static Test monitor = new Test();
static void print(Object[] x) {
for(int i = 0; i < x.length; i++)
System.out.println(x[i]);
}
public static void main(String[] args) {
print(new Object[] {
new Integer(47),new VarArgs(),
new Float(3.14),new Double(11.11)
});
print(new Object[] {"one","two","three" });
print(new Object[] {new A(),new A(),new A()});
monitor.expect(new Object[] {
"47",
"%% VarArgs@\\p{XDigit}+",
"3.14",
"11.11",
"one",
"two",
"three",
new TestExpression("%% A@\\p{XDigit}+",3)
});
}
} ///:~
,< g
3 / 0 I– 1 I A” ¥? I– `P ¥K? ] ¨ ! Q? (
4 ; 0 R S ¢ £ ( !?D L M (
1 $ 1 (¥K,/ ] ¨ ( \ 9
6 ˉ? E h 3 ^ 7 I( · 2 3 4 % 5 b Y
^,m 3?V 7 (I\ ) § ( 1 b? X O? (
c E,' ( ]¨ ( \ 9 ¥K (r?; ˙? R S r L M V I– y
shhgs@wgqqh.com
a0 37
a1 a2 42 a1
//,c04:MultiDimArray.java
// Creating multidimensional arrays,
import com.bruceeckel.simpletest.*;
import java.util.*;
public class MultiDimArray {
static Test monitor = new Test();
static Random rand = new Random();
public static void main(String[] args) {
int[][] a1 = {
{ 1,2,3,},
{ 4,5,6,},
};
for(int i = 0; i < a1.length; i++)
for(int j = 0; j < a1[i].length; j++)
System.out.println(
"a1[" + i + "][" + j + "] = " + a1[i][j]);
// 3-D array with fixed length,
int[][][] a2 = new int[2][2][4];
for(int i = 0; i < a2.length; i++)
for(int j = 0; j < a2[i].length; j++)
for(int k = 0; k < a2[i][j].length; k++)
System.out.println("a2[" + i + "][" + j +
"][" +
k + "] = " + a2[i][j][k]);
// 3-D array with varied-length vectors,
int[][][] a3 = new int[rand.nextInt(7)][][];
for(int i = 0; i < a3.length; i++) {
a3[i] = new int[rand.nextInt(5)][];
for(int j = 0; j < a3[i].length; j++)
a3[i][j] = new int[rand.nextInt(5)];
}
for(int i = 0; i < a3.length; i++)
for(int j = 0; j < a3[i].length; j++)
for(int k = 0; k < a3[i][j].length; k++)
System.out.println("a3[" + i + "][" + j +
"][" +
k + "] = " + a3[i][j][k]);
// Array of nonprimitive objects,
Integer[][] a4 = {
{ new Integer(1),new Integer(2)},
{ new Integer(3),new Integer(4)},
{ new Integer(5),new Integer(6)},
};
for(int i = 0; i < a4.length; i++)
for(int j = 0; j < a4[i].length; j++)
System.out.println("a4[" + i + "][" + j +
"] = " + a4[i][j]);
Integer[][] a5;
a5 = new Integer[3][];
for(int i = 0; i < a5.length; i++) {
a5[i] = new Integer[3];
for(int j = 0; j < a5[i].length; j++)
a5[i][j] = new Integer(i * j);
}
for(int i = 0; i < a5.length; i++)
for(int j = 0; j < a5[i].length; j++)
System.out.println("a5[" + i + "][" + j +
"] = " + a5[i][j]);
// Output test
int ln = 0;
shhgs@wgqqh.com
a0 38
a1 a2 42 a1
for(int i = 0; i < a3.length; i++)
for(int j = 0; j < a3[i].length; j++)
for(int k = 0; k < a3[i][j].length; k++)
ln++;
monitor.expect(new Object[] {
"a1[0][0] = 1",
"a1[0][1] = 2",
"a1[0][2] = 3",
"a1[1][0] = 4",
"a1[1][1] = 5",
"a1[1][2] = 6",
new TestExpression(
"%% a2\\[\\d\\]\\[\\d\\]\\[\\d\\] = 0",16),
new TestExpression(
"%% a3\\[\\d\\]\\[\\d\\]\\[\\d\\] = 0",ln),
"a4[0][0] = 1",
"a4[0][1] = 2",
"a4[1][0] = 3",
"a4[1][1] = 4",
"a4[2][0] = 5",
"a4[2][1] = 6",
"a5[0][0] = 0",
"a5[0][1] = 0",
"a5[0][2] = 0",
"a5[1][0] = 0",
"a5[1][1] = 1",
"a5[1][2] = 2",
"a5[2][0] = 0",
"a5[2][1] = 2",
"a5[2][2] = 4"
});
}
} ///:~
] ¨ ’,
A X F O z (I– J,3 ^,
(V I–? $ ! V I– ([ o + y
int[][] a1 = {
{ 1,2,3,},
{ 4,5,6,},
};
– $,P? { k3? I–
^,
Q 3 L I– I– 3 currency1 (y
int[][][] a2 = new int[2][2][4];
shhgs@wgqqh.com
a0 39
a1 a2 42 a1
L ’ ^,– I– ( o +? E? z (y
int[][][] a3 = new int[rand.nextInt(7)][][];
for(int i = 0; i < a3.length; i++) {
a3[i] = new int[rand.nextInt(5)][];
for(int j = 0; j < a3[i].length; j++)
a3[i][j] = new int[rand.nextInt(5)];
}
3
L M,3? z (I– AN O,3 ( z §Ae z 1 N O
currency1 (,< = § L z N ! " ;?,L
E ! < !? ˉ? X?I–? r f ( A(,B
r f % E A (~? Q Z ¢ fl
(¥K I– ^ F A $? P V
·?– 9 Q y
Integer[][] a4 = {
{ new Integer(1),new Integer(2)},
{ new Integer(3),new Integer(4)},
{ new Integer(5),new Integer(6)},
};
^,? 3 3 r L M fl
(¥K I– y
Integer[][] a5;
a5 = new Integer[3][];
for(int i = 0; i < a5.length; i++) {
a5[i] = new Integer[3];
for(int j = 0; j < a5[i].length; j++)
a5[i][j] = new Integer(i*j);
}
6 0,# k 3 m? m (
G H I y )q (f % v w?,? (] ^ #?
V Z ! " (?,%+ Fb
( M G Fbf i? ) F? ` ( e? #* v ′ 1,? )X ( + f % - 9 (′ + (? ! " (?#
U ( ‰ 3 ! " [ )X (& ¢ ( )
shhgs@wgqqh.com
a0 40
a1 a2 42 a1
G H I; ˙?,,(f % & ¢? VX,˙? z N G H I L M ¥K
m,c( c F
< G ^ > % ( L M (¥K p q B? (& ¢ … ( )
(R S T U V” Qk D m ¥K(E F F V W w k A X g & ¢
X ^? [ ',% ( M? ;?,
FY X % < G H I(,(W w
(R S T U V; F′ ′ u %? y 1 (? !J? " # E ( ¢ E F? ( c,i m R S T U V2 3 E T UG } currency1 $ u (§ A? @ R S T U V( Q ′ (
Y ( ) (£ QV Q 8? A¥ ; ( U,a D
F ; ˇ,? ( z ! " 8 9 % & A t t? ’ c (b′ i &
( )%,D m ¥K? ; B r L M G H I? & J % 8?' (
( 6? w? L M ( ( M,i v w 8 9 m,g 3 ( Q d e \] FE (? *?,? g w? E h Ac? ¥K (
L M (
% 3 ) l ;
k?
([? J? m3 * " ( t
L M 3 { G H I X { I(G H I ( G H Ivw ; ] ¨ L M 3 (¥K
+ * * (G H I ˙ A 3 1 I 9 P \ ˉ 3 ] ¨ ! Q
L M 3 + * D O?( (¥K (I– X % L M ¥K [ X % I–? L,’ ( M e 3 k A X,] ¨ N G H ID 0 (f % —
+ * L M 3 – ¥K Y P A g (I–
L M 3 ¥K (I– 9 [ < =? currency1 ] ¨ I–
L M 3? ( % ¥,? Y % N ( ] ¨ !
,
3 P D m (? N 3 ¥
+ * ˙ Am e? e v w m e X z’ > ( I q 3 kA X ;? y 1
! L M 3 X { G H I( 9 L M 3 Y (¥K E q A” X,0 3 (G H I
shhgs@wgqqh.com
a0 41
a1 a2 42 a1
" L M 3 m e ( F 3 ¢ N e currency1 y 3 currency1 X
currency1
# L M 3 m e? G H I( F 3 G H I? N G H I
L M 3 { ( v w ; ] ¨ F
L M 3 (¥K £ Q3 k ’? L,(
+ * ( ’ ˙ A O ; N
L M 3 ( A; [ ; ‰? m * + s currency1 * +s currency1 & ¢ ¥K ( M - v w? (? 3 q r * + scurrency1 (
~ 5 B ^ u (5,
L M 3 # z 1 f % ( ( ] ¨ § E q X,T (f %
L M 3 z 1 f % ( q 3 k X,PAf %
L M 3 { ) ( FO?( M,f % ` L M 3 FG H I?,f % ( e m Z?
L M 3 { ) ( 3 FO?( M,f % 3 F
,f % (? / 3,] ¨ e ) (
< < Ac F u X,,f %
! L M 3 { ) ( f % ˇ,f % 3 6 ( p
D ' (X
"? 3,L M f % 3 e I– ( I– (′ % ( ( IN O f % ( F ID?! (b b′ u4 L M ˙ A] ¨ 3 0 (I– F
L M ] ¨ i X ′ (I– E Q ~ 5 e
# L I–? + * "
! *? (Y,Q… < < A X X N? x f %,`
(Y e,* (3,S T Q < <A X N? x f % b 3,
( S TQ < <? x f % X,3 currency1
a13 a17 a18 a16F
(3? * G c,3 m % ( \?
/ I(G H I ˇ?
(G H I ˇ –?,V,D E? 8 9
a13 a14 a19 a16m,,,(
k g [ N ) I (? G c? T ;? & 0? ˇ Z T c u DE %? ( % ˙ Af c 1 3 5? ˉ? P
k g 3 p % (r Y Z ’ (,” B 1 o ( ’ ) X,P
g Z k ( 2 3 3 u? (? ~? E? 4 3 4
shhgs@wgqqh.com
a0 42
a1 a2 42 a1
a13 a14a9a17 a16 b E 3 ¥K (
( Y Z E ^
& J N (fl (,Y + ! fl
(\,? ˉ? % g £((? v w ( fl
(
a13 a14 a14 a16
% F"?T - - ˇ3 * '? y?
8 - ( ( 4 u V o( ˇ
! ! " 4W 9 /,* " 44
a13 a14 a20 a16 – (
%,! ! F3 @ G w (5 6 7 J? (
a13 a14 a21 a16>
m G H I(f %? A” F¥K t G H I( | u ¥A,f % (
( & ’ ( m ‰ [ E g
k?
a13 a14 a15 a16
8 %? (,£ (4 | f % " # #