• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

== , autoboxing Doubt

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Check out this code:

Integer i = new Integer(10);
Integer j = new Integer(10);
System.out.println(i==j);
System.out.println(i.equals(j));

Output: false
true

-------------------------

Integer i = new Integer(1000);
Integer j = new Integer(1000);
System.out.println(i==j);
System.out.println(i.equals(j));

O/p : false
true

In the first case according to Kathy and Sierra
Two instances of wrapper objs will always be == when their primitive values same from -128 to 127.
But i dont find any difference in the output.

Also

Integer i = 50;

gives me and error "cannot assign int to java.lang.Integer"
but
Integer 1 = new Integer(50);
works fine.
Thanks in advance
Sirisha
 
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, what you really need to understand is that the == operator checks if two objects are the same instance, not if they have the same state.

Hence if you say:

Integer a = new Integer(1000)
Integer b = new Integer(1000)

The condition a == b will always yield false.

Now, in JSE 1.5 you can use autoboxing, which is a new feature that allows the compiler to treat primitive data types as if they were objects. At run time, the primitive data type is wrapped into its corresponding data type wrapper object (Byte, Short, Integer, Long, Float, Double, Character).

Hence, when using JDK 1.5 ,I could also say:

Integer a = 1000;
Integer b = 1000;

Once again, two instances of the Integer class are created, thus a == b again yields false.

Nevertheless, all numeric values withing the range of byte are cached. That�s to say, there exist only one instance of them. Thus, this assigment

Integer a = 100;
Integer b = 100;

...always returns the same instance of 100. Then the condition a == b will yield true in this case.

In order that a and b receive a different instance of the value "100" you need to use the new operator. Like this:

Integer a = new Integer(100);
Integer b = new Integer(100);

In whose case, the condition a == b will yield false once again.

On the other hand equals(Object) will return true whenever the two Integer objects hold the same value. In this regard, all previous cases should return true, whether you compare the same instance of the object or not.
[ May 24, 2006: Message edited by: Edwin Dalorzo ]
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please explain output for this

Integer i = new Integer(100);
int j = 100 ;

//1
System.out.println(i==j);
System.out.println(i.equals(j));

//2
System.out.println(j==i);
System.out.println(j.equals(i));

Thanks
 
Edwin Dalorzo
Ranch Hand
Posts: 961
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The code does not compile, because j is not an object, hence, you cannot invoke j.equals().

Thus, there is not output.
 
Sai Charan
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
please explain the difference for this (sorry if i am not creating a seperate post for this)

//1
Integer i = new Integer(100);
int j = 100 ;
System.out.println(i==j);
System.out.println(i.equals(j));

//2
Integer i = new Integer(100);
Integer j = 100 ;

System.out.println(i==j);
System.out.println(i.equals(j));

Thanks
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is from the Java Language Specification.

If the value p being boxed is true, false, a byte, a char in the range \u0000 to \u007f, or an int or short number between -128 and 127, then let r1 and r2 be the results of any two boxing conversions of p. It is always the case that r1 == r2.
 
Siri Naray
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank You, i got it.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
could someone clear my doubt..
1.what Keith Lynn mean to say i am not getting?
2.see code below..

Integer i=new Integer(100);
Integer j=new Integer(100);
System.out.println(i==j);
Output:false.
Why it is false?

Equals:it will compare whether both ref variable(i nd j) both pointing to the same obj,is this statement correct
Integer i=new Integer(100);
Integer j=new Integer(50);
System.out.println(i.equals(j));
Output:false.
Why it is false?




Regards
Kirba.
 
Ranch Hand
Posts: 637
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Answer to second question:
You did not get the concept, Lemmeie explain again
a) ==
ref1 == ref2 will return true iff ref1 and ref2 both point to same object on heap otherwise the condition will return false.
so now
Integer a = new Integer(100);
Integer b = new Integer(100);

Here JVM will construct two Integer objects because they are invoked using Integer constructor and now the two references a and b will point to two seperate objects in heap as a result a==b will result in false.

b) equals() : Integer class overides equals() of Object and will return true if two objects in comparision both have same value
i.e., if( a.intValue() == b.intValue() ) return true.
So now if you have
Integer a = new Integer(100);
Integer b = new Integer(100);

a.equals(b) or b.equals(a) will return true because the two Integer objects being referenced by a and b have same integer value of 100 and hence the result is true.
So now you know why
Integer a = new Integer(100);
Integer b = new Integer(50);
a.equals(b) or b.equals(a) will return false.
Deepak
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sirishaaaaa Ghatty,

we have a policy according naming.
Sirishaaaaa does not look much like a propper name.
See this please:
http://www.javaranch.com/name.jsp


Change your user name before posting next time.
Your old postings (including this) will not be affected, only your user name will update.


Yours,
Bu.
 
kirba devi
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi deepu,
Thanks for clearing my doubts.

Regards
Kirba.
 
reply
    Bookmark Topic Watch Topic
  • New Topic