Win a copy of Securing DevOps this week in the Security forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Wrapper Doubt  RSS feed

 
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
 
Bartender
Posts: 4568
9
  • 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.
 
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.
 
author
Sheriff
Posts: 23394
138
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
Boost this thread!