From f5929969b128fe1832ec90701cfc4f5f1a7b2cdf Mon Sep 17 00:00:00 2001 From: Bruce Eckel Date: Thu, 4 Feb 2021 07:21:51 -0700 Subject: [PATCH] Rewrote Operators: Testing Object Equivalence --- concurrent/PSP2.txt | 15 +++++----- operators/AllOps.java | 2 +- operators/AutoInc.java | 2 +- operators/DoubleEquivalence.java | 47 ++++++++++++++++++++++++++++++ operators/EqualsMethod.java | 26 +++++++++++++++-- operators/EqualsMethod2.java | 21 -------------- operators/Equivalence.java | 49 ++++++++++++++++++++++++++++---- 7 files changed, 123 insertions(+), 39 deletions(-) create mode 100644 operators/DoubleEquivalence.java delete mode 100644 operators/EqualsMethod2.java diff --git a/concurrent/PSP2.txt b/concurrent/PSP2.txt index 22443388..491bd153 100644 --- a/concurrent/PSP2.txt +++ b/concurrent/PSP2.txt @@ -1,11 +1,12 @@ 0: main 1: main -2: main +1: ForkJoinPool.commonPool-worker-3 +1: ForkJoinPool.commonPool-worker-5 +3: ForkJoinPool.commonPool-worker-5 3: main -4: main +5: ForkJoinPool.commonPool-worker-5 5: main -6: main -6: ForkJoinPool.commonPool-worker-9 -7: main -8: ForkJoinPool.commonPool-worker-9 -9: main +7: ForkJoinPool.commonPool-worker-5 +8: main +8: ForkJoinPool.commonPool-worker-3 +10: ForkJoinPool.commonPool-worker-5 diff --git a/operators/AllOps.java b/operators/AllOps.java index 3a559a7d..d48e82af 100644 --- a/operators/AllOps.java +++ b/operators/AllOps.java @@ -6,7 +6,7 @@ // to show which ones are accepted by the Java compiler public class AllOps { - // To accept the results of a boolean test: + // To accept the results of a Boolean test: void f(boolean b) {} void boolTest(boolean x, boolean y) { // Arithmetic operators: diff --git a/operators/AutoInc.java b/operators/AutoInc.java index d018f90a..bcb09093 100644 --- a/operators/AutoInc.java +++ b/operators/AutoInc.java @@ -2,7 +2,7 @@ // (c)2021 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. -// Demonstrates the ++ and -- operators +// The ++ and -- operators public class AutoInc { public static void main(String[] args) { diff --git a/operators/DoubleEquivalence.java b/operators/DoubleEquivalence.java new file mode 100644 index 00000000..e0016038 --- /dev/null +++ b/operators/DoubleEquivalence.java @@ -0,0 +1,47 @@ +// operators/DoubleEquivalence.java +// (c)2021 MindView LLC: see Copyright.txt +// We make no guarantees that this code is fit for any purpose. +// Visit http://OnJava8.com for more book information. + +public class DoubleEquivalence { + static void show(String desc, Double n1, Double n2) { + System.out.println(desc + ":"); + System.out.printf( + "%e==%e %b %b%n", n1, n2, n1 == n2, n1.equals(n2)); + } + @SuppressWarnings("deprecation") + public static void test(double x1, double x2) { + // x1.equals(x2) // Won't compile + System.out.printf("%e==%e %b%n", x1, x2, x1 == x2); + Double d1 = x1; + Double d2 = x2; + show("Automatic", d1, d2); + Double r1 = new Double(x1); + Double r2 = new Double(x2); + show("new Double()", r1, r2); + Double v1 = Double.valueOf(x1); + Double v2 = Double.valueOf(x2); + show("Double.valueOf()", v1, v2); + } + public static void main(String[] args) { + test(0, Double.MIN_VALUE); + test(Double.MAX_VALUE, + Double.MAX_VALUE - Double.MIN_VALUE * 1_000_000); + } +} +/* Output: +0.000000e+00==4.900000e-324 false +Automatic: +0.000000e+00==4.900000e-324 false false +new Double(): +0.000000e+00==4.900000e-324 false false +Double.valueOf(): +0.000000e+00==4.900000e-324 false false +1.797693e+308==1.797693e+308 true +Automatic: +1.797693e+308==1.797693e+308 false true +new Double(): +1.797693e+308==1.797693e+308 false true +Double.valueOf(): +1.797693e+308==1.797693e+308 false true +*/ diff --git a/operators/EqualsMethod.java b/operators/EqualsMethod.java index 7915f7de..2f281aeb 100644 --- a/operators/EqualsMethod.java +++ b/operators/EqualsMethod.java @@ -2,14 +2,34 @@ // (c)2021 MindView LLC: see Copyright.txt // We make no guarantees that this code is fit for any purpose. // Visit http://OnJava8.com for more book information. +// Default equals() does not compare contents + +class ValA { + int i; +} + +class ValB { + int i; + // Works for this example, not a complete equals(): + public boolean equals(Object o) { + ValB rval = (ValB)o; // Cast o to be a ValB + return i == rval.i; + } +} public class EqualsMethod { public static void main(String[] args) { - Integer n1 = 47; - Integer n2 = 47; - System.out.println(n1.equals(n2)); + ValA va1 = new ValA(); + ValA va2 = new ValA(); + va1.i = va2.i = 100; + System.out.println(va1.equals(va2)); + ValB vb1 = new ValB(); + ValB vb2 = new ValB(); + vb1.i = vb2.i = 100; + System.out.println(vb1.equals(vb2)); } } /* Output: +false true */ diff --git a/operators/EqualsMethod2.java b/operators/EqualsMethod2.java deleted file mode 100644 index 75ff42c5..00000000 --- a/operators/EqualsMethod2.java +++ /dev/null @@ -1,21 +0,0 @@ -// operators/EqualsMethod2.java -// (c)2021 MindView LLC: see Copyright.txt -// We make no guarantees that this code is fit for any purpose. -// Visit http://OnJava8.com for more book information. -// Default equals() does not compare contents - -class Value { - int i; -} - -public class EqualsMethod2 { - 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)); - } -} -/* Output: -false -*/ diff --git a/operators/Equivalence.java b/operators/Equivalence.java index 603ef631..25f7f3e7 100644 --- a/operators/Equivalence.java +++ b/operators/Equivalence.java @@ -4,14 +4,51 @@ // Visit http://OnJava8.com for more book information. public class Equivalence { + static void show(String desc, Integer n1, Integer n2) { + System.out.println(desc + ":"); + System.out.printf( + "%d==%d %b %b%n", n1, n2, n1 == n2, n1.equals(n2)); + } + @SuppressWarnings("deprecation") + public static void test(int value) { + Integer i1 = value; // [1] + Integer i2 = value; + show("Automatic", i1, i2); + // Old way, deprecated in Java 9 and on: + Integer r1 = new Integer(value); // [2] + Integer r2 = new Integer(value); + show("new Integer()", r1, r2); + // Preferred in Java 9 and on: + Integer v1 = Integer.valueOf(value); // [3] + Integer v2 = Integer.valueOf(value); + show("Integer.valueOf()", v1, v2); + // Primitives can't use equals(): + int x = value; // [4] + int y = value; + // x.equals(y); // Doesn't compile + System.out.println("Primitive int:"); + System.out.printf("%d==%d %b%n", x, y, x == y); + } public static void main(String[] args) { - Integer n1 = 47; - Integer n2 = 47; - System.out.println(n1 == n2); - System.out.println(n1 != n2); + test(127); + test(128); } } /* Output: -true -false +Automatic: +127==127 true true +new Integer(): +127==127 false true +Integer.valueOf(): +127==127 true true +Primitive int: +127==127 true +Automatic: +128==128 false true +new Integer(): +128==128 false true +Integer.valueOf(): +128==128 false true +Primitive int: +128==128 true */