Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper confusion

 
patrick avery
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are there inconsistencies like this regarding Wrapper instantiation......is there a general rule that would help in understanding the inconsistencies?

Example:
 
Anthony Accioly
Greenhorn
Posts: 18
IntelliJ IDE Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

You should take a look at the concepts of casting, widening and boxing (Chapter 3 - Assignments of Kathy Sierra and Bert Bates SCJP 6 Study Guide)

A literal like 123 is understood by the compiler as a int, so autobox wrapps it in a Integer object when it is needed. Integer is not a (instanceof) of Long, so compilation fails.
When you append l to the end of a literal you are telling the compiler that this is a long, so autobox wrapps it in a Long object and it compiles fine.

Long object has a constructor that accepts a primitive type long Long Java API Documentation. A int "fits" in the space of a long, so the compiler casts a int to a long "automagically" as you pass it to the constructor, and, as so, everything works without the 'l' at the end of the literal.

Short object has a constructor that accepts a primitive type short Short Java API Documentation. A int dosen't fit in the space of a short, so you need to explicitely cast the int with the risk of loosing precision (that is why the compiler don't do that automagically for you).

I hope it helps.

Cheers
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with everything that Anthony said.

But I find it a little baffling that

Short s = 4;

compiles, especially given that

Long l = 4;

doesn't.

My guess is that the compiler is doing something similar to what it does when it sees

short s = 4; // 4 is a compile time constant in short range.

What is baffling is the inconsistency. It seems it would be appropriate to let

Long l = 4;

compile by the same principle.
 
patrick avery
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the great feedback.

I think this makes more sense if you consider it as a case of the rule that says "you can't widen then box"

i.e.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's a good point, Patrick. Thanks.
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exactly Patrick, I have also related this concept to widening+boxing not allowed concept.

and for
Short s=4;

no widening + boxing requires so it is allowed.

One more confusing part is that you can not do

Integer i6=(byte)4;
Integer i7=(char)4;
Integer i8=(short)4;

considering widening+boxing not allowed.

But you can do
Character c1=(byte)4;
Short s1=(byte)4;

Again widening+boxing requires here. But you can say widening+boxing concepts applies for Integer type and upper types only.
[ December 29, 2008: Message edited by: punit singh ]
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by punit singh:
Character c1=(byte)4;
Short s1=(byte)4;

Very interesting, thanks. Punit, do you know if you can apply that to argument passing to? Or is argument passing different (no widening and boxing at all)?
 
Punit Singh
Ranch Hand
Posts: 952
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ruben it seems different for argument passing, at least my jdk 6 is telling it is different, I tried this one.


static void method(Character c1){
System.out.println("Character");
}

method(((byte)4));//compiler error



It is telling method(Character) cannot be applied to arguments (byte).

Well this happens as you know short s=4; is valid, but for argument passing.

static void method(short s1){
System.out.println("short");
}

method(4);//error, method(short) is not applicable for arguments int.
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for checking that, it makes sense.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic