File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting

 
Ramm Kummar
Greenhorn
Posts: 26
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"byte b = 27;
but only because the compiler automatically narrows the literal value to a byte. In
other words, the compiler puts in the cast. The preceding code is identical to the
following:
byte b = (byte) 27; // Explicitly cast the int literal to a byte"

-Kathy


1)but if this is tried, compiler not doing implicit casting. Whats the reason for this?

byte = 7000;
it should be byte =(byte) 7000;


2) Casting is used among objects which are related to each other in the inheritance tree otherwise compiler error(inconvertable types) will be thrown. How these primitives are related? since their wrapper classes are not in same inheritance tree. They are not related to each other. How is this casting possible among primitives?




 
Campbell Ritchie
Sheriff
Pie
Posts: 47281
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the case of byte b = 7000; a cast is necessary because the compiler can "see" that the literal is out of range for a byte.

In the case of class-casting, that is completely different from primitive casting, and the only similarity is the name.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic