aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrapper confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrapper confusion" Watch "Wrapper confusion" New topic
Author

Wrapper confusion

patrick avery
Ranch Hand

Joined: Sep 12, 2008
Posts: 46
Why are there inconsistencies like this regarding Wrapper instantiation......is there a general rule that would help in understanding the inconsistencies?

Example:


SCJA 96%
SCJP 6 88%
skipping SCJD to work on passing SCWCD
Anthony Accioly
Greenhorn

Joined: Dec 22, 2008
Posts: 16

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

Joined: Dec 16, 2008
Posts: 1032
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.


All code in my posts, unless a source is explicitly mentioned, is my own.
patrick avery
Ranch Hand

Joined: Sep 12, 2008
Posts: 46
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

Joined: Dec 16, 2008
Posts: 1032
That's a good point, Patrick. Thanks.
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
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 ]

SCJP 6
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
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

Joined: Oct 16, 2008
Posts: 952
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

Joined: Dec 16, 2008
Posts: 1032
Thanks for checking that, it makes sense.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrapper confusion
 
Similar Threads
Automatic cast
Wrapper Assignments from ExamLab
AutoBoxing Doubt
primitive narrowing conversions
Long v = 4; why is this faulty?