wood burning stoves 2.0*
The moose likes Java in General and the fly likes Confused with the topic Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Confused with the topic "Casting Of Reference variables"  " Watch "Confused with the topic "Casting Of Reference variables"  " New topic
Author

Confused with the topic "Casting Of Reference variables"

sandy chauhan
Ranch Hand

Joined: Apr 14, 2013
Posts: 31
I am not able to understand how the casting of reference variable works.
Someone please provide an easy explanation of this topic.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Could you be more specific and explain a bit in detail what is it exactly that you don't understand about reference variable casting in Java?


~ Mansukh
sandy chauhan
Ranch Hand

Joined: Apr 14, 2013
Posts: 31
Actually I want to know why we need casting of reference variable and when to use it.??
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18978
    
  40

sandy chauhan wrote:Actually I want to know why we need casting of reference variable and when to use it.??


When the compiler can't determine whether an instance is a certain type, such as whether the Object instance passed to the method IS-A String instance needed in the method, it will complain (ie. compiler error). If you, as the programmer know better, then you can override the compiler with an explicit cast. However, you better be right, otherwise, all you did is convert a compiler error to a run-time exception.

Henry

Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14339
    
  22

You can use a cast if you have a variable of a certain type, and you need to let the Java compiler treat it as a different type.

Suppose that you have a plain List (you're not using generics). The List contains objects, but it has no information on what objects it contains. The get() method on List returns an Object. You'll need to cast the object that you got from the List to be able to do things with it. Here's a code example.

Normally, you would use generics in this case, so you would not need to cast. But in older versions of Java (1.4 and older) generics did not exist, so you had to cast every time you got an element from a list.

Note that casting does not do any kind of conversion. If the object you're trying to cast is not compatible with the type you're casting it to, you'll get a ClassCastException at runtime - it's not going to automatically convert the object to the desired type (some people seem to think that casting automatically converts objects, but it doesn't).

All you're really doing, is telling the compiler "I have an object here and I'm telling you that it's of type X, so I want you to treat it as if it is of type X - don't complain about it".

The page Inheritance in Oracle's Java Tutorials also explains casting.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Confused with the topic "Casting Of Reference variables"