shhgs@wgqqh.com
a0 1
a1 a2 48 a1
2002 7
chinapub ! " # $ % ! & ’ ( ) *+,-,&/ 0 (1 2
3 45 (
6 7,8 9 3,) ; < =,> 8 (? @ A B 3 C D E
Bruce EckelFG (H I J K ( L ( M N O PA ! 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 c p 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 A F? ·? fl,? … 3C ¥ ‰ Q $?,
` m
Bruce Eckel? (′?,?,1000V W? ˉ? ˙? = G ¨ XD E? m3 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 48 a1
}m a (0 Y? v w m; a A ( F ^
*1 N O
˙? Q ! "# ! Q 1 N O
$,%,¥ A ( g & ’ X ^
[ 1,3 ( ) *? ˉ? 3? mC U Y Z / + < <,- (V,/
0 A?1 2 3,' 4 (5 6 7 8 9,; E < = >? @ 4 (A ( % ˙? B C r D E F ;
D (G H I
% 3 ge ",J 0 3 K (L "( M
(N X O ˉ > ( P Q R
E < - S ( § A? c > 1 ) 1 "
,J 0 3 L ‰ T ) 1 "(L T U V 1
W < g( Q 1 "( 1 X,V 1? v w Y Z T D J 0 (L [ E ( A Y mV 1 ( 3
i j D m(? ; 1 )
[ \ [ \ E <[
\ ] ‰ A W E 1 ) ^ ¥ _ ‘ [ ¥ _? 3 ˙,a b (c d
‰ e[ f g [ \ [ \
3 h i · 2 j V ( M (k / l,N O w? @
h i · (L m3 m n o @ l (p q r s § *b ) t(3 u?/ Q R Pˇ % W,P§ o (k / lv,D E? v w 3 SQ w x (y w @ (l ]? 0
a = x + y - 2/2 + z;
3 h i · J e z 3 Su,m{ | X (? } 0
a = x + (y - 2)/(2 + z);
shhgs@wgqqh.com
a0 3
a1 a2 48 a1
~ L (
A (? }? ! - S 1 (L? U
| g- S( 1? U ˇ E ^? ; J 0 L (h i ·
s ; 3 w x( m (
[ $ % m3 ¢ 4Q L
8 E? ~? 0
a = 4;
X ; L ~ [ $? X ;
(? X; $
~ L 8 ) t ( ‘ ) g m(FF(L X y ¥ _ (
~ L ( M? R 3 r g 3 r?
ˉ? $
Y Z ( R,T g? ˉ Q?,
Y Z | X,g ( ¥ ) % Q $ F ′ V " G W D % (
¥ _ ~ L ( M mD X,%? | ¥ _?
^ Q ( 1 D EF¥ _ u 4ˇ( ~ L ( M? J? F A (
[ $? ˉ? ¥_? $
O ˉ,? y / m y,(Y ¥_,? (],0
//,c03:Assignment.java
// Assignment with objects is a bit tricky,
import com.bruceeckel.simpletest.*;
class Number {
int i;
}
public class Assignment {
static Test monitor = new Test();
public static void main(String[] args) {
Number n1 = new Number();
Number n2 = new Number();
n1.i = 9;
n2.i = 47;
System.out.println("1,n1.i," + n1.i +
",n2.i," + n2.i);
n1 = n2;
System.out.println("2,n1.i," + n1.i +
",n2.i," + n2.i);
n1.i = 27;
shhgs@wgqqh.com
a0 4
a1 a2 48 a1
System.out.println("3,n1.i," + n1.i +
",n2.i," + n2.i);
monitor.expect(new String[] {
"1,n1.i,9,n2.i,47",
"2,n1.i,47,n2.i,47",
"3,n1.i,27,n2.i,27"
});
}
} ///:~
~ / ′? 3 K ( 3 0
import com.bruceeckel.simpletest.*;
A t,)? ¢ £ (?
ˇe? W,g
` ¥? ' £ e? F e( m Z 3 0
static Test monitor = new Test();
A,§ currency1 3
(( e(?]
(3 e F (b?,< g0
monitor.expect(new String[] {
"1,n1.i,9,n2.i,47",
"2,n1.i,47,n2.i,47",
"3,n1.i,27,n2.i,27"
});
"z h ( (? @ ' ! ( M X,P' ! ],! Q,? A "z 1 8 E X x )?”? ˉ g,
Y A fi O,N ' ! (
; fl £?< g x (' !,
e ) t § currency1,e e(?] !
" A W ( T ~ J,X (L | P " ~? ! `
! F V ( E? | ! " –,? e (¥ _ F
‘ ) ¥ ( ~ L
E < ! ¥ !(,g " · !
"? e y 3 ¥ _ /Y F
! ( y ( L (¥ _ F~ L ^
shhgs@wgqqh.com
a0 5
a1 a2 48 a1
* T,? J T,A D y (Y ¥ _,T,? … ‰? ¢?
_? T U
v ¥ _ (G? ˉ? | `
v w Z 1 ′ E ( 3? ¥? /("# % ( ~ L 0
n1.i = n2.i;
ˉ? 3 ¥ _ `
! " g 3 ¥ _ > 8? ; ˙e > ¨ (¥ _,X ^? C,¥ _ ("# ( m
@ ( s 4 d X 3 e ; '( D E W PA k g,5
!? W,FY ˇ ( F @ 4? % ¥ _ ~ L ;,? (f — ′ >,
P¥ _? ( M [,m
(c d 0
//,c03:PassObject.java
// Passing objects to methods may not be what
// you're used to,
import com.bruceeckel.simpletest.*;
class Letter {
char c;
}
public class PassObject {
static Test monitor = new Test();
static void f(Letter y) {
y.c = 'z';
}
public static void main(String[] args) {
Letter x = new Letter();
x.c = 'a';
System.out.println("1,x.c," + x.c);
f(x);
System.out.println("2,x.c," + x.c);
monitor.expect(new String[] {
"1,x.c,a",
"2,x.c,z"
});
}
} ///:~
F V
#,FA (1?¥ "$ %1 3 (
D E?
shhgs@wgqqh.com
a0 6
a1 a2 48 a1
y.c = 'z';
J,
# ‰? (¥ _ (' ! E,3C
! < § £ N > (4 d | W % g,5 !,1c? ' £? F m c d?,? g3? ˇ
F ′ V " G ("F? > (0
P
R Q A,…? "> Ru (o "? ">R,P O ˉ( "C? c {? X 1? t
[ m E ( @ ~ L ()? S A 3 - SQ h F
* h ; 3 a r ) D m( % m?,
8 % " | ` ~ 0
],"F ( 0
//,c03:MathOps.java
// Demonstrates the mathematical operators,
import com.bruceeckel.simpletest.*;
import java.util.*;
public class MathOps {
static Test monitor = new Test();
// Shorthand to print a string and an int,
static void printInt(String s,int i) {
System.out.println(s + " = " + i);
}
// Shorthand to print a string and a float,
static void printFloat(String s,float f) {
System.out.println(s + " = " + f);
}
public static void main(String[] args) {
// Create a random number generator,
// seeds with current time by default,
Random rand = new Random();
int i,j,k;
// Choose value from 1 to 100,
j = rand.nextInt(100) + 1;
k = rand.nextInt(100) + 1;
printInt("j",j); printInt("k",k);
i = j + k; printInt("j + k",i);
i = j - k; printInt("j - k",i);
i = k / j; printInt("k / j",i);
i = k * j; printInt("k * j",i);
i = k % j; printInt("k % j",i);
j %= k; printInt("j %= k",j);
shhgs@wgqqh.com
a0 7
a1 a2 48 a1
// Floating-point number tests,
float u,v,w; // applies to doubles,too
v = rand.nextFloat();
w = rand.nextFloat();
printFloat("v",v); printFloat("w",w);
u = v + w; printFloat("v + w",u);
u = v - w; printFloat("v - w",u);
u = v * w; printFloat("v * w",u);
u = v / w; printFloat("v / w",u);
// the following also works for
// char,byte,short,int,long,
// and double,
u += v; printFloat("u += v",u);
u -= v; printFloat("u -= v",u);
u *= v; printFloat("u *= v",u);
u /= v; printFloat("u /= v",u);
monitor.expect(new String[] {
"%% j = -?\\d+",
"%% k = -?\\d+",
"%% j \\+ k = -?\\d+",
"%% j - k = -?\\d+",
"%% k / j = -?\\d+",
"%% k \\* j = -?\\d+",
"%% k % j = -?\\d+",
"%% j %= k = -?\\d+",
"%% v = -?\\d+\\.\\d+(E-?\\d)?",
"%% w = -?\\d+\\.\\d+(E-?\\d)?",
"%% v \\+ w = -?\\d+\\.\\d+(E-?\\d)",
"%% v - w = -?\\d+\\.\\d+(E-?\\d)",
"%% v \\* w = -?\\d+\\.\\d+(E-?\\d)",
"%% v / w = -?\\d+\\.\\d+(E-?\\d)",
"%% u \\+= v = -?\\d+\\.\\d+(E-?\\d)",
"%% u -= v = -?\\d+\\.\\d+(E-?\\d)",
"%% u \\*= v = -?\\d+\\.\\d+(E-?\\d)",
"%% u /= v = -?\\d+\\.\\d+(E-?\\d)"
});
}
} ///:~
~ / < g( e O (],0
&,/ ],3
| ` 3 ’,/ ],3
` ],3 # / § currency1 3
( ¥ _ E J 0 " ‘ ) § currency1 ^ * m "
,( 41 "0 (
( ¥ _ ( & ’ 0,V X e( "
[ E $ )
, "0 J 0 (" o ! F 1 "P3 ( u
(
shhgs@wgqqh.com
a0 8
a1 a2 48 a1
‘ ) g, " ' ! (O ˉ”3 currency1 3 currency1 X 3? D E
X ; ` }^ + Y? 3 X r #?,' ! Z,%£ N c d
g3 # $ " D t( T U s h i · (K (p
F% % & ’ u e( p,
B W % g (?,c? 3 8 ′ (y ˉ?X 1 (? < +,A? % ; =
,% | c?,£ R? #? (
* +,,,- s h i · 3 – ( E $ 0
ˉ?m Y Z A F? (>? ˇ8? % h 3 ; m[ ; m(? S? E F? S? 3 c S }? 0
-?
h? " h 3 V " F s h i · * "
[,\h ( F ( ` 3 ; PA? ˇ 3 0[
.,\ % F s h i ·?h?3 V (h i · ˇ? [
\ D E % h ; m PS 1,3 " ˇ 0
-?\\d+
J? Y (
( 3
( * a ([/ / \? 3?,(
X ) s h i · D ( Y A h Q ( 3 s h i · D E
(? < X gM ( s h i · ( § A % A X ) s h i · p m(? % c?
D E 3 0
%% j = -?\\d+
[
) \”T x? F L [) * \ ([ \ ( [
+\3? F s h i ·?A [ 3 p q 1 u i? v w ; < =,? ˉ? (
g,s h i · (K p ( W,` 1 £
shhgs@wgqqh.com
a0 9
a1 a2 48 a1
t (PS
S PS S c >?,^ (hi · Q n o gh t ( ( ]? h i ·
x = -a;
(? } w ‰? [ E ! 0
x = a * -b;
£ (“,D E 0
x = a * (-b);
t PSh? " mt PS mt ( S X 9 ^ 1
3 g? )? )? E ˙? 9 Q ) t – ) Q Z? Y <
P
[ T U 1?,ˇ? P
,ˇ e 8 ()? P(
A (? }?P 3 t ˇ A (?}? 3 t ˇ 8 $
3 Y Z? J
! - ( PX (L,P L …?
e me [? g(
-
-? ˇ(? } ! F h i · (
ˇ(? }
! F h i · ( P
ˇ y ! F h i · ( P
ˇ(? } ! F h i · (? ¥ ) P
[ 1 ^,/@ ` … ¥ ) P
[ 1? / … ` @? ] 0
//,c03:AutoInc.java
// Demonstrates the ++ and -- operators,
import com.bruceeckel.simpletest.*;
shhgs@wgqqh.com
a0 10
a1 a2 48 a1
public class AutoInc {
static Test monitor = new Test();
public static void main(String[] args) {
int i = 1;
System.out.println("i," + i);
System.out.println("++i," + ++i); // Pre-
increment
System.out.println("i++," + i++); // Post-
increment
System.out.println("i," + i);
System.out.println("--i," + --i); // Pre-
decrement
System.out.println("i--," + i--); // Post-
decrement
System.out.println("i," + i);
monitor.expect(new String[] {
"i,1",
"++i,2",
"i++,2",
"i,3",
"--i,2",
"i--,2",
"i,1"
});
}
} ///:~
E < g ( M? D g(? ^ @ (L
(? D g( ( ^ ¢ (L 3 3 R,~ L u ‰
m§ A,( 1 [ $ X % A Q ( @ % A Q 1 "
[
( u 3 A (8 ( 3? ˇ F @ (
! *,& / (§ currency1 u 3 $?
,,ˇ ( P G (? } 3 + R,*V o( ( A 3 ) t (?,(( "?,< g V r? ) t,
X g?8 ) t V ˇ(r?
#,J 0
e$(O ˉ A W,n o 1 "(L (′ #? ˉ # % s # h i · …
ˉ # s …
# # m ) 0 ′ ) 1 ) - ) 0 ′ ) - )
1 > - E < X - 2 D m( "#e$? ; 8 > -
3 X - 3 ¥ e$ R,> - X - (u ‰ § A 8? (
shhgs@wgqqh.com
a0 11
a1 a2 48 a1
# ; ) D m¥ _ A W (? },PK &W? ’ (? 3 ] 0
//,c03:Equivalence.java
import com.bruceeckel.simpletest.*;
public class Equivalence {
static Test monitor = new Test();
public static void main(String[] args) {
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1 == n2);
System.out.println(n1 != n2);
monitor.expect(new String[] {
"false",
"true"
});
}
} ///:~
h i ·
%,,! ",P8 O ˉ(
L ],! Q | ‘ ) e & ¥ _ c > ( ' ! O ˉv w /
# | | ¥ _ (? R ˇ > A W (
X 8 ( ¥ _ (
D E? (' ! v w / # ¥ K & W |,) 3 * (
% | 8 e ¥ _ (? R w Z + ′?,e? m
( ( 0 m
w? 0
//,c03:EqualsMethod.java
import com.bruceeckel.simpletest.*;
public class EqualsMethod {
static Test monitor = new Test();
public static void main(String[] args) {
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1.equals(n2));
monitor.expect(new String[] {
"true"
});
}
} ///:~
F(O ˉ - *(
,,Y Z ) t? ˉ? § currency1,(e 8 $? 0
//,c03:EqualsMethod2.java
shhgs@wgqqh.com
a0 12
a1 a2 48 a1
import com.bruceeckel.simpletest.*;
class Value {
int i;
}
public class EqualsMethod2 {
static Test monitor = new Test();
public static void main(String[] args) {
Value v1 = new Value();
Value v2 = new Value();
v1.i = v2.i = 100;
System.out.println(v1.equals(v2));
monitor.expect(new String[] {
"false"
});
}
} ///:~
/,3 0 … Q,0O ˉ
# 0 (1 8
D E Rfl? FK e
0 2 s? 3? @ ( ˉ 4 5 W % g 4?,' g % g
,'? x r O 0 F @ 4 ˙?
0? ;? X 6 4 ′ V "
e? (e,0 D E A W,8 ¥ _ ( R X
7 8
5 5 6 6 fl 2,9 # "(7 8 # J 0 3
# ( L? # 7 8 (] 0
//,c03:Bool.java
// Relational and logical operators,
import com.bruceeckel.simpletest.*;
import java.util.*;
public class Bool {
static Test monitor = new Test();
public static void main(String[] args) {
Random rand = new Random();
int i = rand.nextInt(100);
int j = rand.nextInt(100);
System.out.println("i = " + i);
System.out.println("j = " + j);
System.out.println("i > j is " + (i > j));
System.out.println("i < j is " + (i < j));
System.out.println("i >= j is " + (i >= j));
System.out.println("i <= j is " + (i <= j));
System.out.println("i == j is " + (i == j));
System.out.println("i != j is " + (i != j));
shhgs@wgqqh.com
a0 13
a1 a2 48 a1
// Treating an int as a boolean is not legal
Java,
//! System.out.println("i && j is " + (i && j));
//! System.out.println("i || j is " + (i || j));
//! System.out.println("!i is " + !i);
System.out.println("(i < 10) && (j < 10) is "
+ ((i < 10) && (j < 10)) );
System.out.println("(i < 10) || (j < 10) is "
+ ((i < 10) || (j < 10)) );
monitor.expect(new String[] {
"%% i = -?\\d+",
"%% j = -?\\d+",
"%% i > j is (true|false)",
"%% i < j is (true|false)",
"%% i >= j is (true|false)",
"%% i <= j is (true|false)",
"%% i == j is (true|false)",
"%% i != j is (true|false)",
"%% \\(i < 10\\) && \\(j < 10\\) is
(true|false)",
"%% \\(i < 10\\) \\|\\| \\(j < 10\\) is
(true|false)"
});
}
} ///:~
F
( s h i ·? 3 S Q h i · z ( [
1\h 7 8? ˇ D E 0
(true|false)
h ( ; [
\ [# \ ‘ ) m F sh i · mp q? D E % ˙ A W F s h i · *h § Q ( [
..\¥ A W ( 7 8?
!7 ˇ? ˇ fl?7 8 ˇ ; ) L? X; }
Y? F7 8 h i · Pfl (L 1
L ¢ [, g,6 ; (< ¢? T Y S?,b h i ·,/ # ( 8 ` 7 8 @ 8 O ˉ(L
%? ˉP
L 1 Q ( A,T = > v (
FJ E P
= R u ‰ (^ "# Y Z > C "(8? @? ( e "u 4A B mh b L (Y 3 mB [,?X - ˇ( 3 " % m3 X
C Y A X C
shhgs@wgqqh.com
a0 14
a1 a2 48 a1
¢ 7 8 ( M,D g3 E?F
’ ˇ( _ [ $ % ; x r? h i · %? G 1 ! no? m ;,! @ 7 8 h i · ( H ; 1 ! n o
(? 3 F (] 0
//,c03:ShortCircuit.java
// Demonstrates short-circuiting behavior,
// with logical operators,
import com.bruceeckel.simpletest.*;
public class ShortCircuit {
static Test monitor = new Test();
static boolean test1(int val) {
System.out.println("test1(" + val + ")");
System.out.println("result," + (val < 1));
return val < 1;
}
static boolean test2(int val) {
System.out.println("test2(" + val + ")");
System.out.println("result," + (val < 2));
return val < 2;
}
static boolean test3(int val) {
System.out.println("test3(" + val + ")");
System.out.println("result," + (val < 3));
return val < 3;
}
public static void main(String[] args) {
if(test1(0) && test2(2) && test3(2))
System.out.println("expression is true");
else
System.out.println("expression is false");
monitor.expect(new String[] {
"test1(0)",
"result,true",
"test2(2)",
"result,false",
"expression is false"
});
}
} ///:~
,? ¢ "? ( 8 | …
‰ A,],3 E w T N,,? (h i · (? ¢ 0
if(test1(0) && test2(2) && test3(2));?,| | r L? ¢? % 3 I ' ! (O ˉJ h
w? fl? 3? ¢ …,D E h i · % (
shhgs@wgqqh.com
a0 15
a1 a2 48 a1
+? ¢ …,# $? h i · (O ˉfi O
# Y Z Z % n o + ′? Z 1 K L,? J F (f—? ˉ;? 3 7 8 n o ;,g M; ˙? N
e$("# e "(> ¥ v ( ( O P § O ˉ (O ˉ
^ Q ( m3 Q R (p C? E A + ! S T? %? S T (U,b
T 1 %V t W X? (? Y Z Q? @ ( D E ˙ 3 h [ \ ; m fl ( X ^? ′ V 6?,Q
ˉ' t? 3 s?
2 ˇ” … 3 2 s C ' t (e * % m3 3 s?
1 ˇ” … 3 me? C( M A … C? ˉ' t (e *m m3 3 s?]
3 ˇ” … 3 fl 4 [ T U
3 t A % 3 " § A (?
fl,¥ ' t C 3 3 C 7 8 > ( S D E W u ¢ ‘ Q ^ _ Y
‘ 0 8? ˇ D E 3 E
a 9 Q E h ( @ ~ L 02 1
3? b ( – ) 4 A c | 3 t [ X %? F3 9,
5 e$”T 1 m3 (L D E A V 6 mC X? E ( E < ] X ; ( fl;,d + 7 8fl > e f
¥ ) 7 8 (F\ ; J > ( A mF ‰
(?m3 7 8 D m(]
L X ; g – ) Z g W h J '
g [ 3 A ; )? "$(
"# g,P
6 6 1 Y 1 " g A 1 Y "D h ( " Q Ci j
S( g,P 7 7 1 Y 1 " g A 1 Y "D h ( " S( g
7 7?9 #? SQ k "
ˇ(r s 0? ˉ L ( s l 3 m i C? ˉ L? ( s l c i 3
[ mX? S( g
7 7 7 A (?3 m C Q k " ˇ(r s 0X n? S l? i C 3
m(
shhgs@wgqqh.com
a0 16
a1 a2 48 a1
ˉ¥ 8 % 8 (,g A W,T / =
(O ˉ[,( 1 Y " mQ m,d + g ( "{ ^
( "? ˉ 1 "
Y Z O ˉ[ 1 Y "(Q o m? g ( " X,{ ^
( " g E - Sa 9 Q
6 6 7 7 7 7 g,
u,T ` ( X S( g - Sa ( M,m3 c d? ˉ 1 "
% 8 ( O ˉ X ¥,A W,T / M g
` ( g K ~ L? / ( L T { F D E g Q g + O ˉ?
!?,ˉ0
//,c03:URShift.java
// Test of unsigned right shift,
import com.bruceeckel.simpletest.*;
public class URShift {
static Test monitor = new Test();
public static void main(String[] args) {
int i = -1;
System.out.println(i >>>= 10);
long l = -1;
System.out.println(l >>>= 10);
short s = -1;
System.out.println(s >>>= 10);
byte b = -1;
System.out.println(b >>>= 10);
b = -1;
System.out.println(b>>>10);
monitor.expect(new String[] {
"4194303",
"18014398509481983",
"-1",
"-1",
"4194303"
});
}
} ///:~
b 3 currency1 g mP O ˉ~?
],! Q,D E A x (
],D m 1 > ( ( 0
//,c03:BitManipulation.java
// Using the bitwise operators,
import com.bruceeckel.simpletest.*;
import java.util.*;
public class BitManipulation {
static Test monitor = new Test();
shhgs@wgqqh.com
a0 17
a1 a2 48 a1
public static void main(String[] args) {
Random rand = new Random();
int i = rand.nextInt();
int j = rand.nextInt();
printBinaryInt("-1",-1);
printBinaryInt("+1",+1);
int maxpos = 2147483647;
printBinaryInt("maxpos",maxpos);
int maxneg = -2147483648;
printBinaryInt("maxneg",maxneg);
printBinaryInt("i",i);
printBinaryInt("~i",~i);
printBinaryInt("-i",-i);
printBinaryInt("j",j);
printBinaryInt("i & j",i & j);
printBinaryInt("i | j",i | j);
printBinaryInt("i ^ j",i ^ j);
printBinaryInt("i << 5",i << 5);
printBinaryInt("i >> 5",i >> 5);
printBinaryInt("(~i) >> 5",(~i) >> 5);
printBinaryInt("i >>> 5",i >>> 5);
printBinaryInt("(~i) >>> 5",(~i) >>> 5);
long l = rand.nextLong();
long m = rand.nextLong();
printBinaryLong("-1L",-1L);
printBinaryLong("+1L",+1L);
long ll = 9223372036854775807L;
printBinaryLong("maxpos",ll);
long lln = -9223372036854775808L;
printBinaryLong("maxneg",lln);
printBinaryLong("l",l);
printBinaryLong("~l",~l);
printBinaryLong("-l",-l);
printBinaryLong("m",m);
printBinaryLong("l & m",l & m);
printBinaryLong("l | m",l | m);
printBinaryLong("l ^ m",l ^ m);
printBinaryLong("l << 5",l << 5);
printBinaryLong("l >> 5",l >> 5);
printBinaryLong("(~l) >> 5",(~l) >> 5);
printBinaryLong("l >>> 5",l >>> 5);
printBinaryLong("(~l) >>> 5",(~l) >>> 5);
monitor.expect("BitManipulation.out");
}
static void printBinaryInt(String s,int i) {
System.out.println(
s + ",int," + i + ",binary,");
System.out.print(" ");
for(int j = 31; j >= 0; j--)
if(((1 << j) & i) != 0)
System.out.print("1");
else
System.out.print("0");
System.out.println();
}
static void printBinaryLong(String s,long l) {
System.out.println(
s + ",long," + l + ",binary,");
System.out.print(" ");
for(int i = 63; i >= 0; i--)
shhgs@wgqqh.com
a0 18
a1 a2 48 a1
if(((1L << i) & l) != 0)
System.out.print("1");
else
System.out.print("0");
System.out.println();
}
} ///:~
O p O (e
5 %&
5 %$ A W? T " | / ],3 $ w ( ` E ( ! ( · ],! " F? E X q A Z? (
,? g? (
%,,X
%,,X,= D E A ; ˙? i currency1 ' ! 3
(
3 T 1 " A,T? @ (Oˉ
§ * E m [ E m s h i · ¥ ) Y O ˉ q X r s tF?( T? T (k "?
$ ˇ A [ fl £ E
u mv w ( E ] T < < gh ' ! Z
R,,¥
( ˉ,
(b L b ′ L,(e ( ! h i x ·? l h S0
9h S h? S (' !? (0
-1,int,-1,binary,
11111111111111111111111111111111
+1,int,1,binary,
00000000000000000000000000000001
maxpos,int,2147483647,binary,
01111111111111111111111111111111
maxneg,int,-2147483648,binary,
10000000000000000000000000000000
i,int,59081716,binary,
00000011100001011000001111110100
~i,int,-59081717,binary,
11111100011110100111110000001011
-i,int,-59081716,binary,
11111100011110100111110000001100
j,int,198850956,binary,
00001011110110100011100110001100
i & j,int,58720644,binary,
00000011100000000000000110000100
i | j,int,199212028,binary,
00001011110111111011101111111100
i ^ j,int,140491384,binary,
00001000010111111011101001111000
i << 5,int,1890614912,binary,
01110000101100000111111010000000
i >> 5,int,1846303,binary,
shhgs@wgqqh.com
a0 19
a1 a2 48 a1
00000000000111000010110000011111
(~i) >> 5,int,-1846304,binary,
11111111111000111101001111100000
i >>> 5,int,1846303,binary,
00000000000111000010110000011111
(~i) >>> 5,int,132371424,binary,
00000111111000111101001111100000
e ( ! h i · T U
m y X A mL 1 " A x? 3 … L (
3 C 3,' (,X h i · (x ·? (0
boolean-exp? value0,value1
ˉ
n o s @ | L (O ˉ? ˉ
# s @ (L |? A (O ˉ
|? E M (
#?,' L ) z B
w ( E A 3 ) z ( {
t L 1 m l | (? } F}? ( M % A m ;,~ (
c E A (V 1 [ E % g3 L
3 Q $ L m mL A,> (r? 3 ] 0
static int ternary(int i) {
return i < 10? i * 100,i * 10;
}; < g £ % 8 X L (,,V 0
static int alternative(int i) {
if (i < 10)
return i * 100;
else
return i * 10;
}
shhgs@wgqqh.com
a0 20
a1 a2 48 a1
9 Q ) t [ V X,i D E L ( M 3 O % L } 3 Q $ Fe 3 ( 8 b ¢ (
F
SX T ) "( " 3 a ~ L
3 ( 3 3 P S (r,' (
#
F
S 3 mp q ( 0 ( A ; a | ; J ( m X 3?
( 8 | (? @
( M ′? ; u
v w 3 X (?,p A ˙ % ; i j D m(? J p (? } X ( % %? @ (e? ( M u E <
(§ A 3 o!,A ^ ),? ˉ
| u (,8
) t,V? @ W p,( % X ; }
( % Y? + u S
F J ( M,mw? ˉh i · E ( Y Z D m?,( 1 "?
Y Z?,P S3 9 Q ( =
( 0
int x = 0,y = 1,z = 2;
String sString = "x,y,z ";
System.out.println(sString + x + y + z);
,P % 9? = A W ( x · X / PA W 9 Q ` =? ˉ 0
System.out.println(x + sString);
Y Z
,P = ! " # ! " # ! " # ! " #
b? ( X ; c fi O h i ·,? P3 S?
, [ 3?
m3 ! | l ( 0
shhgs@wgqqh.com
a0 21
a1 a2 48 a1
while(x = y) {
//,..,
}
w % | 1 > - (? ¢
X ~ L F %
%X - ) C ~ L O ˉ A ;,x 3 o F
F- 3 L h i · (O ˉX
X,P =
( M A,D E,F ( M T ! Q
[ X,0 e 3 3 X,g? ( x
% % 3 b (h i · FJ £ * m ;
m3 e (c d Y (? ˇ? ˇ 7 8 (? ˇ? ˇ m3
2 1 7 8 % me
22 11 3? 3? e ( R S e,
` 3 currency1 p ! AX,˙? r 3 e$ 1 3 e$Q
$ % & ’ $ % & ’ $ % & ’ $ % & ’
(? }?P g?ˇ % b r,( 3 "#e$( = 3 "#e$ 8 $ P? "L ~?> C ( M?,
= # D g3 X,( = ( ( M? [ E w x r # A ( = !( e$ =
% ( =? P"#(A e$
2 3 D m( k ( 1 (3 S ] 0
void casts() {
int i = 200;
long l = (long)i;
long l2 = (long)200;
}
D < g(? c E ¥ "L [ E ¥ ( = F
( e =? V o ( 3 m %?,M
X ^,V o ( = u £,? Fm,% ˙ £ ;? ^ = ; (,
F
e$ = 3 T ˙,a ( (e$ =s c ( D gD (
( & [ $? 3 ; V? ("#e$ = 3 X,Z V
shhgs@wgqqh.com
a0 22
a1 a2 48 a1
("#e$ A,J 0 3 ],!? w x r ( =? J A (¢ £ Z T ;? ¥? ˉ? 3 O % T Y Z w x r # ˇ ¥ )
X ( · =,K e$ E 8? e$ V (? D E Z? ˉ X,
§,? ¥ R u ‰ (§ A e$("#( ^?( =
s 9 X ; = e X §,= ( % ˙ A W >¨ = % mp q (
3 p ] ‰?,' D m¥ _? E FA (e#
! ! J
,; currency1 ' E T g u [ E A X ;? 3 e#(e$ 8?
( ; ! ! ! !
, L ( M? ; x r Z e$
( Fm e$? e ( D g ( M? ; t? L > ( Q? ‰? E Q fi,0
//,c03:Literals.java
public class Literals {
char c = 0xffff; // max char hex value
byte b = 0x7f; // max byte hex value
short s = 0x7fff; // max short hex value
int i1 = 0x2f; // Hexadecimal (lowercase)
int i2 = 0X2F; // Hexadecimal (uppercase)
int i3 = 0177; // Octal (leading zero)
// Hex and Oct also work with long,
long n1 = 200L; // long suffix
long n2 = 200l; // long suffix (but can be
confusing)
long n3 = 200;
//! long l6(200); // not allowed
float f1 = 1;
float f2 = 1F; // float suffix
float f3 = 1f; // float suffix
float f4 = 1e-45f; // 10 to the power
float f5 = 1e+9f; // float suffix
double d1 = 1d; // double suffix
double d2 = 1D; // double suffix
double d3 = 47e47d; // 10 to the power
} ///:~
h
,( ! "( M % < <= ] a? J < > ′ E (
# D m(? "e$? ; E,( ! (x · E ¢ %? 3 8 e$(b ′ L ′ ("Q f * (
X " E A x · h (
,3? 3 F? (? W!,
8 % E < 8 D ; h (b ′ (L?
shhgs@wgqqh.com
a0 23
a1 a2 48 a1
ˉ{ !,?,r L = | #? ~ L M (
)? ^,;( ! " ‘ 9?,3 9,4(" z ( me ( !? L (h
F? L? (Y " w,A (e$ ′ (
$h
′ (’h # ′ ()h
' gy "( M fl m – F y · [
\h | ¥ "(h " ′
($ 4 ; (? 8,@,?! 3 x(
L } $ < - a3
a4 a5? (y "h x ·? J
$ < - ($ 4 ;a3
a4 a5? @
= 8 !7 ( M? @ W Q h W? (?
9(? ˇ (N O = 8 !7 y @? @ (? @,% F t e ( M v w? ¥
a6 a7 a5 a8 X Z $,g,
E < F(
D E? ˉ ; ) P 1 | ¥ "(h "( Y Z <g
(!,A> B # 3? …,? J A h
$ < - 9a3
a4 a5
‰ 3? ˉ? ; x O "#(e$ Y? X ` J?
,8? 0
long n3 = 200;
m” e (r D E
(99? (> V o (? ˉ
float f4 = 1e-47f; // 10 to the power
(,,P y "h ("
D E? ˉ m? ( ( A, 3? #
= # " #" #" #" #
,? ˉ ¥ 8
("# [ 8 % 8 $("#
( "F ( L,/ T M g $` ( D E (O ˉ |
D E? P L ~?Y (e$? ( e$ =
‘ ) PL ~?3 (e$,ˉ 6?
fl u h i · b ′ (Y "#(e$”
shhgs@wgqqh.com
a0 24
a1 a2 48 a1
N O h i · (O ˉ("#e$? ˉ? 3 # 3 (` g(O ˉ[
ˉ? 3 3 ( Oˉ[
( )( )( )( )
( 9 # mp q (0A,#? "#% ′ V 6 (
% m 9 # (b % (,g? > ("#e$FX (? J ;,X 3? D E % F( "#′ > ( 1 u /,£ 3 e$mV ′ 8? 3 £ @
<(? " 3 £,% E F? Y £ ′ (" ;? [? g,¥
( % Q $ g? 3 T a (
mg? (c d X % 9 # D m"#e$FD m(? J? > (? X R (g? q A? T (,
* + * +* +* +
3 currency1 4 J m F 0 ˉ g F? ˙ (k / R mV U Y ( M X
} ¨ r?!,3 _ Y ! & > * ! $ ˇ
,%,
t? &
! – ! ’ E < g / 6 6 7 7
& # 7 6 7 6
> * 7 8 > E < 22 11 2 1 3
u T L ( 7 5 C = D E
! > ~ L ! E < } x · (~ L |? ˉPg J ( X 3 (_ Y?,
ˉX? ( A X,-,-,-,-
,
"#? E A ( G J X e$("# 3 ] ‘ ) mc d (£?
,? T X,mc d (
//,c03:AllOps.java
// Tests all the operators on all the primitive data
types
shhgs@wgqqh.com
a0 25
a1 a2 48 a1
// to show which ones are accepted by the Java
compiler,
public class AllOps {
// To accept the results of a boolean test,
void f(boolean b) {}
void boolTest(boolean x,boolean y) {
// Arithmetic operators,
//! x = x * y;
//! x = x / y;
//! x = x % y;
//! x = x + y;
//! x = x - y;
//! x++;
//! x--;
//! x = +y;
//! x = -y;
// Relational and logical,
//! f(x > y);
//! f(x >= y);
//! f(x < y);
//! f(x <= y);
f(x == y);
f(x != y);
f(!y);
x = x && y;
x = x || y;
// Bitwise operators,
//! x = ~y;
x = x & y;
x = x | y;
x = x ^ y;
//! x = x << 1;
//! x = x >> 1;
//! x = x >>> 1;
// Compound assignment,
//! x += y;
//! x -= y;
//! x *= y;
//! x /= y;
//! x %= y;
//! x <<= 1;
//! x >>= 1;
//! x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting,
//! char c = (char)x;
//! byte B = (byte)x;
//! short s = (short)x;
//! int i = (int)x;
//! long l = (long)x;
//! float f = (float)x;
//! double d = (double)x;
}
void charTest(char x,char y) {
// Arithmetic operators,
x = (char)(x * y);
x = (char)(x / y);
x = (char)(x % y);
shhgs@wgqqh.com
a0 26
a1 a2 48 a1
x = (char)(x + y);
x = (char)(x - y);
x++;
x--;
x = (char)+y;
x = (char)-y;
// Relational and logical,
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators,
x= (char)~y;
x = (char)(x & y);
x = (char)(x | y);
x = (char)(x ^ y);
x = (char)(x << 1);
x = (char)(x >> 1);
x = (char)(x >>> 1);
// Compound assignment,
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting,
//! boolean b = (boolean)x;
byte B = (byte)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
float f = (float)x;
double d = (double)x;
}
void byteTest(byte x,byte y) {
// Arithmetic operators,
x = (byte)(x* y);
x = (byte)(x / y);
x = (byte)(x % y);
x = (byte)(x + y);
x = (byte)(x - y);
x++;
x--;
x = (byte)+ y;
x = (byte)- y;
// Relational and logical,
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
shhgs@wgqqh.com
a0 27
a1 a2 48 a1
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators,
x = (byte)~y;
x = (byte)(x & y);
x = (byte)(x | y);
x = (byte)(x ^ y);
x = (byte)(x << 1);
x = (byte)(x >> 1);
x = (byte)(x >>> 1);
// Compound assignment,
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting,
//! boolean b = (boolean)x;
char c = (char)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
float f = (float)x;
double d = (double)x;
}
void shortTest(short x,short y) {
// Arithmetic operators,
x = (short)(x * y);
x = (short)(x / y);
x = (short)(x % y);
x = (short)(x + y);
x = (short)(x - y);
x++;
x--;
x = (short)+y;
x = (short)-y;
// Relational and logical,
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators,
x = (short)~y;
x = (short)(x & y);
x = (short)(x | y);
x = (short)(x ^ y);
x = (short)(x << 1);
shhgs@wgqqh.com
a0 28
a1 a2 48 a1
x = (short)(x >> 1);
x = (short)(x >>> 1);
// Compound assignment,
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting,
//! boolean b = (boolean)x;
char c = (char)x;
byte B = (byte)x;
int i = (int)x;
long l = (long)x;
float f = (float)x;
double d = (double)x;
}
void intTest(int x,int y) {
// Arithmetic operators,
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
x = -y;
// Relational and logical,
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators,
x = ~y;
x = x & y;
x = x | y;
x = x ^ y;
x = x << 1;
x = x >> 1;
x = x >>> 1;
// Compound assignment,
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
shhgs@wgqqh.com
a0 29
a1 a2 48 a1
x &= y;
x ^= y;
x |= y;
// Casting,
//! boolean b = (boolean)x;
char c = (char)x;
byte B = (byte)x;
short s = (short)x;
long l = (long)x;
float f = (float)x;
double d = (double)x;
}
void longTest(long x,long y) {
// Arithmetic operators,
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
x = -y;
// Relational and logical,
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators,
x = ~y;
x = x & y;
x = x | y;
x = x ^ y;
x = x << 1;
x = x >> 1;
x = x >>> 1;
// Compound assignment,
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting,
//! boolean b = (boolean)x;
char c = (char)x;
byte B = (byte)x;
short s = (short)x;
int i = (int)x;
float f = (float)x;
double d = (double)x;
shhgs@wgqqh.com
a0 30
a1 a2 48 a1
}
void floatTest(float x,float y) {
// Arithmetic operators,
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
x = -y;
// Relational and logical,
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators,
//! x = ~y;
//! x = x & y;
//! x = x | y;
//! x = x ^ y;
//! x = x << 1;
//! x = x >> 1;
//! x = x >>> 1;
// Compound assignment,
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
//! x <<= 1;
//! x >>= 1;
//! x >>>= 1;
//! x &= y;
//! x ^= y;
//! x |= y;
// Casting,
//! boolean b = (boolean)x;
char c = (char)x;
byte B = (byte)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
double d = (double)x;
}
void doubleTest(double x,double y) {
// Arithmetic operators,
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
shhgs@wgqqh.com
a0 31
a1 a2 48 a1
x = -y;
// Relational and logical,
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators,
//! x = ~y;
//! x = x & y;
//! x = x | y;
//! x = x ^ y;
//! x = x << 1;
//! x = x >> 1;
//! x = x >>> 1;
// Compound assignment,
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
//! x <<= 1;
//! x >>= 1;
//! x >>>= 1;
//! x &= y;
//! x ^= y;
//! x |= y;
// Casting,
//! boolean b = (boolean)x;
char c = (char)x;
byte B = (byte)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
float f = (float)x;
}
} ///:~
(o ! (? ; ~?A # e L ;? ¢ A (%? X ; P
L 9 Q ( § A Z 1
[ < g,( "F ( M
8 % E < 8?,/( M O ˉ[
ˉ| PA ~? / Y ( w x r ( e$ =
),ˉ 6? (
¥ )? ` 1 =,A?,X ^ [ ′?,? ˉ? Pe? ′ (
Q 9 Q O ˉ,!?,0
//,c03:Overflow.java
// Surprise! Java lets you overflow,
shhgs@wgqqh.com
a0 32
a1 a2 48 a1
import com.bruceeckel.simpletest.*;
public class Overflow {
static Test monitor = new Test();
public static void main(String[] args) {
int big = 0x7fffffff; // max int value
System.out.println("big = " + big);
int bigger = big * 4;
System.out.println("bigger = " + bigger);
monitor.expect(new String[] {
"big = 2147483647",
"bigger = -4"
});
}
} ///:~
[ #? ( M [? ! ]?
′ m ′ gY z
– 3?
8 % 8 F( e b ~ L
( M [,T M J X % ( e$ = 3? X 1 e$ = [,£ ) t; < g R,
u ‰ D m( e$? ; T = §A (
e$ m F( @ ( M? ¥ A ( ˉm? 2 s?,F = ^ *X X r ˉ,
c ˇ ( ! D E? ˉ? m ( Y Z? v w ¥?' ( —,′ V "^? m3 ! u 4[ m V (r
( m
# F8 F8 # E < F 8 (
m fl ( §? A,£ N c d (’
E } Y? F?g * * (o ! % 8 / (
V V
,
D m(u T? u T h i · D … (O ˉQ n o v w A u
] A 5 3 u T h i · h i ·,u T
% %% %% %% % Q n o (L 2 5(L > - h i ·,…
#? F < g^ (D m #? ; ) u T?
X,§,? P" 1 ( B §,Z T
fl C L h C h #? ˉ? % ¥ fl
shhgs@wgqqh.com
a0 33
a1 a2 48 a1
(L (? ¢ 8? #? ^ } #
<? (u T h i · / A = L
# [,b G ( ! ( D E #? J me 0
if(Boolean-expression)
statement
if(Boolean-expression)
statement
else
statement
u T n o
ˇ % ; J 0 3 O ˉ
c E E Sh O () t [ E 3S3 9 Q ( b % g?
ˇ h A c E ) t [ E b
3
(]? (,# (" ′) ) - ) " 0
//,c03:IfElse.java
import com.bruceeckel.simpletest.*;
public class IfElse {
static Test monitor = new Test();
static int test(int testval,int target) {
int result = 0;
if(testval > target)
result = +1;
else if(testval < target)
result = -1;
else
result = 0; // Match
return result;
}
public static void main(String[] args) {
System.out.println(test(10,5));
System.out.println(test(5,10));
System.out.println(test(5,5));
monitor.expect(new String[] {
"1",
"-1",
"0"
});
}
shhgs@wgqqh.com
a0 34
a1 a2 48 a1
} ///:~
¥ ! ( 3 O (T? ; ˙ R
S r < ! ( O
me 1 0~ / A,#? % … A L? ˉX
+ … $( § currency1 A,? … Y L W E X p Y
0
//,c03:IfElse2.java
import com.bruceeckel.simpletest.*;
public class IfElse2 {
static Test monitor = new Test();
static int test(int testval,int target) {
if(testval > target)
return +1;
else if(testval < target)
return -1;
else
return 0; // Match
}
public static void main(String[] args) {
System.out.println(test(10,5));
System.out.println(test(5,10));
System.out.println(test(5,5));
monitor.expect(new String[] {
"1",
"-1",
"0"
});
}
} ///:~
X `
,u O,/ 0 1 2/ 0 1 2/ 0 1 2/ 0 1 2
^
F8 F8 E < # Q ! ( A W T,U
,X o + g
5 F8 (
while(Boolean-expression)
statement
shhgs@wgqqh.com
a0 35
a1 a2 48 a1
,currency1 u? % n o | currency1
u % ` 1 n o 1 ) t (,X r 0 " gA? x u T 0
//,c03:WhileTest.java
// Demonstrates the while loop,
import com.bruceeckel.simpletest.*;
public class WhileTest {
static Test monitor = new Test();
public static void main(String[] args) {
double r = 0;
while(r < 0.99d) {
r = Math.random();
System.out.println(r);
monitor.expect(new String[] {
"%% \\d\\.\\d+E?-?\\d*"
},Test.AT_LEAST);
}
}
} ///:~
A
8 e? ( ( Q 0 9g u 4(
L 2 3 9 X 2 3 F8 (u T h i · (? }? % L 8
9$? 3 1 + ˇ,currency1 ( M A (' !? X (
,< g F
(? t (?,3
,G $@ Y mi ; § ( Y § *u 3 A h 3 v w – 6 3 currency1 | E § A
,? A (? } v w – 6 ; < g3
L ˇ
F8 (x ·
do
statement
while(Boolean-expression);
F8 F8 ( 3 3 currency1 n o (O ˉ
F8 ( [,– 6 3 currency1 FF8 *? ˉ 3 currency1 n o
Y Z ` [?,, ( M
F8,8 F8 V
shhgs@wgqqh.com
a0 36
a1 a2 48 a1
#,F 3 currency1 ( t u / T f * ` 1 u T n o,currency1 u % m3 ( N? ˇ
# ( 0
for(initialization; Boolean-expression; step)
statement
f * ( u T? ¢ E <
( N E,currency1 u A,? ¢
% 3 < gA # A,* ! +? (?,currency1 u A %
#? T )?@ "ˇ(^ 0
//,c03:ListCharacters.java
// Demonstrates "for" loop by listing
// all the lowercase ASCII letters,
import com.bruceeckel.simpletest.*;
public class ListCharacters {
static Test monitor = new Test();
public static void main(String[] args) {
for(int i = 0; i < 128; i++)
if(Character.isLowerCase((char)i))
System.out.println("value," + i +
" character," + (char)i);
monitor.expect(new String[] {
"value,97 character,a",
"value,98 character,b",
"value,99 character,c",
"value,100 character,d",
"value,101 character,e",
"value,102 character,f",
"value,103 character,g",
"value,104 character,h",
"value,105 character,i",
"value,106 character,j",
"value,107 character,k",
"value,108 character,l",
"value,109 character,m",
"value,110 character,n",
"value,111 character,o",
"value,112 character,p",
"value,113 character,q",
"value,114 character,r",
"value,115 character,s",
"value,116 character,t",
"value,117 character,u",
"value,118 character,v",
"value,119 character,w",
"value,120 character,x",
"value,121 character,y",
"value,122 character,z"
shhgs@wgqqh.com
a0 37
a1 a2 48 a1
});
}
} ///:~
F % gA (r F (? ! h i ·
- ˇ O ( X F a r Fh ( 3 S? O (
(1 m# Z ′ g,
* +,,H 8?@ ˇe A X,P
( 8 2 ( ¥ _,3 ((? g,
( $ F H Q? X?(
u e(,(^,% D m(? F3 ( a O § currency1 ( M,4 F
E F^? r w? ; | £ ( [ l,
E F
#?O V > e$(
for(int i = 0,j = 1; i < 10 && j != 11; i++,j++)
// body of for loop
F
#?O ( 2 3 * m# mF? ! h i· ˇ?O (; a F§ A? Z T
& & & &
F?(? \? g^
F3 r? S ˇ
X? S ˇ Q O "*( (
0F (? ! h i · ˇ? F? ! hi · ˇ(f *? ( N E? V S (,? l o currency1? £ g,p
` 3 ] 0
//,c03:CommaOperator.java
import com.bruceeckel.simpletest.*;
public class CommaOperator {
static Test monitor = new Test();
public static void main(String[] args) {
for(int i = 1,j = i + 10; i < 5;
i++,j = i * 2) {
System.out.println("i= " + i + " j= " + j);
}
monitor.expect(new String[] {
shhgs@wgqqh.com
a0 38
a1 a2 48 a1
"i= 1 j= 11",
"i= 2 j= 4",
"i= 3 j= 6",
"i= 4 j= 8"
});
}
} ///:~
E < gFf *? ( N ( ‰ f
* E O ^? " ( 3 e$(
,? [ E F (?; ! (;,( £ _ !,* ^ (£ *o currency1 ` ( t 3 @ (
,? F
# F8 * ;
0
//,c03:BreakAndContinue.java
// Demonstrates break and continue keywords,
import com.bruceeckel.simpletest.*;
public class BreakAndContinue {
static Test monitor = new Test();
public static void main(String[] args) {
for(int i = 0; i < 100; i++) {
if(i == 74) break; // Out of for loop
if(i % 9 != 0) continue; // Next iteration
System.out.println(i);
}
int i = 0;
// An "infinite loop",
while(true) {
i++;
int j = i * 27;
if(j == 1269) break; // Out of loop
if(i % 10 != 0) continue; // Top of loop
System.out.println(i);
}
monitor.expect(new String[] {
"0",
"9",
"18",
"27",
"36",
"45",
"54",
"63",
"72",
"10",
"20",
"30",
shhgs@wgqqh.com
a0 39
a1 a2 48 a1
"40"
});
}
} ///:~
F
#? [ gX,99 - g4"( M ; *+,3 Q $ mFX ; /? *o u T Z M 0 (,?; (1 % X ;?R
A ˙ K 3?,? ˉ;? R
P L ],! Q,D (? o ˇ ¢ n J ',(
+ A m ; *o ( ; ‰?,< g
,* ^ ( K 3? F? m;? R 9( M,m],‰ 9[ T ],! Q,
9B 9- ) 9 m3 x · ( o
#? ¥ F8 # 3 X ( D E A (c d
’ ( ) * ’ ( ) *’ ( ) *’ ( ) * +
,m,? m,
J F? £ ! (? x 0 ˉu T
! s * g? 2 s * gY?ˇ? J? b,0 ( £ D E? ˉ? ^ £ (?,< g?j,*
,J 0 A ( £ ˇ £ F
J X F %? S T J
s fl £ R J (*?A Q ~ (%? ˉ fl?* gY? Y X m Z + ; K? ¢ £ ˙ AX % Y Z?* ˇ′,
C ) * Y? — (?D
’ ˇ(n ( h % g,? B (r? Y 9 ¥
( ],3? – m,% PA N ! +
§?D g * u b c d X F)
F) X,
F3 p q (? J b ( !
B
( ˙? A PA 1 ( + m m,; e A x?; T ! 3 e *
shhgs@wgqqh.com
a0 40
a1 a2 48 a1
( ˉ X *? J 3 * ! ( u D E,PA W
> n A W,> (? ! 0 3? m S( I }? 0
label1,
F
3 ; k (r F (‰? k F u 4X ; m^ Z T ( 3 ¢ ‘?,V m V [?;,*o,u A,_ g
D F(r 0
label1,
outer-iteration {
inner-iteration {
//..,
break; // 1
//..,
continue; // 2
//..,
continue label1; // 3
//..,
break label1; // 4
}
}
F> ]
* ; *o,( _ g‰ > ] (*
,K g g (b F> ] <*
!”F*o ( *o ‰ ` 3 _ g
! | a currency1 ‰ ` > ] "*; ![,_ g ! A X,` ( t? J A _ ! e
# ] 0
//,c03:LabeledFor.java
// Java's "labeled for" loop,
import com.bruceeckel.simpletest.*;
public class LabeledFor {
static Test monitor = new Test();
public static void main(String[] args) {
int i = 0;
outer,// Can't have statements here
for(; true ;) { // infinite loop
inner,// Can't have statements here
for(; i < 10; i++) {
System.out.println("i = " + i);
shhgs@wgqqh.com
a0 41
a1 a2 48 a1
if(i == 2) {
System.out.println("continue");
continue;
}
if(i == 3) {
System.out.println("break");
i++; // Otherwise i never
// gets incremented,
break;
}
if(i == 7) {
System.out.println("continue outer");
i++; // Otherwise i never
// gets incremented,
continue outer;
}
if(i == 8) {
System.out.println("break outer");
break outer;
}
for(int k = 0; k < 5; k++) {
if(k == 3) {
System.out.println("continue inner");
continue inner;
}
}
}
}
// Can't break or continue to labels here
monitor.expect(new String[] {
"i = 0",
"continue inner",
"i = 1",
"continue inner",
"i = 2",
"continue",
"i = 3",
"break",
"i = 4",
"continue inner",
"i = 5",
"continue inner",
"i = 6",
"continue inner",
"i = 7",
"continue outer",
"i = 8",
"break outer"
});
}
} ///:~
3;,_ ! # X? 3? (# 1 X,0 ‘ ); * ^,h i · % F % %% %% %% % A( M (,1 F
B ( M (
shhgs@wgqqh.com
a0 42
a1 a2 48 a1
[ t,b l [ ( [ * ^,D E [ % (
ˉX;? 9 + _ g
(‰? ; ; _ ! _ ! b [ (
[ 3? |? ˉ_ ! ( % _ !? E
( (; F8 3 9 ( ˉ0
//,c03:LabeledWhile.java
// Java's "labeled while" loop,
import com.bruceeckel.simpletest.*;
public class LabeledWhile {
static Test monitor = new Test();
public static void main(String[] args) {
int i = 0;
outer,
while(true) {
System.out.println("Outer while loop");
while(true) {
i++;
System.out.println("i = " + i);
if(i == 1) {
System.out.println("continue");
continue;
}
if(i == 3) {
System.out.println("continue outer");
continue outer;
}
if(i == 5) {
System.out.println("break");
break;
}
if(i == 7) {
System.out.println("break outer");
break outer;
}
}
}
monitor.expect(new String[] {
"Outer while loop",
"i = 1",
"continue",
"i = 2",
"i = 3",
"continue outer",
"Outer while loop",
"i = 4",
"i = 5",
"break",
"Outer while loop",
"i = 6",
shhgs@wgqqh.com
a0 43
a1 a2 48 a1
"i = 7",
"break outer"
});
}
} ///:~
r s [ r )
F8 0
$ M (,_ g (b |
($ (,* g K ( t F? (
<$ ;,(?h ! + ˇ
4,( ;,‘ I ( (?h ! + ˇ Y,F; ˙? ( 3 ¢ ‘ FV m ( %; _ ! V [ F
) * Y ’ ˇ(n? G w x ¥ ( X
G (" j ("? q ( ‘ )
F ( - ’? t,( fl? U
J m c d A W (k m@? o ! ( X ; ) g ( ! ‰? m3 fl? mw ( _ W o !,(
p A | m,
m
F 8,T F 8,9 # "h i ·
- ˇ(L N O v w A £ A (x · 0
switch(integral-selector) {
case integral-value1,statement; break;
case integral-value2,statement; break;
case integral-value3,statement; break;
case integral-value4,statement; break;
case integral-value5,statement; break;
//,.,
default,statement;
}
" 3,J 0? "L (h i · F 8,L? (
N 1 8? ˉ? g,? ( A,>v (
) t ( b (? ˉ m? g? ( s
# (
shhgs@wgqqh.com
a0 44
a1 a2 48 a1
,FJ? Y?,(b? m ;? > y? u,* !
F 8 F 8 (3 x· X ^; (? ˉ m ;,3
(y? gA ; |? X,\ ; m( % ;,? b Y F
#? ( A m; g? Q X *?,_ g 3 r? ˉ? |,3? [ E P; k g # (b? 3 C ~ [ m
F 8 ; mu ¢ r? V V * 3
| A % 3 ; J 0 } 8? (? "L ( u T > C "1 u T Y Z
F 8 X ;? y1,¥ ) fl? "(e$? ; 3 a (
#? (,/ 0 3 | ` n o G W 6 6 0
//,c03:VowelsAndConsonants.java
// Demonstrates the switch statement,
import com.bruceeckel.simpletest.*;
public class VowelsAndConsonants {
static Test monitor = new Test();
public static void main(String[] args) {
for(int i = 0; i < 100; i++) {
char c = (char)(Math.random() * 26 + 'a');
System.out.print(c + ",");
switch(c) {
case 'a',
case 'e',
case 'i',
case 'o',
case 'u',System.out.println("vowel");
break;
case 'y',
case 'w',System.out.println("Sometimes a
vowel");
break;
default,System.out.println("consonant");
}
monitor.expect(new String[] {
"%% [aeiou],vowel|[yw],Sometimes a vowel|"
+
"[^aeiouyw],consonant"
},Test.AT_LEAST);
}
}
} ///:~
‘ )
8,0 ( 9g u 4(L? / Q E (′
h m(,| ` J g [ " b (Y
shhgs@wgqqh.com
a0 45
a1 a2 48 a1
| < J + W 9 # Q N O *? J F 8 ( (? "L F
t S3 9 Q ( F ) 8 ( M,…? "L
3,˙ 3 £? V x
ˇ(?v w; k FA (? % ( 2 s,^ + 3
(
( s h i · [1\h L ; [I J \,(3?z ˇ s h i · ( D E 3 ;?
*4(3 J 3 S | t? [
@ \ˇ ; &
@ | 0?/ @ $ ˇ Fb 3 ;? [
E\ a A h?X F ( ˇD E A h? fl 6 (
-,/-,/-,/-,/
u 0
char c = (char)(Math.random() * 26 + 'a');
L W? < <
8,0 3 D E (:,/ T =
` ( Q ) (O ˉ[ [ $ [
\ T = ; ( % e$ = P
(O ˉ = 8 =
8 (^ *m1,Z ′?[ $? ˉL
( $ 4 Y Z? PA * 8 ( M PA <9 ( ′,#? = > 0
//,c03:CastingNumbers.java
// What happens when you cast a float
// or double to an integral value?
import com.bruceeckel.simpletest.*;
public class CastingNumbers {
static Test monitor = new Test();
public static void main(String[] args) {
double
above = 0.7,
below = 0.4;
System.out.println("above," + above);
System.out.println("below," + below);
System.out.println("(int)above," + (int)above);
System.out.println("(int)below," + (int)below);
System.out.println("(char)('a' + above)," +
(char)('a' + above));
System.out.println("(char)('a' + below)," +
(char)('a' + below));
shhgs@wgqqh.com
a0 46
a1 a2 48 a1
monitor.expect(new String[] {
"above,0.7",
"below,0.4",
"(int)above,0",
"(int)below,0",
"(char)('a' + above),a",
"(char)('a' + below),a"
});
}
} ///:~
D E = >
# =? "( M A fl §? ( "{ +
c d
8,m A,0 3 9g u 4(L L 2 X 2 3 [
\? "F – A 9A F9A G
9A G F9A? 3 Sh 2 3 3 Sh?X 2 3 ˇ W ` 3? ¢ Q? = > 0
//,c03:RandomBounds.java
// Does Math.random() produce 0.0 and 1.0?
// {RunByHand}
public class RandomBounds {
static void usage() {
System.out.println("Usage,\n\t" +
"RandomBounds lower\n\tRandomBounds upper");
System.exit(1);
}
public static void main(String[] args) {
if(args.length != 1) usage();
if(args[0].equals("lower")) {
while(Math.random() != 0.0); // Keep trying
System.out.println("Produced 0.0!");
}
else if(args[0].equals("upper")) {
while(Math.random() != 1.0); // Keep trying
System.out.println("Produced 1.0!");
}
else
usage();
}
} ///:~
%? F? ' t 0
java RandomBounds lower
shhgs@wgqqh.com
a0 47
a1 a2 48 a1
java RandomBounds upper
n? A u? b? X X & D E < J +
8,c X,J 0 9$ 9[ X,0 $ 9( (3 ˉ?
a9 a10 a11 a12
9g u 4m(
a13 a14 X (L D E% ¢?Q L ;,B @ – 1 ¢?(,( #
8,”0 9$ 9 "F – A (L
F9A
3?',′ V "( m(G p 0@ 1 k /
R e$ = F T?,; ( 3 ¨? ¥ _? (,3? W,' £? | ¥ _ (f *? ¢ u e( ′ c d
% 3 ! L ;
u # $
# $ % & ’ (W? J? m3 ; d (= >
F (?k / R ˇ3 *me u h i · P e u h i · kg?+ E,G W,J 0 X (O ˉ
P T 3 ; (
ˇ? ˇe (e ˙ A n o X F "
u 4 2 3
3,],g (L (
,; ( ˙ A F ( M _ ! ` ¢,
Q £ "
3? e T "( ( 8 e
| PO ˉ],! Q T 8 ( M %
1 8 F i X ( N
3, 0 L ( n o L ′ ) ) - ) 3 0 (L
,; (? o ˇ( Q 2 # ; Q *o,
3 V m e [ ( Q $ " | PA ],! Q
D $ " y ; T A? R( | "
shhgs@wgqqh.com
a0 48
a1 a2 48 a1
3 ˙ A,],3 %? | P k (
E? ¢,/? ¢, m ( | P
&,` < < O ˉ”?
a9 a10 a15 a12
’ # * ’,( FH,,(9J
= 8 !7 H H T @ Y? –?,9 49 £
= 8 !7? 3 c ′? ( ; @ (' t · (W
(,? m? (;a h y "([
C\[ ′? ( Q m | ¥ "([ \e f ^ [
C\ ) t (h y " – ) § h " W (@ "/ # D (
9 Y % [ ;( ! B Q < gm,Z ^? ˉ < gy " m;( ! ("(,A E ; h ( Y 3 currency1 < gy "? ([
\ F 49 £ fl? ’ c d
= 8 !7 m,? 9( A X 3 m(? J? ˉ% h | ¥ "( W? "( X ^ [ ′? ( ˇ
a9 a10 a11 a12
’ * !? 0> C "#,; h ( z
"? ! 3 ! ! § *
h "? ( y z 9( ! (p "? b ′ (y"
b (y " H CCC 4 " 0
!<" AK !<" " K L AK " D E " E m( "
" !<" A !<" " ! " 3L "
" " 3!< ! " 3!< ! " 3L "
" 3!< ! " 3L
" 3M " 3L " (3 H
F h h § y " FF,9((A 9G,8
c 2 3 "[ 2 3? " D E h i · ( I " [ (E,(,
(E ( ′ (E,(,FF9A u 4 < < F
’ J I I @@@$ ’ $ I 99,$ ’ h ((b
a0 1
a1 a2 48 a1
2002 7
chinapub ! " # $ % ! & ’ ( ) *+,-,&/ 0 (1 2
3 45 (
6 7,8 9 3,) ; < =,> 8 (? @ A B 3 C D E
Bruce EckelFG (H I J K ( L ( M N O PA ! 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 c p 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 A F? ·? fl,? … 3C ¥ ‰ Q $?,
` m
Bruce Eckel? (′?,?,1000V W? ˉ? ˙? = G ¨ XD E? m3 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 48 a1
}m a (0 Y? v w m; a A ( F ^
*1 N O
˙? Q ! "# ! Q 1 N O
$,%,¥ A ( g & ’ X ^
[ 1,3 ( ) *? ˉ? 3? mC U Y Z / + < <,- (V,/
0 A?1 2 3,' 4 (5 6 7 8 9,; E < = >? @ 4 (A ( % ˙? B C r D E F ;
D (G H I
% 3 ge ",J 0 3 K (L "( M
(N X O ˉ > ( P Q R
E < - S ( § A? c > 1 ) 1 "
,J 0 3 L ‰ T ) 1 "(L T U V 1
W < g( Q 1 "( 1 X,V 1? v w Y Z T D J 0 (L [ E ( A Y mV 1 ( 3
i j D m(? ; 1 )
[ \ [ \ E <[
\ ] ‰ A W E 1 ) ^ ¥ _ ‘ [ ¥ _? 3 ˙,a b (c d
‰ e[ f g [ \ [ \
3 h i · 2 j V ( M (k / l,N O w? @
h i · (L m3 m n o @ l (p q r s § *b ) t(3 u?/ Q R Pˇ % W,P§ o (k / lv,D E? v w 3 SQ w x (y w @ (l ]? 0
a = x + y - 2/2 + z;
3 h i · J e z 3 Su,m{ | X (? } 0
a = x + (y - 2)/(2 + z);
shhgs@wgqqh.com
a0 3
a1 a2 48 a1
~ L (
A (? }? ! - S 1 (L? U
| g- S( 1? U ˇ E ^? ; J 0 L (h i ·
s ; 3 w x( m (
[ $ % m3 ¢ 4Q L
8 E? ~? 0
a = 4;
X ; L ~ [ $? X ;
(? X; $
~ L 8 ) t ( ‘ ) g m(FF(L X y ¥ _ (
~ L ( M? R 3 r g 3 r?
ˉ? $
Y Z ( R,T g? ˉ Q?,
Y Z | X,g ( ¥ ) % Q $ F ′ V " G W D % (
¥ _ ~ L ( M mD X,%? | ¥ _?
^ Q ( 1 D EF¥ _ u 4ˇ( ~ L ( M? J? F A (
[ $? ˉ? ¥_? $
O ˉ,? y / m y,(Y ¥_,? (],0
//,c03:Assignment.java
// Assignment with objects is a bit tricky,
import com.bruceeckel.simpletest.*;
class Number {
int i;
}
public class Assignment {
static Test monitor = new Test();
public static void main(String[] args) {
Number n1 = new Number();
Number n2 = new Number();
n1.i = 9;
n2.i = 47;
System.out.println("1,n1.i," + n1.i +
",n2.i," + n2.i);
n1 = n2;
System.out.println("2,n1.i," + n1.i +
",n2.i," + n2.i);
n1.i = 27;
shhgs@wgqqh.com
a0 4
a1 a2 48 a1
System.out.println("3,n1.i," + n1.i +
",n2.i," + n2.i);
monitor.expect(new String[] {
"1,n1.i,9,n2.i,47",
"2,n1.i,47,n2.i,47",
"3,n1.i,27,n2.i,27"
});
}
} ///:~
~ / ′? 3 K ( 3 0
import com.bruceeckel.simpletest.*;
A t,)? ¢ £ (?
ˇe? W,g
` ¥? ' £ e? F e( m Z 3 0
static Test monitor = new Test();
A,§ currency1 3
(( e(?]
(3 e F (b?,< g0
monitor.expect(new String[] {
"1,n1.i,9,n2.i,47",
"2,n1.i,47,n2.i,47",
"3,n1.i,27,n2.i,27"
});
"z h ( (? @ ' ! ( M X,P' ! ],! Q,? A "z 1 8 E X x )?”? ˉ g,
Y A fi O,N ' ! (
; fl £?< g x (' !,
e ) t § currency1,e e(?] !
" A W ( T ~ J,X (L | P " ~? ! `
! F V ( E? | ! " –,? e (¥ _ F
‘ ) ¥ ( ~ L
E < ! ¥ !(,g " · !
"? e y 3 ¥ _ /Y F
! ( y ( L (¥ _ F~ L ^
shhgs@wgqqh.com
a0 5
a1 a2 48 a1
* T,? J T,A D y (Y ¥ _,T,? … ‰? ¢?
_? T U
v ¥ _ (G? ˉ? | `
v w Z 1 ′ E ( 3? ¥? /("# % ( ~ L 0
n1.i = n2.i;
ˉ? 3 ¥ _ `
! " g 3 ¥ _ > 8? ; ˙e > ¨ (¥ _,X ^? C,¥ _ ("# ( m
@ ( s 4 d X 3 e ; '( D E W PA k g,5
!? W,FY ˇ ( F @ 4? % ¥ _ ~ L ;,? (f — ′ >,
P¥ _? ( M [,m
(c d 0
//,c03:PassObject.java
// Passing objects to methods may not be what
// you're used to,
import com.bruceeckel.simpletest.*;
class Letter {
char c;
}
public class PassObject {
static Test monitor = new Test();
static void f(Letter y) {
y.c = 'z';
}
public static void main(String[] args) {
Letter x = new Letter();
x.c = 'a';
System.out.println("1,x.c," + x.c);
f(x);
System.out.println("2,x.c," + x.c);
monitor.expect(new String[] {
"1,x.c,a",
"2,x.c,z"
});
}
} ///:~
F V
#,FA (1?¥ "$ %1 3 (
D E?
shhgs@wgqqh.com
a0 6
a1 a2 48 a1
y.c = 'z';
J,
# ‰? (¥ _ (' ! E,3C
! < § £ N > (4 d | W % g,5 !,1c? ' £? F m c d?,? g3? ˇ
F ′ V " G ("F? > (0
P
R Q A,…? "> Ru (o "? ">R,P O ˉ( "C? c {? X 1? t
[ m E ( @ ~ L ()? S A 3 - SQ h F
* h ; 3 a r ) D m( % m?,
8 % " | ` ~ 0
],"F ( 0
//,c03:MathOps.java
// Demonstrates the mathematical operators,
import com.bruceeckel.simpletest.*;
import java.util.*;
public class MathOps {
static Test monitor = new Test();
// Shorthand to print a string and an int,
static void printInt(String s,int i) {
System.out.println(s + " = " + i);
}
// Shorthand to print a string and a float,
static void printFloat(String s,float f) {
System.out.println(s + " = " + f);
}
public static void main(String[] args) {
// Create a random number generator,
// seeds with current time by default,
Random rand = new Random();
int i,j,k;
// Choose value from 1 to 100,
j = rand.nextInt(100) + 1;
k = rand.nextInt(100) + 1;
printInt("j",j); printInt("k",k);
i = j + k; printInt("j + k",i);
i = j - k; printInt("j - k",i);
i = k / j; printInt("k / j",i);
i = k * j; printInt("k * j",i);
i = k % j; printInt("k % j",i);
j %= k; printInt("j %= k",j);
shhgs@wgqqh.com
a0 7
a1 a2 48 a1
// Floating-point number tests,
float u,v,w; // applies to doubles,too
v = rand.nextFloat();
w = rand.nextFloat();
printFloat("v",v); printFloat("w",w);
u = v + w; printFloat("v + w",u);
u = v - w; printFloat("v - w",u);
u = v * w; printFloat("v * w",u);
u = v / w; printFloat("v / w",u);
// the following also works for
// char,byte,short,int,long,
// and double,
u += v; printFloat("u += v",u);
u -= v; printFloat("u -= v",u);
u *= v; printFloat("u *= v",u);
u /= v; printFloat("u /= v",u);
monitor.expect(new String[] {
"%% j = -?\\d+",
"%% k = -?\\d+",
"%% j \\+ k = -?\\d+",
"%% j - k = -?\\d+",
"%% k / j = -?\\d+",
"%% k \\* j = -?\\d+",
"%% k % j = -?\\d+",
"%% j %= k = -?\\d+",
"%% v = -?\\d+\\.\\d+(E-?\\d)?",
"%% w = -?\\d+\\.\\d+(E-?\\d)?",
"%% v \\+ w = -?\\d+\\.\\d+(E-?\\d)",
"%% v - w = -?\\d+\\.\\d+(E-?\\d)",
"%% v \\* w = -?\\d+\\.\\d+(E-?\\d)",
"%% v / w = -?\\d+\\.\\d+(E-?\\d)",
"%% u \\+= v = -?\\d+\\.\\d+(E-?\\d)",
"%% u -= v = -?\\d+\\.\\d+(E-?\\d)",
"%% u \\*= v = -?\\d+\\.\\d+(E-?\\d)",
"%% u /= v = -?\\d+\\.\\d+(E-?\\d)"
});
}
} ///:~
~ / < g( e O (],0
&,/ ],3
| ` 3 ’,/ ],3
` ],3 # / § currency1 3
( ¥ _ E J 0 " ‘ ) § currency1 ^ * m "
,( 41 "0 (
( ¥ _ ( & ’ 0,V X e( "
[ E $ )
, "0 J 0 (" o ! F 1 "P3 ( u
(
shhgs@wgqqh.com
a0 8
a1 a2 48 a1
‘ ) g, " ' ! (O ˉ”3 currency1 3 currency1 X 3? D E
X ; ` }^ + Y? 3 X r #?,' ! Z,%£ N c d
g3 # $ " D t( T U s h i · (K (p
F% % & ’ u e( p,
B W % g (?,c? 3 8 ′ (y ˉ?X 1 (? < +,A? % ; =
,% | c?,£ R? #? (
* +,,,- s h i · 3 – ( E $ 0
ˉ?m Y Z A F? (>? ˇ8? % h 3 ; m[ ; m(? S? E F? S? 3 c S }? 0
-?
h? " h 3 V " F s h i · * "
[,\h ( F ( ` 3 ; PA? ˇ 3 0[
.,\ % F s h i ·?h?3 V (h i · ˇ? [
\ D E % h ; m PS 1,3 " ˇ 0
-?\\d+
J? Y (
( 3
( * a ([/ / \? 3?,(
X ) s h i · D ( Y A h Q ( 3 s h i · D E
(? < X gM ( s h i · ( § A % A X ) s h i · p m(? % c?
D E 3 0
%% j = -?\\d+
[
) \”T x? F L [) * \ ([ \ ( [
+\3? F s h i ·?A [ 3 p q 1 u i? v w ; < =,? ˉ? (
g,s h i · (K p ( W,` 1 £
shhgs@wgqqh.com
a0 9
a1 a2 48 a1
t (PS
S PS S c >?,^ (hi · Q n o gh t ( ( ]? h i ·
x = -a;
(? } w ‰? [ E ! 0
x = a * -b;
£ (“,D E 0
x = a * (-b);
t PSh? " mt PS mt ( S X 9 ^ 1
3 g? )? )? E ˙? 9 Q ) t – ) Q Z? Y <
P
[ T U 1?,ˇ? P
,ˇ e 8 ()? P(
A (? }?P 3 t ˇ A (?}? 3 t ˇ 8 $
3 Y Z? J
! - ( PX (L,P L …?
e me [? g(
-
-? ˇ(? } ! F h i · (
ˇ(? }
! F h i · ( P
ˇ y ! F h i · ( P
ˇ(? } ! F h i · (? ¥ ) P
[ 1 ^,/@ ` … ¥ ) P
[ 1? / … ` @? ] 0
//,c03:AutoInc.java
// Demonstrates the ++ and -- operators,
import com.bruceeckel.simpletest.*;
shhgs@wgqqh.com
a0 10
a1 a2 48 a1
public class AutoInc {
static Test monitor = new Test();
public static void main(String[] args) {
int i = 1;
System.out.println("i," + i);
System.out.println("++i," + ++i); // Pre-
increment
System.out.println("i++," + i++); // Post-
increment
System.out.println("i," + i);
System.out.println("--i," + --i); // Pre-
decrement
System.out.println("i--," + i--); // Post-
decrement
System.out.println("i," + i);
monitor.expect(new String[] {
"i,1",
"++i,2",
"i++,2",
"i,3",
"--i,2",
"i--,2",
"i,1"
});
}
} ///:~
E < g ( M? D g(? ^ @ (L
(? D g( ( ^ ¢ (L 3 3 R,~ L u ‰
m§ A,( 1 [ $ X % A Q ( @ % A Q 1 "
[
( u 3 A (8 ( 3? ˇ F @ (
! *,& / (§ currency1 u 3 $?
,,ˇ ( P G (? } 3 + R,*V o( ( A 3 ) t (?,(( "?,< g V r? ) t,
X g?8 ) t V ˇ(r?
#,J 0
e$(O ˉ A W,n o 1 "(L (′ #? ˉ # % s # h i · …
ˉ # s …
# # m ) 0 ′ ) 1 ) - ) 0 ′ ) - )
1 > - E < X - 2 D m( "#e$? ; 8 > -
3 X - 3 ¥ e$ R,> - X - (u ‰ § A 8? (
shhgs@wgqqh.com
a0 11
a1 a2 48 a1
# ; ) D m¥ _ A W (? },PK &W? ’ (? 3 ] 0
//,c03:Equivalence.java
import com.bruceeckel.simpletest.*;
public class Equivalence {
static Test monitor = new Test();
public static void main(String[] args) {
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1 == n2);
System.out.println(n1 != n2);
monitor.expect(new String[] {
"false",
"true"
});
}
} ///:~
h i ·
%,,! ",P8 O ˉ(
L ],! Q | ‘ ) e & ¥ _ c > ( ' ! O ˉv w /
# | | ¥ _ (? R ˇ > A W (
X 8 ( ¥ _ (
D E? (' ! v w / # ¥ K & W |,) 3 * (
% | 8 e ¥ _ (? R w Z + ′?,e? m
( ( 0 m
w? 0
//,c03:EqualsMethod.java
import com.bruceeckel.simpletest.*;
public class EqualsMethod {
static Test monitor = new Test();
public static void main(String[] args) {
Integer n1 = new Integer(47);
Integer n2 = new Integer(47);
System.out.println(n1.equals(n2));
monitor.expect(new String[] {
"true"
});
}
} ///:~
F(O ˉ - *(
,,Y Z ) t? ˉ? § currency1,(e 8 $? 0
//,c03:EqualsMethod2.java
shhgs@wgqqh.com
a0 12
a1 a2 48 a1
import com.bruceeckel.simpletest.*;
class Value {
int i;
}
public class EqualsMethod2 {
static Test monitor = new Test();
public static void main(String[] args) {
Value v1 = new Value();
Value v2 = new Value();
v1.i = v2.i = 100;
System.out.println(v1.equals(v2));
monitor.expect(new String[] {
"false"
});
}
} ///:~
/,3 0 … Q,0O ˉ
# 0 (1 8
D E Rfl? FK e
0 2 s? 3? @ ( ˉ 4 5 W % g 4?,' g % g
,'? x r O 0 F @ 4 ˙?
0? ;? X 6 4 ′ V "
e? (e,0 D E A W,8 ¥ _ ( R X
7 8
5 5 6 6 fl 2,9 # "(7 8 # J 0 3
# ( L? # 7 8 (] 0
//,c03:Bool.java
// Relational and logical operators,
import com.bruceeckel.simpletest.*;
import java.util.*;
public class Bool {
static Test monitor = new Test();
public static void main(String[] args) {
Random rand = new Random();
int i = rand.nextInt(100);
int j = rand.nextInt(100);
System.out.println("i = " + i);
System.out.println("j = " + j);
System.out.println("i > j is " + (i > j));
System.out.println("i < j is " + (i < j));
System.out.println("i >= j is " + (i >= j));
System.out.println("i <= j is " + (i <= j));
System.out.println("i == j is " + (i == j));
System.out.println("i != j is " + (i != j));
shhgs@wgqqh.com
a0 13
a1 a2 48 a1
// Treating an int as a boolean is not legal
Java,
//! System.out.println("i && j is " + (i && j));
//! System.out.println("i || j is " + (i || j));
//! System.out.println("!i is " + !i);
System.out.println("(i < 10) && (j < 10) is "
+ ((i < 10) && (j < 10)) );
System.out.println("(i < 10) || (j < 10) is "
+ ((i < 10) || (j < 10)) );
monitor.expect(new String[] {
"%% i = -?\\d+",
"%% j = -?\\d+",
"%% i > j is (true|false)",
"%% i < j is (true|false)",
"%% i >= j is (true|false)",
"%% i <= j is (true|false)",
"%% i == j is (true|false)",
"%% i != j is (true|false)",
"%% \\(i < 10\\) && \\(j < 10\\) is
(true|false)",
"%% \\(i < 10\\) \\|\\| \\(j < 10\\) is
(true|false)"
});
}
} ///:~
F
( s h i ·? 3 S Q h i · z ( [
1\h 7 8? ˇ D E 0
(true|false)
h ( ; [
\ [# \ ‘ ) m F sh i · mp q? D E % ˙ A W F s h i · *h § Q ( [
..\¥ A W ( 7 8?
!7 ˇ? ˇ fl?7 8 ˇ ; ) L? X; }
Y? F7 8 h i · Pfl (L 1
L ¢ [, g,6 ; (< ¢? T Y S?,b h i ·,/ # ( 8 ` 7 8 @ 8 O ˉ(L
%? ˉP
L 1 Q ( A,T = > v (
FJ E P
= R u ‰ (^ "# Y Z > C "(8? @? ( e "u 4A B mh b L (Y 3 mB [,?X - ˇ( 3 " % m3 X
C Y A X C
shhgs@wgqqh.com
a0 14
a1 a2 48 a1
¢ 7 8 ( M,D g3 E?F
’ ˇ( _ [ $ % ; x r? h i · %? G 1 ! no? m ;,! @ 7 8 h i · ( H ; 1 ! n o
(? 3 F (] 0
//,c03:ShortCircuit.java
// Demonstrates short-circuiting behavior,
// with logical operators,
import com.bruceeckel.simpletest.*;
public class ShortCircuit {
static Test monitor = new Test();
static boolean test1(int val) {
System.out.println("test1(" + val + ")");
System.out.println("result," + (val < 1));
return val < 1;
}
static boolean test2(int val) {
System.out.println("test2(" + val + ")");
System.out.println("result," + (val < 2));
return val < 2;
}
static boolean test3(int val) {
System.out.println("test3(" + val + ")");
System.out.println("result," + (val < 3));
return val < 3;
}
public static void main(String[] args) {
if(test1(0) && test2(2) && test3(2))
System.out.println("expression is true");
else
System.out.println("expression is false");
monitor.expect(new String[] {
"test1(0)",
"result,true",
"test2(2)",
"result,false",
"expression is false"
});
}
} ///:~
,? ¢ "? ( 8 | …
‰ A,],3 E w T N,,? (h i · (? ¢ 0
if(test1(0) && test2(2) && test3(2));?,| | r L? ¢? % 3 I ' ! (O ˉJ h
w? fl? 3? ¢ …,D E h i · % (
shhgs@wgqqh.com
a0 15
a1 a2 48 a1
+? ¢ …,# $? h i · (O ˉfi O
# Y Z Z % n o + ′? Z 1 K L,? J F (f—? ˉ;? 3 7 8 n o ;,g M; ˙? N
e$("# e "(> ¥ v ( ( O P § O ˉ (O ˉ
^ Q ( m3 Q R (p C? E A + ! S T? %? S T (U,b
T 1 %V t W X? (? Y Z Q? @ ( D E ˙ 3 h [ \ ; m fl ( X ^? ′ V 6?,Q
ˉ' t? 3 s?
2 ˇ” … 3 2 s C ' t (e * % m3 3 s?
1 ˇ” … 3 me? C( M A … C? ˉ' t (e *m m3 3 s?]
3 ˇ” … 3 fl 4 [ T U
3 t A % 3 " § A (?
fl,¥ ' t C 3 3 C 7 8 > ( S D E W u ¢ ‘ Q ^ _ Y
‘ 0 8? ˇ D E 3 E
a 9 Q E h ( @ ~ L 02 1
3? b ( – ) 4 A c | 3 t [ X %? F3 9,
5 e$”T 1 m3 (L D E A V 6 mC X? E ( E < ] X ; ( fl;,d + 7 8fl > e f
¥ ) 7 8 (F\ ; J > ( A mF ‰
(?m3 7 8 D m(]
L X ; g – ) Z g W h J '
g [ 3 A ; )? "$(
"# g,P
6 6 1 Y 1 " g A 1 Y "D h ( " Q Ci j
S( g,P 7 7 1 Y 1 " g A 1 Y "D h ( " S( g
7 7?9 #? SQ k "
ˇ(r s 0? ˉ L ( s l 3 m i C? ˉ L? ( s l c i 3
[ mX? S( g
7 7 7 A (?3 m C Q k " ˇ(r s 0X n? S l? i C 3
m(
shhgs@wgqqh.com
a0 16
a1 a2 48 a1
ˉ¥ 8 % 8 (,g A W,T / =
(O ˉ[,( 1 Y " mQ m,d + g ( "{ ^
( "? ˉ 1 "
Y Z O ˉ[ 1 Y "(Q o m? g ( " X,{ ^
( " g E - Sa 9 Q
6 6 7 7 7 7 g,
u,T ` ( X S( g - Sa ( M,m3 c d? ˉ 1 "
% 8 ( O ˉ X ¥,A W,T / M g
` ( g K ~ L? / ( L T { F D E g Q g + O ˉ?
!?,ˉ0
//,c03:URShift.java
// Test of unsigned right shift,
import com.bruceeckel.simpletest.*;
public class URShift {
static Test monitor = new Test();
public static void main(String[] args) {
int i = -1;
System.out.println(i >>>= 10);
long l = -1;
System.out.println(l >>>= 10);
short s = -1;
System.out.println(s >>>= 10);
byte b = -1;
System.out.println(b >>>= 10);
b = -1;
System.out.println(b>>>10);
monitor.expect(new String[] {
"4194303",
"18014398509481983",
"-1",
"-1",
"4194303"
});
}
} ///:~
b 3 currency1 g mP O ˉ~?
],! Q,D E A x (
],D m 1 > ( ( 0
//,c03:BitManipulation.java
// Using the bitwise operators,
import com.bruceeckel.simpletest.*;
import java.util.*;
public class BitManipulation {
static Test monitor = new Test();
shhgs@wgqqh.com
a0 17
a1 a2 48 a1
public static void main(String[] args) {
Random rand = new Random();
int i = rand.nextInt();
int j = rand.nextInt();
printBinaryInt("-1",-1);
printBinaryInt("+1",+1);
int maxpos = 2147483647;
printBinaryInt("maxpos",maxpos);
int maxneg = -2147483648;
printBinaryInt("maxneg",maxneg);
printBinaryInt("i",i);
printBinaryInt("~i",~i);
printBinaryInt("-i",-i);
printBinaryInt("j",j);
printBinaryInt("i & j",i & j);
printBinaryInt("i | j",i | j);
printBinaryInt("i ^ j",i ^ j);
printBinaryInt("i << 5",i << 5);
printBinaryInt("i >> 5",i >> 5);
printBinaryInt("(~i) >> 5",(~i) >> 5);
printBinaryInt("i >>> 5",i >>> 5);
printBinaryInt("(~i) >>> 5",(~i) >>> 5);
long l = rand.nextLong();
long m = rand.nextLong();
printBinaryLong("-1L",-1L);
printBinaryLong("+1L",+1L);
long ll = 9223372036854775807L;
printBinaryLong("maxpos",ll);
long lln = -9223372036854775808L;
printBinaryLong("maxneg",lln);
printBinaryLong("l",l);
printBinaryLong("~l",~l);
printBinaryLong("-l",-l);
printBinaryLong("m",m);
printBinaryLong("l & m",l & m);
printBinaryLong("l | m",l | m);
printBinaryLong("l ^ m",l ^ m);
printBinaryLong("l << 5",l << 5);
printBinaryLong("l >> 5",l >> 5);
printBinaryLong("(~l) >> 5",(~l) >> 5);
printBinaryLong("l >>> 5",l >>> 5);
printBinaryLong("(~l) >>> 5",(~l) >>> 5);
monitor.expect("BitManipulation.out");
}
static void printBinaryInt(String s,int i) {
System.out.println(
s + ",int," + i + ",binary,");
System.out.print(" ");
for(int j = 31; j >= 0; j--)
if(((1 << j) & i) != 0)
System.out.print("1");
else
System.out.print("0");
System.out.println();
}
static void printBinaryLong(String s,long l) {
System.out.println(
s + ",long," + l + ",binary,");
System.out.print(" ");
for(int i = 63; i >= 0; i--)
shhgs@wgqqh.com
a0 18
a1 a2 48 a1
if(((1L << i) & l) != 0)
System.out.print("1");
else
System.out.print("0");
System.out.println();
}
} ///:~
O p O (e
5 %&
5 %$ A W? T " | / ],3 $ w ( ` E ( ! ( · ],! " F? E X q A Z? (
,? g? (
%,,X
%,,X,= D E A ; ˙? i currency1 ' ! 3
(
3 T 1 " A,T? @ (Oˉ
§ * E m [ E m s h i · ¥ ) Y O ˉ q X r s tF?( T? T (k "?
$ ˇ A [ fl £ E
u mv w ( E ] T < < gh ' ! Z
R,,¥
( ˉ,
(b L b ′ L,(e ( ! h i x ·? l h S0
9h S h? S (' !? (0
-1,int,-1,binary,
11111111111111111111111111111111
+1,int,1,binary,
00000000000000000000000000000001
maxpos,int,2147483647,binary,
01111111111111111111111111111111
maxneg,int,-2147483648,binary,
10000000000000000000000000000000
i,int,59081716,binary,
00000011100001011000001111110100
~i,int,-59081717,binary,
11111100011110100111110000001011
-i,int,-59081716,binary,
11111100011110100111110000001100
j,int,198850956,binary,
00001011110110100011100110001100
i & j,int,58720644,binary,
00000011100000000000000110000100
i | j,int,199212028,binary,
00001011110111111011101111111100
i ^ j,int,140491384,binary,
00001000010111111011101001111000
i << 5,int,1890614912,binary,
01110000101100000111111010000000
i >> 5,int,1846303,binary,
shhgs@wgqqh.com
a0 19
a1 a2 48 a1
00000000000111000010110000011111
(~i) >> 5,int,-1846304,binary,
11111111111000111101001111100000
i >>> 5,int,1846303,binary,
00000000000111000010110000011111
(~i) >>> 5,int,132371424,binary,
00000111111000111101001111100000
e ( ! h i · T U
m y X A mL 1 " A x? 3 … L (
3 C 3,' (,X h i · (x ·? (0
boolean-exp? value0,value1
ˉ
n o s @ | L (O ˉ? ˉ
# s @ (L |? A (O ˉ
|? E M (
#?,' L ) z B
w ( E A 3 ) z ( {
t L 1 m l | (? } F}? ( M % A m ;,~ (
c E A (V 1 [ E % g3 L
3 Q $ L m mL A,> (r? 3 ] 0
static int ternary(int i) {
return i < 10? i * 100,i * 10;
}; < g £ % 8 X L (,,V 0
static int alternative(int i) {
if (i < 10)
return i * 100;
else
return i * 10;
}
shhgs@wgqqh.com
a0 20
a1 a2 48 a1
9 Q ) t [ V X,i D E L ( M 3 O % L } 3 Q $ Fe 3 ( 8 b ¢ (
F
SX T ) "( " 3 a ~ L
3 ( 3 3 P S (r,' (
#
F
S 3 mp q ( 0 ( A ; a | ; J ( m X 3?
( 8 | (? @
( M ′? ; u
v w 3 X (?,p A ˙ % ; i j D m(? J p (? } X ( % %? @ (e? ( M u E <
(§ A 3 o!,A ^ ),? ˉ
| u (,8
) t,V? @ W p,( % X ; }
( % Y? + u S
F J ( M,mw? ˉh i · E ( Y Z D m?,( 1 "?
Y Z?,P S3 9 Q ( =
( 0
int x = 0,y = 1,z = 2;
String sString = "x,y,z ";
System.out.println(sString + x + y + z);
,P % 9? = A W ( x · X / PA W 9 Q ` =? ˉ 0
System.out.println(x + sString);
Y Z
,P = ! " # ! " # ! " # ! " #
b? ( X ; c fi O h i ·,? P3 S?
, [ 3?
m3 ! | l ( 0
shhgs@wgqqh.com
a0 21
a1 a2 48 a1
while(x = y) {
//,..,
}
w % | 1 > - (? ¢
X ~ L F %
%X - ) C ~ L O ˉ A ;,x 3 o F
F- 3 L h i · (O ˉX
X,P =
( M A,D E,F ( M T ! Q
[ X,0 e 3 3 X,g? ( x
% % 3 b (h i · FJ £ * m ;
m3 e (c d Y (? ˇ? ˇ 7 8 (? ˇ? ˇ m3
2 1 7 8 % me
22 11 3? 3? e ( R S e,
` 3 currency1 p ! AX,˙? r 3 e$ 1 3 e$Q
$ % & ’ $ % & ’ $ % & ’ $ % & ’
(? }?P g?ˇ % b r,( 3 "#e$( = 3 "#e$ 8 $ P? "L ~?> C ( M?,
= # D g3 X,( = ( ( M? [ E w x r # A ( = !( e$ =
% ( =? P"#(A e$
2 3 D m( k ( 1 (3 S ] 0
void casts() {
int i = 200;
long l = (long)i;
long l2 = (long)200;
}
D < g(? c E ¥ "L [ E ¥ ( = F
( e =? V o ( 3 m %?,M
X ^,V o ( = u £,? Fm,% ˙ £ ;? ^ = ; (,
F
e$ = 3 T ˙,a ( (e$ =s c ( D gD (
( & [ $? 3 ; V? ("#e$ = 3 X,Z V
shhgs@wgqqh.com
a0 22
a1 a2 48 a1
("#e$ A,J 0 3 ],!? w x r ( =? J A (¢ £ Z T ;? ¥? ˉ? 3 O % T Y Z w x r # ˇ ¥ )
X ( · =,K e$ E 8? e$ V (? D E Z? ˉ X,
§,? ¥ R u ‰ (§ A e$("#( ^?( =
s 9 X ; = e X §,= ( % ˙ A W >¨ = % mp q (
3 p ] ‰?,' D m¥ _? E FA (e#
! ! J
,; currency1 ' E T g u [ E A X ;? 3 e#(e$ 8?
( ; ! ! ! !
, L ( M? ; x r Z e$
( Fm e$? e ( D g ( M? ; t? L > ( Q? ‰? E Q fi,0
//,c03:Literals.java
public class Literals {
char c = 0xffff; // max char hex value
byte b = 0x7f; // max byte hex value
short s = 0x7fff; // max short hex value
int i1 = 0x2f; // Hexadecimal (lowercase)
int i2 = 0X2F; // Hexadecimal (uppercase)
int i3 = 0177; // Octal (leading zero)
// Hex and Oct also work with long,
long n1 = 200L; // long suffix
long n2 = 200l; // long suffix (but can be
confusing)
long n3 = 200;
//! long l6(200); // not allowed
float f1 = 1;
float f2 = 1F; // float suffix
float f3 = 1f; // float suffix
float f4 = 1e-45f; // 10 to the power
float f5 = 1e+9f; // float suffix
double d1 = 1d; // double suffix
double d2 = 1D; // double suffix
double d3 = 47e47d; // 10 to the power
} ///:~
h
,( ! "( M % < <= ] a? J < > ′ E (
# D m(? "e$? ; E,( ! (x · E ¢ %? 3 8 e$(b ′ L ′ ("Q f * (
X " E A x · h (
,3? 3 F? (? W!,
8 % E < 8 D ; h (b ′ (L?
shhgs@wgqqh.com
a0 23
a1 a2 48 a1
ˉ{ !,?,r L = | #? ~ L M (
)? ^,;( ! " ‘ 9?,3 9,4(" z ( me ( !? L (h
F? L? (Y " w,A (e$ ′ (
$h
′ (’h # ′ ()h
' gy "( M fl m – F y · [
\h | ¥ "(h " ′
($ 4 ; (? 8,@,?! 3 x(
L } $ < - a3
a4 a5? (y "h x ·? J
$ < - ($ 4 ;a3
a4 a5? @
= 8 !7 ( M? @ W Q h W? (?
9(? ˇ (N O = 8 !7 y @? @ (? @,% F t e ( M v w? ¥
a6 a7 a5 a8 X Z $,g,
E < F(
D E? ˉ ; ) P 1 | ¥ "(h "( Y Z <g
(!,A> B # 3? …,? J A h
$ < - 9a3
a4 a5
‰ 3? ˉ? ; x O "#(e$ Y? X ` J?
,8? 0
long n3 = 200;
m” e (r D E
(99? (> V o (? ˉ
float f4 = 1e-47f; // 10 to the power
(,,P y "h ("
D E? ˉ m? ( ( A, 3? #
= # " #" #" #" #
,? ˉ ¥ 8
("# [ 8 % 8 $("#
( "F ( L,/ T M g $` ( D E (O ˉ |
D E? P L ~?Y (e$? ( e$ =
‘ ) PL ~?3 (e$,ˉ 6?
fl u h i · b ′ (Y "#(e$”
shhgs@wgqqh.com
a0 24
a1 a2 48 a1
N O h i · (O ˉ("#e$? ˉ? 3 # 3 (` g(O ˉ[
ˉ? 3 3 ( Oˉ[
( )( )( )( )
( 9 # mp q (0A,#? "#% ′ V 6 (
% m 9 # (b % (,g? > ("#e$FX (? J ;,X 3? D E % F( "#′ > ( 1 u /,£ 3 e$mV ′ 8? 3 £ @
<(? " 3 £,% E F? Y £ ′ (" ;? [? g,¥
( % Q $ g? 3 T a (
mg? (c d X % 9 # D m"#e$FD m(? J? > (? X R (g? q A? T (,
* + * +* +* +
3 currency1 4 J m F 0 ˉ g F? ˙ (k / R mV U Y ( M X
} ¨ r?!,3 _ Y ! & > * ! $ ˇ
,%,
t? &
! – ! ’ E < g / 6 6 7 7
& # 7 6 7 6
> * 7 8 > E < 22 11 2 1 3
u T L ( 7 5 C = D E
! > ~ L ! E < } x · (~ L |? ˉPg J ( X 3 (_ Y?,
ˉX? ( A X,-,-,-,-
,
"#? E A ( G J X e$("# 3 ] ‘ ) mc d (£?
,? T X,mc d (
//,c03:AllOps.java
// Tests all the operators on all the primitive data
types
shhgs@wgqqh.com
a0 25
a1 a2 48 a1
// to show which ones are accepted by the Java
compiler,
public class AllOps {
// To accept the results of a boolean test,
void f(boolean b) {}
void boolTest(boolean x,boolean y) {
// Arithmetic operators,
//! x = x * y;
//! x = x / y;
//! x = x % y;
//! x = x + y;
//! x = x - y;
//! x++;
//! x--;
//! x = +y;
//! x = -y;
// Relational and logical,
//! f(x > y);
//! f(x >= y);
//! f(x < y);
//! f(x <= y);
f(x == y);
f(x != y);
f(!y);
x = x && y;
x = x || y;
// Bitwise operators,
//! x = ~y;
x = x & y;
x = x | y;
x = x ^ y;
//! x = x << 1;
//! x = x >> 1;
//! x = x >>> 1;
// Compound assignment,
//! x += y;
//! x -= y;
//! x *= y;
//! x /= y;
//! x %= y;
//! x <<= 1;
//! x >>= 1;
//! x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting,
//! char c = (char)x;
//! byte B = (byte)x;
//! short s = (short)x;
//! int i = (int)x;
//! long l = (long)x;
//! float f = (float)x;
//! double d = (double)x;
}
void charTest(char x,char y) {
// Arithmetic operators,
x = (char)(x * y);
x = (char)(x / y);
x = (char)(x % y);
shhgs@wgqqh.com
a0 26
a1 a2 48 a1
x = (char)(x + y);
x = (char)(x - y);
x++;
x--;
x = (char)+y;
x = (char)-y;
// Relational and logical,
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators,
x= (char)~y;
x = (char)(x & y);
x = (char)(x | y);
x = (char)(x ^ y);
x = (char)(x << 1);
x = (char)(x >> 1);
x = (char)(x >>> 1);
// Compound assignment,
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting,
//! boolean b = (boolean)x;
byte B = (byte)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
float f = (float)x;
double d = (double)x;
}
void byteTest(byte x,byte y) {
// Arithmetic operators,
x = (byte)(x* y);
x = (byte)(x / y);
x = (byte)(x % y);
x = (byte)(x + y);
x = (byte)(x - y);
x++;
x--;
x = (byte)+ y;
x = (byte)- y;
// Relational and logical,
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
shhgs@wgqqh.com
a0 27
a1 a2 48 a1
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators,
x = (byte)~y;
x = (byte)(x & y);
x = (byte)(x | y);
x = (byte)(x ^ y);
x = (byte)(x << 1);
x = (byte)(x >> 1);
x = (byte)(x >>> 1);
// Compound assignment,
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting,
//! boolean b = (boolean)x;
char c = (char)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
float f = (float)x;
double d = (double)x;
}
void shortTest(short x,short y) {
// Arithmetic operators,
x = (short)(x * y);
x = (short)(x / y);
x = (short)(x % y);
x = (short)(x + y);
x = (short)(x - y);
x++;
x--;
x = (short)+y;
x = (short)-y;
// Relational and logical,
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators,
x = (short)~y;
x = (short)(x & y);
x = (short)(x | y);
x = (short)(x ^ y);
x = (short)(x << 1);
shhgs@wgqqh.com
a0 28
a1 a2 48 a1
x = (short)(x >> 1);
x = (short)(x >>> 1);
// Compound assignment,
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting,
//! boolean b = (boolean)x;
char c = (char)x;
byte B = (byte)x;
int i = (int)x;
long l = (long)x;
float f = (float)x;
double d = (double)x;
}
void intTest(int x,int y) {
// Arithmetic operators,
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
x = -y;
// Relational and logical,
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators,
x = ~y;
x = x & y;
x = x | y;
x = x ^ y;
x = x << 1;
x = x >> 1;
x = x >>> 1;
// Compound assignment,
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
shhgs@wgqqh.com
a0 29
a1 a2 48 a1
x &= y;
x ^= y;
x |= y;
// Casting,
//! boolean b = (boolean)x;
char c = (char)x;
byte B = (byte)x;
short s = (short)x;
long l = (long)x;
float f = (float)x;
double d = (double)x;
}
void longTest(long x,long y) {
// Arithmetic operators,
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
x = -y;
// Relational and logical,
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators,
x = ~y;
x = x & y;
x = x | y;
x = x ^ y;
x = x << 1;
x = x >> 1;
x = x >>> 1;
// Compound assignment,
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
x <<= 1;
x >>= 1;
x >>>= 1;
x &= y;
x ^= y;
x |= y;
// Casting,
//! boolean b = (boolean)x;
char c = (char)x;
byte B = (byte)x;
short s = (short)x;
int i = (int)x;
float f = (float)x;
double d = (double)x;
shhgs@wgqqh.com
a0 30
a1 a2 48 a1
}
void floatTest(float x,float y) {
// Arithmetic operators,
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
x = -y;
// Relational and logical,
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators,
//! x = ~y;
//! x = x & y;
//! x = x | y;
//! x = x ^ y;
//! x = x << 1;
//! x = x >> 1;
//! x = x >>> 1;
// Compound assignment,
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
//! x <<= 1;
//! x >>= 1;
//! x >>>= 1;
//! x &= y;
//! x ^= y;
//! x |= y;
// Casting,
//! boolean b = (boolean)x;
char c = (char)x;
byte B = (byte)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
double d = (double)x;
}
void doubleTest(double x,double y) {
// Arithmetic operators,
x = x * y;
x = x / y;
x = x % y;
x = x + y;
x = x - y;
x++;
x--;
x = +y;
shhgs@wgqqh.com
a0 31
a1 a2 48 a1
x = -y;
// Relational and logical,
f(x > y);
f(x >= y);
f(x < y);
f(x <= y);
f(x == y);
f(x != y);
//! f(!x);
//! f(x && y);
//! f(x || y);
// Bitwise operators,
//! x = ~y;
//! x = x & y;
//! x = x | y;
//! x = x ^ y;
//! x = x << 1;
//! x = x >> 1;
//! x = x >>> 1;
// Compound assignment,
x += y;
x -= y;
x *= y;
x /= y;
x %= y;
//! x <<= 1;
//! x >>= 1;
//! x >>>= 1;
//! x &= y;
//! x ^= y;
//! x |= y;
// Casting,
//! boolean b = (boolean)x;
char c = (char)x;
byte B = (byte)x;
short s = (short)x;
int i = (int)x;
long l = (long)x;
float f = (float)x;
}
} ///:~
(o ! (? ; ~?A # e L ;? ¢ A (%? X ; P
L 9 Q ( § A Z 1
[ < g,( "F ( M
8 % E < 8?,/( M O ˉ[
ˉ| PA ~? / Y ( w x r ( e$ =
),ˉ 6? (
¥ )? ` 1 =,A?,X ^ [ ′?,? ˉ? Pe? ′ (
Q 9 Q O ˉ,!?,0
//,c03:Overflow.java
// Surprise! Java lets you overflow,
shhgs@wgqqh.com
a0 32
a1 a2 48 a1
import com.bruceeckel.simpletest.*;
public class Overflow {
static Test monitor = new Test();
public static void main(String[] args) {
int big = 0x7fffffff; // max int value
System.out.println("big = " + big);
int bigger = big * 4;
System.out.println("bigger = " + bigger);
monitor.expect(new String[] {
"big = 2147483647",
"bigger = -4"
});
}
} ///:~
[ #? ( M [? ! ]?
′ m ′ gY z
– 3?
8 % 8 F( e b ~ L
( M [,T M J X % ( e$ = 3? X 1 e$ = [,£ ) t; < g R,
u ‰ D m( e$? ; T = §A (
e$ m F( @ ( M? ¥ A ( ˉm? 2 s?,F = ^ *X X r ˉ,
c ˇ ( ! D E? ˉ? m ( Y Z? v w ¥?' ( —,′ V "^? m3 ! u 4[ m V (r
( m
# F8 F8 # E < F 8 (
m fl ( §? A,£ N c d (’
E } Y? F?g * * (o ! % 8 / (
V V
,
D m(u T? u T h i · D … (O ˉQ n o v w A u
] A 5 3 u T h i · h i ·,u T
% %% %% %% % Q n o (L 2 5(L > - h i ·,…
#? F < g^ (D m #? ; ) u T?
X,§,? P" 1 ( B §,Z T
fl C L h C h #? ˉ? % ¥ fl
shhgs@wgqqh.com
a0 33
a1 a2 48 a1
(L (? ¢ 8? #? ^ } #
<? (u T h i · / A = L
# [,b G ( ! ( D E #? J me 0
if(Boolean-expression)
statement
if(Boolean-expression)
statement
else
statement
u T n o
ˇ % ; J 0 3 O ˉ
c E E Sh O () t [ E 3S3 9 Q ( b % g?
ˇ h A c E ) t [ E b
3
(]? (,# (" ′) ) - ) " 0
//,c03:IfElse.java
import com.bruceeckel.simpletest.*;
public class IfElse {
static Test monitor = new Test();
static int test(int testval,int target) {
int result = 0;
if(testval > target)
result = +1;
else if(testval < target)
result = -1;
else
result = 0; // Match
return result;
}
public static void main(String[] args) {
System.out.println(test(10,5));
System.out.println(test(5,10));
System.out.println(test(5,5));
monitor.expect(new String[] {
"1",
"-1",
"0"
});
}
shhgs@wgqqh.com
a0 34
a1 a2 48 a1
} ///:~
¥ ! ( 3 O (T? ; ˙ R
S r < ! ( O
me 1 0~ / A,#? % … A L? ˉX
+ … $( § currency1 A,? … Y L W E X p Y
0
//,c03:IfElse2.java
import com.bruceeckel.simpletest.*;
public class IfElse2 {
static Test monitor = new Test();
static int test(int testval,int target) {
if(testval > target)
return +1;
else if(testval < target)
return -1;
else
return 0; // Match
}
public static void main(String[] args) {
System.out.println(test(10,5));
System.out.println(test(5,10));
System.out.println(test(5,5));
monitor.expect(new String[] {
"1",
"-1",
"0"
});
}
} ///:~
X `
,u O,/ 0 1 2/ 0 1 2/ 0 1 2/ 0 1 2
^
F8 F8 E < # Q ! ( A W T,U
,X o + g
5 F8 (
while(Boolean-expression)
statement
shhgs@wgqqh.com
a0 35
a1 a2 48 a1
,currency1 u? % n o | currency1
u % ` 1 n o 1 ) t (,X r 0 " gA? x u T 0
//,c03:WhileTest.java
// Demonstrates the while loop,
import com.bruceeckel.simpletest.*;
public class WhileTest {
static Test monitor = new Test();
public static void main(String[] args) {
double r = 0;
while(r < 0.99d) {
r = Math.random();
System.out.println(r);
monitor.expect(new String[] {
"%% \\d\\.\\d+E?-?\\d*"
},Test.AT_LEAST);
}
}
} ///:~
A
8 e? ( ( Q 0 9g u 4(
L 2 3 9 X 2 3 F8 (u T h i · (? }? % L 8
9$? 3 1 + ˇ,currency1 ( M A (' !? X (
,< g F
(? t (?,3
,G $@ Y mi ; § ( Y § *u 3 A h 3 v w – 6 3 currency1 | E § A
,? A (? } v w – 6 ; < g3
L ˇ
F8 (x ·
do
statement
while(Boolean-expression);
F8 F8 ( 3 3 currency1 n o (O ˉ
F8 ( [,– 6 3 currency1 FF8 *? ˉ 3 currency1 n o
Y Z ` [?,, ( M
F8,8 F8 V
shhgs@wgqqh.com
a0 36
a1 a2 48 a1
#,F 3 currency1 ( t u / T f * ` 1 u T n o,currency1 u % m3 ( N? ˇ
# ( 0
for(initialization; Boolean-expression; step)
statement
f * ( u T? ¢ E <
( N E,currency1 u A,? ¢
% 3 < gA # A,* ! +? (?,currency1 u A %
#? T )?@ "ˇ(^ 0
//,c03:ListCharacters.java
// Demonstrates "for" loop by listing
// all the lowercase ASCII letters,
import com.bruceeckel.simpletest.*;
public class ListCharacters {
static Test monitor = new Test();
public static void main(String[] args) {
for(int i = 0; i < 128; i++)
if(Character.isLowerCase((char)i))
System.out.println("value," + i +
" character," + (char)i);
monitor.expect(new String[] {
"value,97 character,a",
"value,98 character,b",
"value,99 character,c",
"value,100 character,d",
"value,101 character,e",
"value,102 character,f",
"value,103 character,g",
"value,104 character,h",
"value,105 character,i",
"value,106 character,j",
"value,107 character,k",
"value,108 character,l",
"value,109 character,m",
"value,110 character,n",
"value,111 character,o",
"value,112 character,p",
"value,113 character,q",
"value,114 character,r",
"value,115 character,s",
"value,116 character,t",
"value,117 character,u",
"value,118 character,v",
"value,119 character,w",
"value,120 character,x",
"value,121 character,y",
"value,122 character,z"
shhgs@wgqqh.com
a0 37
a1 a2 48 a1
});
}
} ///:~
F % gA (r F (? ! h i ·
- ˇ O ( X F a r Fh ( 3 S? O (
(1 m# Z ′ g,
* +,,H 8?@ ˇe A X,P
( 8 2 ( ¥ _,3 ((? g,
( $ F H Q? X?(
u e(,(^,% D m(? F3 ( a O § currency1 ( M,4 F
E F^? r w? ; | £ ( [ l,
E F
#?O V > e$(
for(int i = 0,j = 1; i < 10 && j != 11; i++,j++)
// body of for loop
F
#?O ( 2 3 * m# mF? ! h i· ˇ?O (; a F§ A? Z T
& & & &
F?(? \? g^
F3 r? S ˇ
X? S ˇ Q O "*( (
0F (? ! h i · ˇ? F? ! hi · ˇ(f *? ( N E? V S (,? l o currency1? £ g,p
` 3 ] 0
//,c03:CommaOperator.java
import com.bruceeckel.simpletest.*;
public class CommaOperator {
static Test monitor = new Test();
public static void main(String[] args) {
for(int i = 1,j = i + 10; i < 5;
i++,j = i * 2) {
System.out.println("i= " + i + " j= " + j);
}
monitor.expect(new String[] {
shhgs@wgqqh.com
a0 38
a1 a2 48 a1
"i= 1 j= 11",
"i= 2 j= 4",
"i= 3 j= 6",
"i= 4 j= 8"
});
}
} ///:~
E < gFf *? ( N ( ‰ f
* E O ^? " ( 3 e$(
,? [ E F (?; ! (;,( £ _ !,* ^ (£ *o currency1 ` ( t 3 @ (
,? F
# F8 * ;
0
//,c03:BreakAndContinue.java
// Demonstrates break and continue keywords,
import com.bruceeckel.simpletest.*;
public class BreakAndContinue {
static Test monitor = new Test();
public static void main(String[] args) {
for(int i = 0; i < 100; i++) {
if(i == 74) break; // Out of for loop
if(i % 9 != 0) continue; // Next iteration
System.out.println(i);
}
int i = 0;
// An "infinite loop",
while(true) {
i++;
int j = i * 27;
if(j == 1269) break; // Out of loop
if(i % 10 != 0) continue; // Top of loop
System.out.println(i);
}
monitor.expect(new String[] {
"0",
"9",
"18",
"27",
"36",
"45",
"54",
"63",
"72",
"10",
"20",
"30",
shhgs@wgqqh.com
a0 39
a1 a2 48 a1
"40"
});
}
} ///:~
F
#? [ gX,99 - g4"( M ; *+,3 Q $ mFX ; /? *o u T Z M 0 (,?; (1 % X ;?R
A ˙ K 3?,? ˉ;? R
P L ],! Q,D (? o ˇ ¢ n J ',(
+ A m ; *o ( ; ‰?,< g
,* ^ ( K 3? F? m;? R 9( M,m],‰ 9[ T ],! Q,
9B 9- ) 9 m3 x · ( o
#? ¥ F8 # 3 X ( D E A (c d
’ ( ) * ’ ( ) *’ ( ) *’ ( ) * +
,m,? m,
J F? £ ! (? x 0 ˉu T
! s * g? 2 s * gY?ˇ? J? b,0 ( £ D E? ˉ? ^ £ (?,< g?j,*
,J 0 A ( £ ˇ £ F
J X F %? S T J
s fl £ R J (*?A Q ~ (%? ˉ fl?* gY? Y X m Z + ; K? ¢ £ ˙ AX % Y Z?* ˇ′,
C ) * Y? — (?D
’ ˇ(n ( h % g,? B (r? Y 9 ¥
( ],3? – m,% PA N ! +
§?D g * u b c d X F)
F) X,
F3 p q (? J b ( !
B
( ˙? A PA 1 ( + m m,; e A x?; T ! 3 e *
shhgs@wgqqh.com
a0 40
a1 a2 48 a1
( ˉ X *? J 3 * ! ( u D E,PA W
> n A W,> (? ! 0 3? m S( I }? 0
label1,
F
3 ; k (r F (‰? k F u 4X ; m^ Z T ( 3 ¢ ‘?,V m V [?;,*o,u A,_ g
D F(r 0
label1,
outer-iteration {
inner-iteration {
//..,
break; // 1
//..,
continue; // 2
//..,
continue label1; // 3
//..,
break label1; // 4
}
}
F> ]
* ; *o,( _ g‰ > ] (*
,K g g (b F> ] <*
!”F*o ( *o ‰ ` 3 _ g
! | a currency1 ‰ ` > ] "*; ![,_ g ! A X,` ( t? J A _ ! e
# ] 0
//,c03:LabeledFor.java
// Java's "labeled for" loop,
import com.bruceeckel.simpletest.*;
public class LabeledFor {
static Test monitor = new Test();
public static void main(String[] args) {
int i = 0;
outer,// Can't have statements here
for(; true ;) { // infinite loop
inner,// Can't have statements here
for(; i < 10; i++) {
System.out.println("i = " + i);
shhgs@wgqqh.com
a0 41
a1 a2 48 a1
if(i == 2) {
System.out.println("continue");
continue;
}
if(i == 3) {
System.out.println("break");
i++; // Otherwise i never
// gets incremented,
break;
}
if(i == 7) {
System.out.println("continue outer");
i++; // Otherwise i never
// gets incremented,
continue outer;
}
if(i == 8) {
System.out.println("break outer");
break outer;
}
for(int k = 0; k < 5; k++) {
if(k == 3) {
System.out.println("continue inner");
continue inner;
}
}
}
}
// Can't break or continue to labels here
monitor.expect(new String[] {
"i = 0",
"continue inner",
"i = 1",
"continue inner",
"i = 2",
"continue",
"i = 3",
"break",
"i = 4",
"continue inner",
"i = 5",
"continue inner",
"i = 6",
"continue inner",
"i = 7",
"continue outer",
"i = 8",
"break outer"
});
}
} ///:~
3;,_ ! # X? 3? (# 1 X,0 ‘ ); * ^,h i · % F % %% %% %% % A( M (,1 F
B ( M (
shhgs@wgqqh.com
a0 42
a1 a2 48 a1
[ t,b l [ ( [ * ^,D E [ % (
ˉX;? 9 + _ g
(‰? ; ; _ ! _ ! b [ (
[ 3? |? ˉ_ ! ( % _ !? E
( (; F8 3 9 ( ˉ0
//,c03:LabeledWhile.java
// Java's "labeled while" loop,
import com.bruceeckel.simpletest.*;
public class LabeledWhile {
static Test monitor = new Test();
public static void main(String[] args) {
int i = 0;
outer,
while(true) {
System.out.println("Outer while loop");
while(true) {
i++;
System.out.println("i = " + i);
if(i == 1) {
System.out.println("continue");
continue;
}
if(i == 3) {
System.out.println("continue outer");
continue outer;
}
if(i == 5) {
System.out.println("break");
break;
}
if(i == 7) {
System.out.println("break outer");
break outer;
}
}
}
monitor.expect(new String[] {
"Outer while loop",
"i = 1",
"continue",
"i = 2",
"i = 3",
"continue outer",
"Outer while loop",
"i = 4",
"i = 5",
"break",
"Outer while loop",
"i = 6",
shhgs@wgqqh.com
a0 43
a1 a2 48 a1
"i = 7",
"break outer"
});
}
} ///:~
r s [ r )
F8 0
$ M (,_ g (b |
($ (,* g K ( t F? (
<$ ;,(?h ! + ˇ
4,( ;,‘ I ( (?h ! + ˇ Y,F; ˙? ( 3 ¢ ‘ FV m ( %; _ ! V [ F
) * Y ’ ˇ(n? G w x ¥ ( X
G (" j ("? q ( ‘ )
F ( - ’? t,( fl? U
J m c d A W (k m@? o ! ( X ; ) g ( ! ‰? m3 fl? mw ( _ W o !,(
p A | m,
m
F 8,T F 8,9 # "h i ·
- ˇ(L N O v w A £ A (x · 0
switch(integral-selector) {
case integral-value1,statement; break;
case integral-value2,statement; break;
case integral-value3,statement; break;
case integral-value4,statement; break;
case integral-value5,statement; break;
//,.,
default,statement;
}
" 3,J 0? "L (h i · F 8,L? (
N 1 8? ˉ? g,? ( A,>v (
) t ( b (? ˉ m? g? ( s
# (
shhgs@wgqqh.com
a0 44
a1 a2 48 a1
,FJ? Y?,(b? m ;? > y? u,* !
F 8 F 8 (3 x· X ^; (? ˉ m ;,3
(y? gA ; |? X,\ ; m( % ;,? b Y F
#? ( A m; g? Q X *?,_ g 3 r? ˉ? |,3? [ E P; k g # (b? 3 C ~ [ m
F 8 ; mu ¢ r? V V * 3
| A % 3 ; J 0 } 8? (? "L ( u T > C "1 u T Y Z
F 8 X ;? y1,¥ ) fl? "(e$? ; 3 a (
#? (,/ 0 3 | ` n o G W 6 6 0
//,c03:VowelsAndConsonants.java
// Demonstrates the switch statement,
import com.bruceeckel.simpletest.*;
public class VowelsAndConsonants {
static Test monitor = new Test();
public static void main(String[] args) {
for(int i = 0; i < 100; i++) {
char c = (char)(Math.random() * 26 + 'a');
System.out.print(c + ",");
switch(c) {
case 'a',
case 'e',
case 'i',
case 'o',
case 'u',System.out.println("vowel");
break;
case 'y',
case 'w',System.out.println("Sometimes a
vowel");
break;
default,System.out.println("consonant");
}
monitor.expect(new String[] {
"%% [aeiou],vowel|[yw],Sometimes a vowel|"
+
"[^aeiouyw],consonant"
},Test.AT_LEAST);
}
}
} ///:~
‘ )
8,0 ( 9g u 4(L? / Q E (′
h m(,| ` J g [ " b (Y
shhgs@wgqqh.com
a0 45
a1 a2 48 a1
| < J + W 9 # Q N O *? J F 8 ( (? "L F
t S3 9 Q ( F ) 8 ( M,…? "L
3,˙ 3 £? V x
ˇ(?v w; k FA (? % ( 2 s,^ + 3
(
( s h i · [1\h L ; [I J \,(3?z ˇ s h i · ( D E 3 ;?
*4(3 J 3 S | t? [
@ \ˇ ; &
@ | 0?/ @ $ ˇ Fb 3 ;? [
E\ a A h?X F ( ˇD E A h? fl 6 (
-,/-,/-,/-,/
u 0
char c = (char)(Math.random() * 26 + 'a');
L W? < <
8,0 3 D E (:,/ T =
` ( Q ) (O ˉ[ [ $ [
\ T = ; ( % e$ = P
(O ˉ = 8 =
8 (^ *m1,Z ′?[ $? ˉL
( $ 4 Y Z? PA * 8 ( M PA <9 ( ′,#? = > 0
//,c03:CastingNumbers.java
// What happens when you cast a float
// or double to an integral value?
import com.bruceeckel.simpletest.*;
public class CastingNumbers {
static Test monitor = new Test();
public static void main(String[] args) {
double
above = 0.7,
below = 0.4;
System.out.println("above," + above);
System.out.println("below," + below);
System.out.println("(int)above," + (int)above);
System.out.println("(int)below," + (int)below);
System.out.println("(char)('a' + above)," +
(char)('a' + above));
System.out.println("(char)('a' + below)," +
(char)('a' + below));
shhgs@wgqqh.com
a0 46
a1 a2 48 a1
monitor.expect(new String[] {
"above,0.7",
"below,0.4",
"(int)above,0",
"(int)below,0",
"(char)('a' + above),a",
"(char)('a' + below),a"
});
}
} ///:~
D E = >
# =? "( M A fl §? ( "{ +
c d
8,m A,0 3 9g u 4(L L 2 X 2 3 [
\? "F – A 9A F9A G
9A G F9A? 3 Sh 2 3 3 Sh?X 2 3 ˇ W ` 3? ¢ Q? = > 0
//,c03:RandomBounds.java
// Does Math.random() produce 0.0 and 1.0?
// {RunByHand}
public class RandomBounds {
static void usage() {
System.out.println("Usage,\n\t" +
"RandomBounds lower\n\tRandomBounds upper");
System.exit(1);
}
public static void main(String[] args) {
if(args.length != 1) usage();
if(args[0].equals("lower")) {
while(Math.random() != 0.0); // Keep trying
System.out.println("Produced 0.0!");
}
else if(args[0].equals("upper")) {
while(Math.random() != 1.0); // Keep trying
System.out.println("Produced 1.0!");
}
else
usage();
}
} ///:~
%? F? ' t 0
java RandomBounds lower
shhgs@wgqqh.com
a0 47
a1 a2 48 a1
java RandomBounds upper
n? A u? b? X X & D E < J +
8,c X,J 0 9$ 9[ X,0 $ 9( (3 ˉ?
a9 a10 a11 a12
9g u 4m(
a13 a14 X (L D E% ¢?Q L ;,B @ – 1 ¢?(,( #
8,”0 9$ 9 "F – A (L
F9A
3?',′ V "( m(G p 0@ 1 k /
R e$ = F T?,; ( 3 ¨? ¥ _? (,3? W,' £? | ¥ _ (f *? ¢ u e( ′ c d
% 3 ! L ;
u # $
# $ % & ’ (W? J? m3 ; d (= >
F (?k / R ˇ3 *me u h i · P e u h i · kg?+ E,G W,J 0 X (O ˉ
P T 3 ; (
ˇ? ˇe (e ˙ A n o X F "
u 4 2 3
3,],g (L (
,; ( ˙ A F ( M _ ! ` ¢,
Q £ "
3? e T "( ( 8 e
| PO ˉ],! Q T 8 ( M %
1 8 F i X ( N
3, 0 L ( n o L ′ ) ) - ) 3 0 (L
,; (? o ˇ( Q 2 # ; Q *o,
3 V m e [ ( Q $ " | PA ],! Q
D $ " y ; T A? R( | "
shhgs@wgqqh.com
a0 48
a1 a2 48 a1
3 ˙ A,],3 %? | P k (
E? ¢,/? ¢, m ( | P
&,` < < O ˉ”?
a9 a10 a15 a12
’ # * ’,( FH,,(9J
= 8 !7 H H T @ Y? –?,9 49 £
= 8 !7? 3 c ′? ( ; @ (' t · (W
(,? m? (;a h y "([
C\[ ′? ( Q m | ¥ "([ \e f ^ [
C\ ) t (h y " – ) § h " W (@ "/ # D (
9 Y % [ ;( ! B Q < gm,Z ^? ˉ < gy " m;( ! ("(,A E ; h ( Y 3 currency1 < gy "? ([
\ F 49 £ fl? ’ c d
= 8 !7 m,? 9( A X 3 m(? J? ˉ% h | ¥ "( W? "( X ^ [ ′? ( ˇ
a9 a10 a11 a12
’ * !? 0> C "#,; h ( z
"? ! 3 ! ! § *
h "? ( y z 9( ! (p "? b ′ (y"
b (y " H CCC 4 " 0
!<" AK !<" " K L AK " D E " E m( "
" !<" A !<" " ! " 3L "
" " 3!< ! " 3!< ! " 3L "
" 3!< ! " 3L
" 3M " 3L " (3 H
F h h § y " FF,9((A 9G,8
c 2 3 "[ 2 3? " D E h i · ( I " [ (E,(,
(E ( ′ (E,(,FF9A u 4 < < F
’ J I I @@@$ ’ $ I 99,$ ’ h ((b