Contents [0/23] |
Video [1/23] |
Objects and equality [2/23] |
Equals Operator and Equals Method [3/23] |
Null on the Right [4/23] |
Null on the Left [5/23] |
Object [6/23] |
String Literal [7/23] |
String Constructor [8/23] |
String Operators [9/23] |
String Interning [10/23] |
Base Type [11/23] |
Boxing [12/23] |
Unboxing [13/23] |
Boxing [14/23] |
Integers Close to Zero [15/23] |
Integers Far from Zero [16/23] |
Integer Boxing with valueOf [17/23] |
Integer Constructor (Deprecated) [18/23] |
Copying a Reference [19/23] |
Arrays of Base Values [20/23] |
Arrays of Objects [21/23] |
Arrays of Arrays of Base Values [22/23] |
Arrays of Arrays of Objects [23/23] |
(Click here for one slide per page)
Video [1/23] |
This lecture is semi-optional. You should watch it, but it will not be on exams.
Objects and equality [2/23] |
int[] x = new int[] { 11, 21, 31 }; int[] y = new int[] { 11, 21, 31 }; x=[I@8807e25, y=[I@2a3046da x=[11, 21, 31], y=[11, 21, 31] x==y : false Objects.equals(x,y) : false x.equals(y) : false Arrays.equals(x,y) : true |
file:Hello.java [source] [doc-public] [doc-private]
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
package algs12; import stdlib.*; import java.util.*; public class Hello { public static void main (String[] args) { Trace.showBuiltInObjects (true); Trace.run (); int[] x = new int[] { 11, 21, 31 }; int[] y = new int[] { 11, 21, 31 }; Trace.draw (); StdOut.println ("x=" + x + ", y=" + y); StdOut.println ("x=" + Arrays.toString(x) + ", y=" + Arrays.toString(y)); StdOut.println (" x==y : " + (x == y)); StdOut.println (" Objects.equals(x,y) : " + (Objects.equals(x,y))); StdOut.println (" x.equals(y) : " + (x.equals(y))); StdOut.println (" Arrays.equals(x,y) : " + (Arrays.equals(x,y))); } }
Equals Operator and Equals Method [3/23] |
In Java, ==
checks object identity
on object types
Unlike other languages (such as C++) this behavior cannot be changed.
Objects all have an equals
method.
equals
varies from class to class.
==
The java.util.Objects class provides some handy
utilities, like Objects.equals
Navigate > Open Declaration
Null on the Right [4/23] |
Integer x = 3000; Integer y = null; x=3000, y=null x==y : false Objects.equals(x,y) : false x.equals(y) : false |
Null on the Left [5/23] |
Integer x = null; Integer y = 3000; x=null, y=3000 x==y : false Objects.equals(x,y) : false Exception in thread "main" java.lang.NullPointerException at algs12.Hello.main(Hello.java:15) |
Object [6/23] |
Try java.lang.Object
Object x = new Object(); Object y = new Object(); x=java.lang.Object@8807e25, y=java.lang.Object@2a3046da x==y : false Objects.equals(x,y) : false x.equals(y) : false |
String Literal [7/23] |
Try java.lang.String
String x = "Hello"; String y = "Hello"; x=Hello, y=Hello x==y : true Objects.equals(x,y) : true x.equals(y) : true |
String Constructor [8/23] |
String x = new String ("Hello"); String y = new String ("Hello"); x=Hello, y=Hello x==y : false Objects.equals(x,y) : true x.equals(y) : true |
String Operators [9/23] |
String x = "Hel" + "lo"; String y = "Hel" + "lo"; x=Hello, y=Hello x==y : true Objects.equals(x,y) : true x.equals(y) : true |
String Interning [10/23] |
String a = new String ("Hello"); String b = new String ("Hello"); String x = a.intern(); String y = b.intern(); x=Hello, y=Hello x==y : true Objects.equals(x,y) : true x.equals(y) : true |
Base Type [11/23] |
int x = 3000; int y = 3000; x=3000, y=3000 x==y : true |
Boxing [12/23] |
Integer x = 3000; Integer y = 3000; x=3000, y=3000 x==y : false Objects.equals(x,y) : true x.equals(y) : true |
Unboxing [13/23] |
Integer a = 3000; Integer b = 3000; int x = a.intValue(); int y = b.intValue(); x=3000, y=3000 x==y : true |
Boxing [14/23] |
Conversion from base type to object type is called boxing
Conversion from object type to base type is called unboxing
For int
and double
, the operations are as follows
Base type | Object type | Boxing (base to object) | Unboxing (object to base) |
---|---|---|---|
int base = 0; |
Integer object = null; |
object = Integer.valueOf(base); |
base = object.intValue(); |
double base = 0.0; |
Double object = null; |
object = Double.valueOf(base); |
base = object.doubleValue(); |
Java has six additional base types:
Base type | Object type | Boxing (base to object) | Unboxing (object to base) |
---|---|---|---|
boolean base = false; |
Boolean object = null; |
object = Boolean.valueOf(base); |
base = object.booleanValue(); |
float base = 0.0F; |
Float object = null; |
object = Float.valueOf(base); |
base = object.floatValue(); |
byte base = 0; |
Byte object = null; |
object = Byte.valueOf(base); |
base = object.byteValue(); |
char base = 0; |
Character object = null; |
object = Character.valueOf(base); |
base = object.charValue(); |
short base = 0; |
Short object = null; |
object = Short.valueOf(base); |
base = object.shortValue(); |
long base = 0L; |
Long object = null; |
object = Long.valueOf(base); |
base = object.longValue(); |
Many languages make an explicit distinction between object and base types
struct
)
Integers Close to Zero [15/23] |
Integer x = 30; Integer y = 30; x=30, y=30 x==y : true Objects.equals(x,y) : true x.equals(y) : true |
Integers Far from Zero [16/23] |
Integer x = 3000; Integer y = 3000; x=3000, y=3000 x==y : false Objects.equals(x,y) : true x.equals(y) : true |
Integer Boxing with valueOf [17/23] |
Integer x = Integer.valueOf(30); Integer y = Integer.valueOf(30); x=30, y=30 x==y : true Objects.equals(x,y) : true x.equals(y) : true |
Integer Constructor (Deprecated) [18/23] |
Integer x = new Integer(30); Integer y = new Integer(30); x=30, y=30 x==y : false Objects.equals(x,y) : true x.equals(y) : true |
Copying a Reference [19/23] |
Integer x = 3000; Integer y = x; x=3000, y=3000 x==y : true Objects.equals(x,y) : true x.equals(y) : true |
Arrays of Base Values [20/23] |
Try arrays, with utility functions in java.util.Arrays
int[] x = new int[] { 11, 21, 31 }; int[] y = new int[] { 11, 21, 31 }; x=[I@8807e25, y=[I@2a3046da x=[11, 21, 31], y=[11, 21, 31] x==y : false Objects.equals(x,y) : false x.equals(y) : false Arrays.equals(x,y) : true |
Arrays of Objects [21/23] |
Integer[] x = new Integer[] { 11, 21, 31 }; Integer[] y = new Integer[] { 11, 21, 31 }; x=[Ljava.lang.Integer;@8807e25, y=[Ljava.lang.Integer;@2a3046da x=[11, 21, 31], y=[11, 21, 31] x==y : false Objects.equals(x,y) : false x.equals(y) : false Arrays.equals(x,y) : true |
Arrays of Arrays of Base Values [22/23] |
int[][] x = new int[][] { { 11, 21, 31 }, { 12, 22, 32 } }; int[][] y = new int[][] { { 11, 21, 31 }, { 12, 22, 32 } }; x=[[I@8807e25, y=[[I@2a3046da x=[[I@12f40c25, [I@3ada9e37], y=[[I@5cbc508c, [I@3419866c] x==y : false Objects.equals(x,y) : false x.equals(y) : false Arrays.equals(x,y) : false Arrays.deepEquals(x,y) : true |
Arrays of Arrays of Objects [23/23] |
Integer[][] x = new Integer[][] { { 11, 21, 31 }, { 12, 22, 32 } }; Integer[][] y = new Integer[][] { { 11, 21, 31 }, { 12, 22, 32 } }; x=[[Ljava.lang.Integer;@8807e25, y=[[Ljava.lang.Integer;@2a3046da x=[[Ljava.lang.Integer;@12f40c25, [Ljava.lang.Integer;@3ada9e37], y=[[Ljava.lang.Integer;@5cbc508c, [Ljava.lang.Integer;@3419866c] x==y : false Objects.equals(x,y) : false x.equals(y) : false Arrays.equals(x,y) : false Arrays.deepEquals(x,y) : true |
Revised: 2008/03/17 13:01