This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

short and Integer

 
Ismael Upright
Ranch Hand
Posts: 166
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 162
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think the Integer i gets unboxed to int. Thats why it returns true
 
Dumitru Postoronca
Ranch Hand
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
agilemanoj kumar
Ranch Hand
Posts: 70
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Ben Reich
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 46
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 166
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 486
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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'.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic