Hi I tried out the following code StringBuffer sb1 = new StringBuffer("xyz"); StringBuffer sb2 = new StringBuffer("xyz"); System.out.println(sb1.equals(sb2));//output is false Float f1 = new Float("0.9f"); Float f2 = new Float("0.9f"); System.out.println(f1.equals(f2));//output is true Can anyone explain me exactly what is happening in the above code, as we are using equals()method in both cases... Why the output is different in both cases... Cheers Ravi
The StringBuffer class does not override the "equals" method, so equality means identity. The two StringBuffer references are not to the same object on the heap, therefore the objects are not "equal". The Float class overrides the "equals" method. Two float objects are "equal" if the floatToIntBits() method returns the same int value for each. Since the two Float objects are created with identical constructor calls, they pass the test.
permaculture is giving a gift to your future self. After reading this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop