File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Boxing question

 
Bob Damiano
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Marshal
Pie
Posts: 20835
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Rob Spoor
Sheriff
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Bob Damiano
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47262
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20380
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic