• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why it is not working? Casting, widering or narrowing error?

 
Alam Ameghino
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Ranchers,

I am again with a fool question. I have a problem with this issue! ;-)

Why third line is correct and the fourth line is not correct?

A lot of Thanks!
 
Henry Wong
author
Marshal
Pie
Posts: 21489
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Alam Ameghino wrote:Hi Ranchers,

I am again with a fool question. I have a problem with this issue! ;-)

Why third line is correct and the fourth line is not correct?

A lot of Thanks!



Unfortunately, there are many things going on here ... and many reasons why something works, while others do not.

Line 2: An int can be implicitly cast to to a long, hence, this line compiles.

Line 1: An int that is a compile time constant, and can be deemed to fit into the value of a byte, can be implicitly cast to a byte, hence, this line compiles.

Line 3: An int that is a compile time constant, and can be deemed to fit into the value of a byte, can be autoboxed to a Byte, hence, this line compiles.

Line 4: Unfortunately, there is no such rule regarding autoboxing an int (compile time constant or otherwise) into a Long object.

Henry
 
Alam Ameghino
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henrry. I will memorize it!
Thanks!
 
Alam Ameghino
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there any rule to remember it in an easy way this cases?
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wonder whether the following page specifies the current rules - Autoboxing support for the Javatm Programming Language.

Regards,
Dan
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic