• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Wrapper Doubt

 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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

Why inconvertible ? After casting it should be convertible
 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 808
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Marshal
Pie
Posts: 21192
81
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
saloni jhanwar
Ranch Hand
Posts: 583
Firefox Browser Notepad Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry for new logic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic