The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes == operator.... Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "== operator...." Watch "== operator...." New topic

== operator....

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362

output is:false false true

a,b,c,d are the objects which referenced are in string constant pool..

so c and d are refering to the same object as well as (a+b) also refers to the same object..

then why 1st and 2nd is false....?

SCJP5 and SCWCD1.5
Think Twice Act Wise...
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952

c = a+b, d = a+b

both time new object created by JVM, so they are different.

Ganeshkumar cheekati
Ranch Hand

Joined: Oct 13, 2008
Posts: 362
but after creating object d it check whether it is available in string constant pool or not it is available so object d also pointing to the same which is referenced by object c.javascript: x()
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

I am not sure to what extent i am right. But i think i will share it.
when we say, c=a+b, it will call,
concat() of string class
append() of StringBuffer class(internally it would look like this:- new StringBuilder().append(a).append(b).toString(); )

( not sure which one of these)

Where in both of them returns a new string.

[ December 11, 2008: Message edited by: James Tharakan ]
[ December 11, 2008: Message edited by: James Tharakan ]

Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Whenever we make a String object by doing some operation on the Strings then we will always end up with new String Object.

String class has overridden hashCode() and equals() method so meaningfully the String objects may be equal and they will return the same hash code but then also they are situated at different memory location in the Objcet heap.

The default implementation of hashCode() provided by Object is derived by mapping the memory address of the object to an integer value. Because on some architectures the address space is larger than the range of values for int, it is possible that two distinct objects could have the same hashCode(). If you override hashCode(), you can still use the System.identityHashCode() method to access this default value.

The following example which I prepared by altering the above question will simplify my point.

The output of the above code is

So it is visible that whenever we make some String object by doing some alteration we will always end up in a new Object in heap. So the system HashCode will be different for them as no two Objects can share the same memory.

The hashcodes we get from the String Objects return the hashcode generated by this function as it is overridden in String class.

To get the actaul hashcode we used the static function identityHashCode() of System class.

The system hashcode for str1 and str2 are same as those objects were created without any alteration on any of the objects.

My Blog SCJP 5 SCWCD 5
I agree. Here's the link:
subject: == operator....
It's not a secret anymore!