aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Why it is not working? Casting, widering or narrowing error? 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 "Why it is not working? Casting, widering or narrowing error?" Watch "Why it is not working? Casting, widering or narrowing error?" New topic
Author

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

Alam Ameghino
Ranch Hand

Joined: Jun 01, 2011
Posts: 60
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
Sheriff

Joined: Sep 28, 2004
Posts: 19066
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Alam Ameghino
Ranch Hand

Joined: Jun 01, 2011
Posts: 60
Thanks Henrry. I will memorize it!
Thanks!
Alam Ameghino
Ranch Hand

Joined: Jun 01, 2011
Posts: 60
Is there any rule to remember it in an easy way this cases?
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1180
I wonder whether the following page specifies the current rules - Autoboxing support for the Javatm Programming Language.

Regards,
Dan


William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why it is not working? Casting, widering or narrowing error?