aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Wrappers Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Wrappers" Watch "Wrappers" New topic
Author

Wrappers

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634


It compiles and runs fine..

how is this possible
As far i know ,20 would be treated as int and it would be boxed to Integer .
Then,since Integer Does not extends Short,CLASSCASTEXCEPTION should occur


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Trivikram Kamat
Ranch Hand

Joined: Sep 26, 2010
Posts: 155
May be Java boxes 20 to Short.
It gives compiler error, if the value is not within the range of Short.

For example,


OCPJP6
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Is there some specific reason for the problem i have posted ?
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
if you call a method, for example testMe(10, 30)..then these values will be treated as ints. If you say short s = 20 or Short s = 20(Boxing), then it will be fine. Integer and Short are wrapper classes and both of them extends Number..So if you try to cast one with other(Short vs Integer), you will get classcastException which is thrown by JVM during Runtime. Did I answer your question ?


OCPJP 6.0-81% | Preparing for OCWCD
http://www.certpal.com/blogs/cert-articles | http://sites.google.com/site/mostlyjava/scwcd |
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634



if there is no other statement in the program,then
Why isn't it gives ClassCastException ?
Rohit Ramachandran
Ranch Hand

Joined: Oct 05, 2010
Posts: 102
Brilliant dude. Really. Very good question.
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Mohit,

Are you saying why Short s = 20; compiles and runs fine ? and why it is not throwing ClassCastException ? [or] something else ?
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

Harikrishna Gorrepati wrote:



Are you saying why Short s = 20; compiles and runs fine ? and why it is not throwing ClassCastException ? [or] something else ?


yes,exactly that..
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
I am saying, short s = 20 or Short s = 20(Boxing happens here), compiles & runs fine. Why do you think ClassCastException should occur so that I can answer further
Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

20 would be treated as int and it would be boxed to Integer .
Then,since Integer Does not extends Short,CLASSCASTEXCEPTION should occur
Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
20 is not integer value here. 20 is short here.. Let me put it this way, why do you think Short s = 20 is an integer here ?

If you understand my earlier post, you will be clear. I changes it little bit now.
if you call a method, for example testMe(10, 30) [here 10 and 30 are not assigned to any primitives..thats why they are int values) ..then these values will be treated as ints. If you say short s = 20 or Short s = 20(Boxing), it will be considered as short values and NOT null values. Integer and Short are wrapper classes and both of them extends Number..So if you try to cast one with other(Short vs Integer), you will get classcastException which is thrown by JVM during Runtime.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4420
    
    8

It works for the same reason that:works without a cast. The compiler can tell from context that the integer literal should be treated as a short. Therefore, in this case, it can be boxed to a Short.

Roy Miro
Greenhorn

Joined: Aug 29, 2010
Posts: 12
Hello,

What about this code:


Why does not the last line compile while "Integer I7 = (short)7;" does?
I think that the compiler could perform widening then boxign here.
Are my comments correct?

Thank you for your time.
Bert Bates
author
Sheriff

Joined: Oct 14, 2002
Posts: 8829
    
    5
I don't know if you'll agree to this approach but what if you looked at the Short class's constructors in the API?


Spot false dilemmas now, ask me how!
(If you're not on the edge, you're taking up too much room.)
Petar Thomas
Ranch Hand

Joined: Oct 11, 2009
Posts: 234
uf
Roy Miro
Greenhorn

Joined: Aug 29, 2010
Posts: 12
Sorry, but i don't get the thing with class Short API constructors.

To my mind:
first line: byte -> int (widening) ->Boxing
second and third line: int/byte -> long (windening) -> Boxing

The same mechanisms are implied, so why 2nd and last line don't compile?

Harikrishna Gorrepati
Ranch Hand

Joined: Sep 23, 2010
Posts: 422
Hi Roy, Line # 2 means.. By default any number is integer(int primitive)..So we can say either int l = 7 or Integer l = 7(Autobox) ..Only one it can do. It cannot do 2 things. i.e., you cannot say Long ll = 8; (Widening and boxing) I have given sample program..Please respond if you are not clear. I am not clear with some portion. Hopefully, somebody will help us
Roy Miro
Greenhorn

Joined: Aug 29, 2010
Posts: 12
Content edited by poster

Hello,

Thanks for your answer Harikrishna Gorrepati
Your piece of code make things clearer to me, particularly line 3 to 6.


My understanding
- It seems that with a code like because actually there is no widening followed by boxing, but only boxing.
"(byte)1" 's value is 1(int). That's why . The compiler only checks if 10000(int) cast to byte is compatible with integer range "((byte)10000"'s value is 16 (int))

To resume:



Any precisions or correction is welcomed.

Thank you again.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrappers