• 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

Dout on boxing

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class Boxing {
public static void main(String args[]) {
Integer i1 = new Integer(10);
Integer i2 = 10;
System.out.println("Lessthan: " + (i1 <= i2));
System.out.println("Greaterthan: " + (i1 >= i2));
System.out.println("Equality: " + (i1 == i2));
}
}

it compiles fine.and give output as

Lessthan: true
Greaterthan: true
Equality: false


why they are not equal...
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by karnatam narendraprasad:
public class Boxing {
public static void main(String args[]) {
Integer i1 = new Integer(10);
Integer i2 = 10;
System.out.println("Lessthan: " + (i1 <= i2));
System.out.println("Greaterthan: " + (i1 >= i2));
System.out.println("Equality: " + (i1 == i2));
}
}

it compiles fine.and give output as

Lessthan: true
Greaterthan: true
Equality: false


why they are not equal...



When == is applied to two object references, then what is being tested is whether those two references point to the same object.

The first reference refers to a new Integer. The second refers to an Integer object created from boxing.

Unless one of the operands is a primitive in ==, then unboxing will not occur.
 
Ranch Hand
Posts: 893
Tomcat Server Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
For testing if two objects are meanfully the same you should use the equals command. You're using the == command which checks if the two references are referring to the same object which is not true.

Maybe the code below can help you
 
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

why these two are giving out put as true ?

System.out.println("Lessthan: " + (i1 <= i2));
System.out.println("Greaterthan: " + (i1 >= i2));
 
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


why these two are giving out put as true ?

System.out.println("Lessthan: " + (i1 <= i2));
System.out.println("Greaterthan: " + (i1 >= i2));


Here Auto Boxing come in picture and Integer wrapper class convert to normal int type and than compare two int variable so It will return True in this case.
 
Ram Reddy
Ranch Hand
Posts: 88
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
thanks patel.

then for "==" unboxing wont happen ?
 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

then for "==" unboxing wont happen ?



That is correct. Not for a reference comparison.
 
reply
    Bookmark Topic Watch Topic
  • New Topic