Equals Method
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
public class Invoke
{
public static void main(String[] args)
{
int i1=4000;
int i2=4000;
System.out.println(i1==i2);
new Invoke().go(i1,i2);
}
public void go(Integer i1, Integer i2)
{
System.out.println(i1==i2);
}
}
The answer is true false.
But when i1=2; i2=2; then answer is true.
Why so?
{
public static void main(String[] args)
{
int i1=4000;
int i2=4000;
System.out.println(i1==i2);
new Invoke().go(i1,i2);
}
public void go(Integer i1, Integer i2)
{
System.out.println(i1==i2);
}
}
The answer is true false.
But when i1=2; i2=2; then answer is true.
Why so?
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Abhi Vijay :
In case of go() method primitive int is auto-boxed to wrappered Integer.
It will create new objects, for new objects == will return false as their memory location would be different, whereas if you try .equals() method it will return true
In case of go() method primitive int is auto-boxed to wrappered Integer.
It will create new objects, for new objects == will return false as their memory location would be different, whereas if you try .equals() method it will return true
Regards,<br />BulletProof Monk.<br /> <br />"Nthing the Mind of man can Conceive<br />and Believe,It can achieve"
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Well I think,
: For ASCII value (-128 to 127), it will compare it as an value (so gives true).
: For other value, it will pass it as an object as shown in the code (So gives false).
: For ASCII value (-128 to 127), it will compare it as an value (so gives true).
: For other value, it will pass it as an object as shown in the code (So gives false).
Pranav Patel
Greenhorn
Posts: 6
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Sorry for the previous post, it is not readable..
i1=2, i2=2: For ASCII value (int -128 to 127), it will compare it as an int value (so gives true).
i1=4000, i2=4000: For other int value, it will pass it as an Integer object as shown in the code (So gives false).
Auto boxing for ASCII value for int works different here.
i1=2, i2=2: For ASCII value (int -128 to 127), it will compare it as an int value (so gives true).
i1=4000, i2=4000: For other int value, it will pass it as an Integer object as shown in the code (So gives false).
Auto boxing for ASCII value for int works different here.
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Look code of Integer.valueOf() method in Integer class. You will get your answer.
If you call with i1=2, i2=2, then JVM will not going to create new Integer object. Why?
Here Integer.valueOf(2) method is get called not new Integer(2); And this method has already created a cache for Integer object of 256 size that will contain all Integer object starting from -128 to 127.
From -128 to 127, no new Integer objects are created, as they are already created and cached.
If you call with i1=2, i2=2, then JVM will not going to create new Integer object. Why?
Here Integer.valueOf(2) method is get called not new Integer(2); And this method has already created a cache for Integer object of 256 size that will contain all Integer object starting from -128 to 127.
From -128 to 127, no new Integer objects are created, as they are already created and cached.
SCJP 6
Abhi vijay
Ranch Hand
Posts: 509
posted 16 years ago
-
-
Number of slices to send:Optional 'thank-you' note:
-
-
Thanks Punit and Pranav.
| Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad: Paul Wheaton's 16th Kickstarter: Gardening playing cards for gardeners and homesteaders https://coderanch.com/t/889615/Paul-Wheaton-Kickstarter-Gardening-playing |






