milan chovatia wrote:Integer i3=10;
Integer i4=10;
if(i3 == i4)
{
System.out.println("they are same);
}
else
{
System.out.println("not same");
output: they are same
this is code part from book scjp6 by k&b and in that it says "in order to save memory instances of wrapper objects are always == when their primitive values are same."
but when i am running code i am getting output "not same".....
please someone can explain it....
"in order to save memory instances of wrapper objects are always == when their primitive values are same."
"I do not feel obliged to believe that the same God who has endowed us with sense, reason, and intellect has intended us to forgo their use."
-- Galileo Galilei
milan chovatia wrote:... from book scjp6 by k&b and in that it says "in order to save memory instances of wrapper objects are always == when their primitive values are same."
Steve
milan chovatia wrote:
i am getting output : not equal
shouldnt i get they are equal??....if not then how i get equal....and i am compiling it fine...
milan chovatia wrote:shouldnt i get they are equal??
Steve Luke wrote: It is only true when:
1) The Integer is boxed from a primitive (like Integer i3 = 10) -or- created using the Integer.valueOf() method.
...
if Integers were created using the constructors, then comparing them with == would not yield true.
Steve
Steve Luke wrote:I don't think the range is specified, but -128 to +127 is a range I often see.
Ivan Jozsef Balazs wrote:I ask myself sometimes whether autoboxing solves more problems than it creates.
milan chovatia wrote:Guys i am not getting they are equal in any condition....
Integer i=10;
Integer j=10;
milan chovatia wrote:I am writing exacly same file as luke wrote.....one with package example
i am getting output "not equal"
milan chovatia wrote:have you guys run this code/???
Steve
Stuart A. Burkett wrote:
Ivan Jozsef Balazs wrote:I ask myself sometimes whether autoboxing solves more problems than it creates.
Isn't that a good thing ?
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |