wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes primitive casting+boxing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "primitive casting+boxing" Watch "primitive casting+boxing" New topic
Author

primitive casting+boxing

Mahesh Murugaiyan
Greenhorn

Joined: Jun 25, 2009
Posts: 21
hello. im preparing for SCJP 1.6 and came across these two questions which confuses me a bit. i tried these out and they work as mentioned in the book, still i need some clarifications:

Question 1:

2. Question 2:

- I read that all numerical literals are int by default. so in the first example, the int 200 is narrowed down to short and then boxed into Short. (is it also because 200 can fit in short easily?)

- then, why cant the 7 which can fit in the short is being accepted as a parameter? while short story = 200; works, why cant an int be passed as a parameter?

Thanks
Mahesh
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18495
    
  40

Mahesh Murugaiyan wrote:
- I read that all numerical literals are int by default. so in the first example, the int 200 is narrowed down to short and then boxed into Short. (is it also because 200 can fit in short easily?)


Correct. This is defined in section 5.2 of the Java Language Specification....

http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.2

And in this section, is a discussion regarding compile time constants, and boxing to Short.


Mahesh Murugaiyan wrote:
- then, why cant the 7 which can fit in the short is being accepted as a parameter? while short story = 200; works, why cant an int be passed as a parameter?


Method parameter conversions is defined in section 5.3 of the Java Language Specification....

http://docs.oracle.com/javase/specs/jls/se7/html/jls-5.html#jls-5.3

And interestingly, there isn't a provision for compile time constants.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mahesh Murugaiyan
Greenhorn

Joined: Jun 25, 2009
Posts: 21
Thanks for the pointer!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: primitive casting+boxing
 
Similar Threads
Objects - GC
garbage collection?
GC help
Garbage collection
Confused with GC related K&B 1.5 question