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 short and Integer 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 "short and Integer" Watch "short and Integer" New topic
Author

short and Integer

Ismael Upright
Ranch Hand

Joined: Feb 15, 2007
Posts: 166
Consider the following code:


public class TestClass
{
public static void main(String[] args)
{
short s = 9;
Integer i = 9;
System.out.println( s == i );
}
}



This actually compiles and prints "true".
For me it seems like code that should not compile..

What actually happend there?
sridhar row
Ranch Hand

Joined: Jan 16, 2008
Posts: 162
I think the Integer i gets unboxed to int. Thats why it returns true
Dumitru Postoronca
Ranch Hand

Joined: May 06, 2008
Posts: 46
I can see your point, as

prints false.

So it's definately not trying to box the short into Short in your example.

Maybe unboxing has higher priority than boxing?
Krishnamoorthy Vuyala Muralidharan
Ranch Hand

Joined: Sep 13, 2005
Posts: 52
Originally posted by Ismael Upright:
Consider the following code:




This actually compiles and prints "true".
For me it seems like code that should not compile..

What actually happend there?



Hi There

In the above code, the comparison is done using the operand == instead of using equals() method. This means that the unboxing of Integer to int will be done in such a way it is possible to do a == comparison between short and int.

short s =9;
Integer i =9;
System.out.println(s==i);// which returns true as both are having a value of 9

Kind regards
Kris
Ben Reich
Greenhorn

Joined: May 07, 2008
Posts: 3
comparing Short and Integer by equals can't work because object passed to equals method must pass IS-A test.

btw. here is some intresting code to think ab. ;-)

Integer i1_a = 7;
Integer i1_b = 7;
Integer i2_a = 128;
Integer i2_b = 128;
Integer i1constr_a = new Integer(7);
Integer i1constr_b = new Integer(7);

System.out.println("i1_a==i1_b " + (i1_a==i1_b));
System.out.println("i2_a==i2_b " + (i2_a==i2_b));
System.out.println("i1_a==i1constr_a " + (i1_a==i1constr_a));
System.out.println("i1constr_b==i1constr_a " + (i1constr_b==i1constr_a))


results are:


[ May 10, 2008: Message edited by: Ben Reich ]
[ May 10, 2008: Message edited by: Ben Reich ]

SCJP 5 83%
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
i2_a==i2_b false
i1_a==i1constr_a false
i1constr_b==i1constr_a false


Can you explain me this please with valid reason how it has come


Thanks<br />Dinesh
agilemanoj kumar
Ranch Hand

Joined: Mar 07, 2008
Posts: 70


In order to save memory, two instances of the
following wrapper objects will always be == when their primitive values are the same:
Boolean
Byte
Character from \u0000 to \u007f (7f is 127 in decimal)
Short and Integer from -128 to 127


Integer i1constr_a = new Integer(7);
Integer i1constr_b = new Integer(7);


Two objects are created, so both fail == operation.


Manoj Kumar
Ben Reich
Greenhorn

Joined: May 07, 2008
Posts: 3
[edit]

agilemanoj kumar was faster than me ;-)

btw. consider:


String a1_a = "a";
String a1_b = "a";

String a2_a = new String("a");
String a2_b = new String("a");

System.out.println("a1_a==a1_b " + (a1_a==a1_b));
System.out.println("a2_a==a2_b " + (a2_a==a2_b));

[ May 10, 2008: Message edited by: Ben Reich ]
Dumitru Postoronca
Ranch Hand

Joined: May 06, 2008
Posts: 46
The compiler choses to unbox first because if it were to box the short into Short, it won't be able to compare the two references, as neither is a subclass of the other (you can't say if(myString == myInteger)).
Ismael Upright
Ranch Hand

Joined: Feb 15, 2007
Posts: 166
Going back to my problem here


Was it like this?

1. Integer got unboxed to int
2. short was promoted to int
3. Two ints were compared by ==
4. true was returned
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
i2_a==i2_b false

Why this false ??.
both are having saem values,as you said that wrapper class saves the memory if wrapper objects have same primitive value, which in this is 128 for both i2_a and i2_b..

please help me in understanding this concept..
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14266
    
  21

Ismael: Yes, that's exactly what's happening.

Dinesh: When you initialize an Integer object by autoboxing, as happens with i2_a and i2_b:

Integer i2_a = 128;
Integer i2_b = 128;

This is converted automatically to the following by the compiler:

Integer i2_a = Integer.valueOf(128);
Integer i2_b = Integer.valueOf(128);

The method valueOf(...) of class Integer has a caching mechanism. If the value you put into it is between -128 and +127, it will return a pre-made Integer object from the cache. If the value is outside that range, it will create a new Integer object initialized with the given value.

In the case of i2_a and i2_b, the value is outside the range, so i2_a and i2_b will refer to two new, different Integer objects.

As you know, if you use == on reference variables, it will only compare the references - you'll only get 'true' if the two variables refer to the exact same object. In this case, they don't - there are two separate Integer objects - so the result is 'false'.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: short and Integer