Rewrote Operators: Testing Object Equivalence
This commit is contained in:
parent
451aca780a
commit
f5929969b1
@ -1,11 +1,12 @@
|
|||||||
0: main
|
0: main
|
||||||
1: main
|
1: main
|
||||||
2: main
|
1: ForkJoinPool.commonPool-worker-3
|
||||||
|
1: ForkJoinPool.commonPool-worker-5
|
||||||
|
3: ForkJoinPool.commonPool-worker-5
|
||||||
3: main
|
3: main
|
||||||
4: main
|
5: ForkJoinPool.commonPool-worker-5
|
||||||
5: main
|
5: main
|
||||||
6: main
|
7: ForkJoinPool.commonPool-worker-5
|
||||||
6: ForkJoinPool.commonPool-worker-9
|
8: main
|
||||||
7: main
|
8: ForkJoinPool.commonPool-worker-3
|
||||||
8: ForkJoinPool.commonPool-worker-9
|
10: ForkJoinPool.commonPool-worker-5
|
||||||
9: main
|
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
// to show which ones are accepted by the Java compiler
|
// to show which ones are accepted by the Java compiler
|
||||||
|
|
||||||
public class AllOps {
|
public class AllOps {
|
||||||
// To accept the results of a boolean test:
|
// To accept the results of a Boolean test:
|
||||||
void f(boolean b) {}
|
void f(boolean b) {}
|
||||||
void boolTest(boolean x, boolean y) {
|
void boolTest(boolean x, boolean y) {
|
||||||
// Arithmetic operators:
|
// Arithmetic operators:
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
// (c)2021 MindView LLC: see Copyright.txt
|
// (c)2021 MindView LLC: see Copyright.txt
|
||||||
// We make no guarantees that this code is fit for any purpose.
|
// We make no guarantees that this code is fit for any purpose.
|
||||||
// Visit http://OnJava8.com for more book information.
|
// Visit http://OnJava8.com for more book information.
|
||||||
// Demonstrates the ++ and -- operators
|
// The ++ and -- operators
|
||||||
|
|
||||||
public class AutoInc {
|
public class AutoInc {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
47
operators/DoubleEquivalence.java
Normal file
47
operators/DoubleEquivalence.java
Normal file
@ -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
|
||||||
|
*/
|
@ -2,14 +2,34 @@
|
|||||||
// (c)2021 MindView LLC: see Copyright.txt
|
// (c)2021 MindView LLC: see Copyright.txt
|
||||||
// We make no guarantees that this code is fit for any purpose.
|
// We make no guarantees that this code is fit for any purpose.
|
||||||
// Visit http://OnJava8.com for more book information.
|
// 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 class EqualsMethod {
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
Integer n1 = 47;
|
ValA va1 = new ValA();
|
||||||
Integer n2 = 47;
|
ValA va2 = new ValA();
|
||||||
System.out.println(n1.equals(n2));
|
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:
|
/* Output:
|
||||||
|
false
|
||||||
true
|
true
|
||||||
*/
|
*/
|
||||||
|
@ -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
|
|
||||||
*/
|
|
@ -4,14 +4,51 @@
|
|||||||
// Visit http://OnJava8.com for more book information.
|
// Visit http://OnJava8.com for more book information.
|
||||||
|
|
||||||
public class Equivalence {
|
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) {
|
public static void main(String[] args) {
|
||||||
Integer n1 = 47;
|
test(127);
|
||||||
Integer n2 = 47;
|
test(128);
|
||||||
System.out.println(n1 == n2);
|
|
||||||
System.out.println(n1 != n2);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
/* Output:
|
/* Output:
|
||||||
true
|
Automatic:
|
||||||
false
|
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
|
||||||
*/
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user