| Author |
Boxing ==,and equals()
|
aslika bahini
Ranch Hand
Joined: Mar 03, 2007
Posts: 111
|
posted

0
|
How this works? 1 Integer i1=1000; 2 Integer i2=1000; 3 if(i1!=i2) 4 System.out.println("different objects"); 5 if(i1.equals(i2)) 6 System.out.println("meaningfully equal"); output different objects meaningfully equal i1 & i2 refer to same object in order to save memory. when it refers to same object how come line 3 is true and prints o/p different object regards samura
|
 |
Barry Gaunt
Ranch Hand
Joined: Aug 03, 2002
Posts: 7729
|
|
"i1 & i2 refer to same object in order to save memory" What makes you think that?
|
Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
|
 |
Vishal K Patel
Ranch Hand
Joined: Oct 20, 2006
Posts: 43
|
|
Here, if We will assign the value between -128 to 127 then and only then, Both reference variable will refer to the same object as you are saying. Here you are assigning 1000, so it refers to the different object. Try it with,
|
 |
Ram Reddy
Ranch Hand
Joined: Feb 20, 2007
Posts: 88
|
|
hi , can any one please clarify this. for integers 1000 & 50 both are in acceptable ranges.
|
 |
Musou Dee
Greenhorn
Joined: Mar 07, 2007
Posts: 1
|
|
I guess there is a confusion between int and the wrapperclass Integer. If you do: int i=100; int j=100; System.out.println(i==j); will return a true since the value are compared and they are the same. However, Integer i= New Integer(100); Integer j= New Integer(100); will define two different Integer class in which case they are different, so the comparsion will returen false. BTW, since Integer is a wrapperclass, you cannot directly write Integer i=500. /D
|
 |
manmit roy
Greenhorn
Joined: Feb 28, 2007
Posts: 11
|
posted

0
|
1 Integer i1=1000; 2 Integer i2=1000; 3 if(i1!=i2) 4 System.out.println("different objects");
result : true, because its reference comparison. both are different.
5 if(i1.equals(i2)) 6 System.out.println("meaningfully equal");
result : true, because its value comparison. both having same value 1000 hope you got ... [ March 08, 2007: Message edited by: meet roy ]
|
 |
Milan Jagatiya
Ranch Hand
Joined: Jan 01, 2007
Posts: 164
|
|
Yeah meet is correct.... 1 Integer i1=1000; 2 Integer i2=1000; both having differet reference but same value... try by adding line i1=i2 now both have same reference and value. and the result of your code is meaningfully equal
|
Milan.<br />I can because I think I can...
|
 |
Jesse Custer
Ranch Hand
Joined: Feb 07, 2007
Posts: 45
|
|
This code generates output: 12 Explanation: x==y => true, because it's the value of the primitive that is compareda==b => true, the reference to the Integer object is compared, but because the Integer value is in byte range, they refer to the same objecta==c => false, the object references are compared, and c refers to different object because of declaration despite being in byte ranged==e => false, the object references are compared and are different because out of byte range
|
 |
 |
|
|
subject: Boxing ==,and equals()
|
|
|