File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Wrapper Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Wrapper Doubt" Watch "Wrapper Doubt" New topic
Author

Wrapper Doubt

saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583


F:\Wrapper classes>javac Example.java
Example.java:11: error: inconvertible types
byte b2=(Byte)f2;

Why inconvertible ? After casting it should be convertible


Tell the difficulties that i am difficult.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4457
    
    8

You're trying to cast am Integer to a Byte. They aren't compatible, they're on different inheritance branches. That would only make sense if Byte was a subclass of Integer.
saloni jhanwar
Ranch Hand

Joined: Feb 09, 2012
Posts: 583

Matthew Brown wrote:You're trying to cast am Integer to a Byte. They aren't compatible, they're on different inheritance branches. That would only make sense if Byte was a subclass of Integer.


Then i were expecting ClassCastException.
dennis deems
Ranch Hand

Joined: Mar 12, 2011
Posts: 808
saloni jhanwar wrote:
Matthew Brown wrote:You're trying to cast am Integer to a Byte. They aren't compatible, they're on different inheritance branches. That would only make sense if Byte was a subclass of Integer.


Then i were expecting ClassCastException.


The compiler is clever enough to know when a cast could not possibly work, so an error is thrown. If there is any chance the cast could work, the compiler will allow it.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18962
    
  40

Also, while going from Integer to Byte to byte is not a valid casting route, going from Integer to int to byte is a valid explicit casting route (provided that you are willing to accept the possible loss of precision), so this should work....



Henry


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

Joined: Feb 09, 2012
Posts: 583

Thanks Henry for new logic.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Wrapper Doubt