• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
Sheriffs:
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Wrapper Doubt

 
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
Posts: 23887
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux 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.
 
Everybody's invited. Even this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic