Skip to content

Commit f592996

Browse files
committed
Rewrote Operators: Testing Object Equivalence
1 parent 451aca7 commit f592996

File tree

7 files changed

+123
-39
lines changed

7 files changed

+123
-39
lines changed

concurrent/PSP2.txt

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
0: main
22
1: main
3-
2: main
3+
1: ForkJoinPool.commonPool-worker-3
4+
1: ForkJoinPool.commonPool-worker-5
5+
3: ForkJoinPool.commonPool-worker-5
46
3: main
5-
4: main
7+
5: ForkJoinPool.commonPool-worker-5
68
5: main
7-
6: main
8-
6: ForkJoinPool.commonPool-worker-9
9-
7: main
10-
8: ForkJoinPool.commonPool-worker-9
11-
9: main
9+
7: ForkJoinPool.commonPool-worker-5
10+
8: main
11+
8: ForkJoinPool.commonPool-worker-3
12+
10: ForkJoinPool.commonPool-worker-5

operators/AllOps.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
// to show which ones are accepted by the Java compiler
77

88
public class AllOps {
9-
// To accept the results of a boolean test:
9+
// To accept the results of a Boolean test:
1010
void f(boolean b) {}
1111
void boolTest(boolean x, boolean y) {
1212
// Arithmetic operators:

operators/AutoInc.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// (c)2021 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
5-
// Demonstrates the ++ and -- operators
5+
// The ++ and -- operators
66

77
public class AutoInc {
88
public static void main(String[] args) {

operators/DoubleEquivalence.java

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
// operators/DoubleEquivalence.java
2+
// (c)2021 MindView LLC: see Copyright.txt
3+
// We make no guarantees that this code is fit for any purpose.
4+
// Visit http://OnJava8.com for more book information.
5+
6+
public class DoubleEquivalence {
7+
static void show(String desc, Double n1, Double n2) {
8+
System.out.println(desc + ":");
9+
System.out.printf(
10+
"%e==%e %b %b%n", n1, n2, n1 == n2, n1.equals(n2));
11+
}
12+
@SuppressWarnings("deprecation")
13+
public static void test(double x1, double x2) {
14+
// x1.equals(x2) // Won't compile
15+
System.out.printf("%e==%e %b%n", x1, x2, x1 == x2);
16+
Double d1 = x1;
17+
Double d2 = x2;
18+
show("Automatic", d1, d2);
19+
Double r1 = new Double(x1);
20+
Double r2 = new Double(x2);
21+
show("new Double()", r1, r2);
22+
Double v1 = Double.valueOf(x1);
23+
Double v2 = Double.valueOf(x2);
24+
show("Double.valueOf()", v1, v2);
25+
}
26+
public static void main(String[] args) {
27+
test(0, Double.MIN_VALUE);
28+
test(Double.MAX_VALUE,
29+
Double.MAX_VALUE - Double.MIN_VALUE * 1_000_000);
30+
}
31+
}
32+
/* Output:
33+
0.000000e+00==4.900000e-324 false
34+
Automatic:
35+
0.000000e+00==4.900000e-324 false false
36+
new Double():
37+
0.000000e+00==4.900000e-324 false false
38+
Double.valueOf():
39+
0.000000e+00==4.900000e-324 false false
40+
1.797693e+308==1.797693e+308 true
41+
Automatic:
42+
1.797693e+308==1.797693e+308 false true
43+
new Double():
44+
1.797693e+308==1.797693e+308 false true
45+
Double.valueOf():
46+
1.797693e+308==1.797693e+308 false true
47+
*/

operators/EqualsMethod.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,34 @@
22
// (c)2021 MindView LLC: see Copyright.txt
33
// We make no guarantees that this code is fit for any purpose.
44
// Visit http://OnJava8.com for more book information.
5+
// Default equals() does not compare contents
6+
7+
class ValA {
8+
int i;
9+
}
10+
11+
class ValB {
12+
int i;
13+
// Works for this example, not a complete equals():
14+
public boolean equals(Object o) {
15+
ValB rval = (ValB)o; // Cast o to be a ValB
16+
return i == rval.i;
17+
}
18+
}
519

620
public class EqualsMethod {
721
public static void main(String[] args) {
8-
Integer n1 = 47;
9-
Integer n2 = 47;
10-
System.out.println(n1.equals(n2));
22+
ValA va1 = new ValA();
23+
ValA va2 = new ValA();
24+
va1.i = va2.i = 100;
25+
System.out.println(va1.equals(va2));
26+
ValB vb1 = new ValB();
27+
ValB vb2 = new ValB();
28+
vb1.i = vb2.i = 100;
29+
System.out.println(vb1.equals(vb2));
1130
}
1231
}
1332
/* Output:
33+
false
1434
true
1535
*/

operators/EqualsMethod2.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

operators/Equivalence.java

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,51 @@
44
// Visit http://OnJava8.com for more book information.
55

66
public class Equivalence {
7+
static void show(String desc, Integer n1, Integer n2) {
8+
System.out.println(desc + ":");
9+
System.out.printf(
10+
"%d==%d %b %b%n", n1, n2, n1 == n2, n1.equals(n2));
11+
}
12+
@SuppressWarnings("deprecation")
13+
public static void test(int value) {
14+
Integer i1 = value; // [1]
15+
Integer i2 = value;
16+
show("Automatic", i1, i2);
17+
// Old way, deprecated in Java 9 and on:
18+
Integer r1 = new Integer(value); // [2]
19+
Integer r2 = new Integer(value);
20+
show("new Integer()", r1, r2);
21+
// Preferred in Java 9 and on:
22+
Integer v1 = Integer.valueOf(value); // [3]
23+
Integer v2 = Integer.valueOf(value);
24+
show("Integer.valueOf()", v1, v2);
25+
// Primitives can't use equals():
26+
int x = value; // [4]
27+
int y = value;
28+
// x.equals(y); // Doesn't compile
29+
System.out.println("Primitive int:");
30+
System.out.printf("%d==%d %b%n", x, y, x == y);
31+
}
732
public static void main(String[] args) {
8-
Integer n1 = 47;
9-
Integer n2 = 47;
10-
System.out.println(n1 == n2);
11-
System.out.println(n1 != n2);
33+
test(127);
34+
test(128);
1235
}
1336
}
1437
/* Output:
15-
true
16-
false
38+
Automatic:
39+
127==127 true true
40+
new Integer():
41+
127==127 false true
42+
Integer.valueOf():
43+
127==127 true true
44+
Primitive int:
45+
127==127 true
46+
Automatic:
47+
128==128 false true
48+
new Integer():
49+
128==128 false true
50+
Integer.valueOf():
51+
128==128 false true
52+
Primitive int:
53+
128==128 true
1754
*/

0 commit comments

Comments
 (0)