Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrappers

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Trivikram Kamat
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
May be Java boxes 20 to Short.
It gives compiler error, if the value is not within the range of Short.

For example,
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there some specific reason for the problem i have posted ?
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ?
 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


if there is no other statement in the program,then
Why isn't it gives ClassCastException ?
 
Rohit Ramachandran
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Brilliant dude. Really. Very good question.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8898
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Petar Thomas
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
uf
 
Roy Miro
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 423
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic