Forums Register Login

simple problem of == & equals( )

+Pie Number of slices to send: Send
Integer i = new Integer(1);
Integer i1 = new Integer("1");
System.out.println(i.equals(i1)); // true
System.out.println(i==i1); // false

Why first one is giving true ... as i know that equals() method check if both object are equal or not . so one is string object & one is integer object . one is on string constant pool & one is on integer constant pool ( i am not sure in this , may be GCH ) ... so how can both equal ...
please help ...

second one is clear ...

thanks a lot .
+Pie Number of slices to send: Send
But one is not a String object, just another Integer object created from a String, no?
+Pie Number of slices to send: Send
If you look in the javadocs Integer(String) can throw a NumberFormatException. This is because that constructor attempts to parse an Integer from the String. In the code you are compairing two integers with the same value of 1 so they are equal(). But they do not reference the same Object so they are not ==.
+Pie Number of slices to send: Send
A good rule is that every time you use new, you get a unique new object that will not == any other object.

Note that while two String.equals() and Integer.equals() check for same actual classes and values, StringBuffer inherits Object.equals() and checks for identical objects.
+Pie Number of slices to send: Send
I think the equals method is overridden in the Interger class to check if Integer objects equality.
+Pie Number of slices to send: Send
Guys not clear
+Pie Number of slices to send: Send
The equals() methods is overridden in all wrapper classes to compare value equality instead of reference equality.

Integer i = new Integer(1);
Integer i1 = new Integer("1");

The Integer class wraps the value of primitive int in an object regardless of which constructor you have used.
Whether you pass '1' as an int or String to the constructor the final stored value in the wrapper class will be the primitive '1'.

Note: if you use the constructor that accepts String as a parameter to pass a value, then the compiler implicitly performs Integer.parseInt() on the passed parameter and raises java.lang.NumberFormatException if it was not able to convert the String value to primitive '1'.
+Pie Number of slices to send: Send
One more thing, equals() method always returns false if you compare the equality of two different wrapper classes (ie. Integer and Long) even if they are holding the same value.


Here is Sun's implementation of the equals() method in the Integer class.
+Pie Number of slices to send: Send
Thanks Vicken & all , thank you vary much .
+Pie Number of slices to send: Send
Integer i1 = new Integer('2');
Integer i2 = new Integer("2");
System.out.println(i1.equals(i2));

why this code is giving false ...
please help ...
thanks a lot .
+Pie Number of slices to send: Send
 

Originally posted by rathi ji:
Integer i1 = new Integer('2');
Integer i2 = new Integer("2");
System.out.println(i1.equals(i2));

why this code is giving false ...
please help ...
thanks a lot .[/QUOTE

Only values that can be passed to Integer is a primitive int,long,short,byte.
If you pass char then also i doesnt throw any error rather the output will be a different number.So the output of i1=50
If you pass a String then parseInteger()is invoked and output is a primitive with same value.and output of i2=2.
Henceforth equal method returns false.

 
Every time you till, you lose 30% of your organic matter. But this tiny ad is durable:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 849 times.
Similar Threads
Help me with this Wrapper class code
Autoboxing vs Unboxing
Creation of Ineger Wrapper Object
Boxing and equals operators
wrapper objects. equals and ==
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 12:22:57.