aspose file tools*
The moose likes Beginning Java and the fly likes Boxing question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Boxing question" Watch "Boxing question" New topic
Author

Boxing question

Bob Damiano
Greenhorn

Joined: Sep 17, 2008
Posts: 8
Hi All,

This is my first (of many probably) post here at the lodge. I'm a long time c++ programmer with a little java in my history. I'm coming back from the darkside and preparing to go for a scjp.

I'm puzzled about some details regarding creation of primative wrapper objects.

Basically, I'm wondering how is this legal:

Short s = 5;

My understanding is that this is the equivalent of:

Short s = new Short(5);

But here the literal 5 is considered to be an int literal, not a short literal right? (I definitely missed one of the self test questions because of this rule).

And the Short class only has a constructor that accepts shorts - not ints.

"Widening" won't work because shorts are narrower than ints. What we need here is narrowing

Are my assumptions just wrong about how s is being created, or in this case does Java accept this literal as a short (provided it fits into 16 bits)? If so, are there rules about when Java will and will not strictly interpret a non-floating point literal as an int?

Just curious.

Thanks in advance for your wisdom.

- Bob


These bits go to 2
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 19060
    
  40


My understanding is that this is the equivalent of:

Short s = new Short(5);


Actually, it is the equivalent of:

Short s = Short.valueOf(5);


But here the literal 5 is considered to be an int literal, not a short literal right? (I definitely missed one of the self test questions because of this rule).

And the Short class only has a constructor that accepts shorts - not ints.

"Widening" won't work because shorts are narrower than ints. What we need here is narrowing


It's a compile time constant. The Java compiler knows the value at compile time, and know that it can "fit" into a short, hence, the cast is not needed.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Originally posted by Henry Wong:
Actually, it is the equivalent of:

Short s = Short.valueOf(5);

The difference being that all values between -128 and 127 will return a cached object for all primitives from byte to long.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Bob Damiano
Greenhorn

Joined: Sep 17, 2008
Posts: 8
Thanks very much for the replies Henry and Rob!

oooo moved to "Beginner" ouch.

I'll try to be more advanced in my next questions

-Bob
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 40034
    
  28
Originally posted by Bob Damiano:

I'll try to be more advanced in my next questions
No, stay here; you'll be much happier here.
Bob Damiano
Greenhorn

Joined: Sep 17, 2008
Posts: 8
Thanks again guys. Not to beat a dead horse or anything...

Actually, it is the equivalent of:
Short s = Short.valueOf(5);


According to the docs, the valueOf() methods of all the Number derivitaves, takes a String arg, not a primitive. Some have an overloaded version that takes a String and a radix.


It's a compile time constant. The Java compiler knows the value at compile time, and know that it can "fit" into a short, hence, the cast is not needed.


Aside from that, here is what got me asking the original question:

In the Sierra/Bates test prep book self test for the Assignment chapter, there was a question (#3) about overloading.

There were two overloaded methods called invade(). One takes short..., and the other takes short (both little short, not big short).

The question was which one would be called if you called this method with (7).

The answer (which I got wrong) was "Compilation Fails" because the 7 will be interpreted as an int literal and therefore needed to be casted. Here is a case where the compiler can certainly know at compile time that 7 will fit into the short. I've tried it and it really does fail of course.

So that led me to my question of how an instance of the Short wrapper could be created from a int literal. For the test I'm worried I won't know the conditions under which the compiler can decide that it "knows" the constant will fit into the short and when it will require a cast.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19783
    
  20

Originally posted by Bob Damiano:
According to the docs, the valueOf() methods of all the Number derivitaves, takes a String arg, not a primitive. Some have an overloaded version that takes a String and a radix.

Update your link to the Java API to Java 5 or preferrably Java 6. You're still using a 1.4 link.
I know this since the valueOf methods that take primitives have been added in Java 5

Aside from that, here is what got me asking the original question:

In the Sierra/Bates test prep book self test for the Assignment chapter, there was a question (#3) about overloading.

There were two overloaded methods called invade(). One takes short..., and the other takes short (both little short, not big short).

The question was which one would be called if you called this method with (7).

The answer (which I got wrong) was "Compilation Fails" because the 7 will be interpreted as an int literal and therefore needed to be casted. Here is a case where the compiler can certainly know at compile time that 7 will fit into the short. I've tried it and it really does fail of course.

So that led me to my question of how an instance of the Short wrapper could be created from a int literal. For the test I'm worried I won't know the conditions under which the compiler can decide that it "knows" the constant will fit into the short and when it will require a cast.

7 will only be implicitly cast to a short (or byte) in direct assignment, in the form of "short s = 7;". "Short s = 7;" falls in that same category since it shorthand for the following:

In all other cases it is indeed an int, and you need to explicitly cast it:
Bob Damiano
Greenhorn

Joined: Sep 17, 2008
Posts: 8
Thanks for the reply.

Update your link to the Java API to Java 5 or preferrably Java 6. You're still using a 1.4 link.
I know this since the valueOf methods that take primitives have been added in Java 5


By "docs", I meant the SCJP study guide for Java 5 by Sierra and Bates. It only lists the string and string + radix overloads. Apparently the primitive versions will not be asked about on the J5 exam? I'll look at the online docs for j6. Thanks

-bob
Bob Damiano
Greenhorn

Joined: Sep 17, 2008
Posts: 8
Thank you very much. Now it all hangs together.

I got confused because Short.valueOf(7) does not compile. The parameter needs to be (short)7.


So :
Assignments... the compiler will decide if the int literal can fit.
Method args... very strictly interpreted as int and requires a cast.

Thank you very much again.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Boxing question