Comparing references through “==”

Each Java object has an identity hash code. This can be read by Object.hashCode or in the case that this method overridden, by System.identityHashCode. The default hash code is usually the location in memory of the object.

When we use “==” operator on Object References then  System.identityHashCode(object) is used for evaluation.

If the identityHashCode() is different, the references definitely do not point to the same object. In that case  obj1==obj2 will return false.

If the identityHashCode() is the same and obj1==obj2, the references probably do point to the same object. But they might not, because you are getting a 32-bit hash code. Obviously, you cannot have different 32-bit hash codes for every possible address in a 64-bit system. But you are not allowed to assume, even on a 32-bit system, that same hash code definitely means identical object.

A word on Strings :

String s1 = “jim”;
String s2 = “jim”;

System.out.println(s1==s2); // true.

Object s2 is the same object with s1. But if you create using new operator:

String s1 = “jim”;
String s2 = new String(“jim”);
System.out.println(s1==s2); //false.

It will allocate new Object instead getting from String constant pool.

String s1 = “jim”;
String s2 = “j”+”im”;
String s3 = “j”;
String im = “im”;
s3+= im;
System.out.println(s1==s2); //true.
System.out.println(s1==s3); //false.

Concatenation during runtime will create new String Object.
Sample if using final modifier:

final String s1 = “j”;
final String s2 = “im”;
String jim = “jim”;
System.out.println(s1+s2 == jim); // returns true: Constant expression.

String s3 = “j”;
String s4 = “im”;
System.out.println(s3 + s4 == jim); // returns false: Not a constant expression.

Concatenation of 2 final String is a Constant.

String s1 = “jim”;
String s2 = “j”+”im”;
String s3 = “j”;
String im = “im”;
s3+= im;
System.out.println(s1==s2); //true.
System.out.println(s1==s3); //false.

Concatenation during runtime will create new String Object.
Sample if using final modifier:

final String s1 = “j”;
final String s2 = “im”;
String jim = “jim”;
System.out.println(s1+s2 == jim); // returns true: Constant expression.

String s3 = “j”;
String s4 = “im”;
System.out.println(s3 + s4 == jim); // returns false: Not a constant expression.

Concatenation of 2 final String is a Constant.

A word on toString :

When we try to print a object using  System.out.print(obj) it will print the output according to the implemetation of  toString method for that class.

The defautl implementation of toString method :

“The toString method for class Object returns a string consisting of the name
of the class of which the object is an instance, the at-sign character `@’,
and the unsigned hexadecimal representation of the hash code of the object. In
other words, this method returns a string equal to the value of:

getClass().getName() + ‘@’ + Integer.toHexString(hashCode())”

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s