File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Dout on boxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Dout on boxing" Watch "Dout on boxing" New topic
Author

Dout on boxing

karnatam narendraprasad
Ranch Hand

Joined: Mar 01, 2007
Posts: 30
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...
Keith Lynn
Ranch Hand

Joined: Feb 07, 2005
Posts: 2367
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.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
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


Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
Hi,

why these two are giving out put as true ?

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

Joined: Jun 16, 2006
Posts: 57

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.


Pinkal Patel<br />SCJP 1.5<br />Preparing for SCWCD
Ram Reddy
Ranch Hand

Joined: Feb 20, 2007
Posts: 88
thanks patel.

then for "==" unboxing wont happen ?
Chris Stann
Ranch Hand

Joined: Oct 10, 2006
Posts: 49
then for "==" unboxing wont happen ?


That is correct. Not for a reference comparison.


EXCEL IN ALL YOU DO
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dout on boxing