aspose file tools*
The moose likes Beginning Java and the fly likes casting.. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "casting.." Watch "casting.." New topic
Author

casting..

Krishna prasad ambala
Greenhorn

Joined: Jul 03, 2008
Posts: 11


please tell me what is the meaning of the above one
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14338
    
  22

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".


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

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 ]

Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

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

Joined: Apr 20, 2006
Posts: 3355

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

Joined: Jan 28, 2008
Posts: 5575

Thanks Raghavan
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Welcome Seetharaman
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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.


[Jess in Action][AskingGoodQuestions]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: casting..