Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

casting..

 
Krishna prasad ambala
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


please tell me what is the meaning of the above one
 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15150
31
Android IntelliJ IDE Java Scala Spring
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch.

You're declaring a variable named 'e'. The type of 'e' is 'D'.

Then you create a new object of type 'E', and cast it to type 'D'.

Casting means that you tell the compiler: "I have an object here, and you must pretend that this object is of type D".
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just to add on top of what Jesper has clearly explained, Casting is a process in which the programmer requests the compiler to treat one type/kind of data (object) to be of another kind so as to avoid any errors during compilation. Otherwise, the compilation would not succeed as the compiler is not instructed to do so.

One example is, when you have an integer whose value is less and trying to assign it into a byte variable. Let's say,


int i = 5;
byte b = i;


Even though the actual value in 'i' is less than or allowed to get stored into a variable of type byte, the compiler can't check and ensure the same during the runtime. So, just to prevent the errors thrown by JVM at the runtime, the compiler gives an error and does not let the program compile successfully.

To proceed further, we do request the compiler by making an explicit typecast or cast as follows.


int i = 5;
byte b = (byte) i;


This way the program is compiled and run successfully. However, if the value stored in 'i' is more than the maximum limit of byte's, the value is truncated accordingly.
[ July 10, 2008: Message edited by: Raghavan Muthu ]
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Krishna prasad ambala:


----------------------------------------------------
if D is a subclass of E,then ClassCastException

-----------------------------------------------------

anyone tell me that how we can prevent this
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by seetharaman venkatasamy:


anyone tell me that how we can prevent this


You are right. But it is NOT possible to override/overcome by no way you can allow a super class object to be referred by a subclass reference as it is against the nature/reality.

Only the *permissible* values are allowed to get stored with the typecast and not the other way!
[ July 10, 2008: Message edited by: Raghavan Muthu ]
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Raghavan
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome Seetharaman
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also note that it is never necessary, nor does it ever make any sense, to cast the result of a "new" expression this way. Casting makes sense only when the compiler doesn't already know the exact class of an object. Here, the cast cannot add any new information, since the compiler knows the exact type of the object being created.

This will never throw a ClassCastException. If the cast is invalid, the code will not compile.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic