• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Wrappers

 
Ranch Hand
Posts: 634
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 155
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Is there some specific reason for the problem i have posted ?
 
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


if there is no other statement in the program,then
Why isn't it gives ClassCastException ?
 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brilliant dude. Really. Very good question.
 
Harikrishna Gorrepati
Ranch Hand
Posts: 423
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 9050
21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 234
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
uf
 
Roy Miro
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic