Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different methods for casting an object in Java

Tags:

java

casting

I know the three following methods that can be used for casting objects.

Object o = "str";

String str1 = (String) o;                // Method 1

String str2 = o.toString();              // Method 2

String str3 = String.class.cast(o);      // Method 3
  1. Which approach is better, and what are the pros/cons of one method as compared to the others?
  2. What happens to the object during the time of casting internally?
like image 319
Muhammad Suleman Avatar asked Jun 05 '15 07:06

Muhammad Suleman


People also ask

What is casting objects in Java?

Type Casting is a feature in Java using which the form or type of a variable or object is cast into some other kind or Object, and the process of conversion from one type to another is called Type Casting.

How do you cast an object to a different class in Java?

The java. lang. Class. cast() method casts an object to the class or interface represented by this Class object.

What is type casting in Java with example?

Example: Converting int to doubleAnd then assign it to the double variable. In the case of Widening Type Casting, the lower data type (having smaller size) is converted into the higher data type (having larger size). Hence there is no loss in data. This is why this type of conversion happens automatically.


3 Answers

The second method that you show is not casting; it's simply calling the toString() method on an object, which is not different than any other method call:

String str2 = o.toString();

The effect of the first and third methods is essentially the same. I would prefer using the first method.

What happened with object on the time of casting internally?

Nothing happens to the object. Casting is not a way to somehow automatically convert objects from one type to another type. The only thing that a cast does, is tell the compiler to accept the assignment statement and not check the types for this statement. You're saying to the compiler "I know better than you what kind of object this is, so let me do this assignment and don't complain about the type".

In your example, the type of the variable o is Object. When you assign o to a variable of type String, the compiler won't allow it because it checks the types, and it can't be sure that o in fact refers to a String object. So you use a cast to tell the compiler "I know this is a String object, so let me do this assignment".

The type will still be checked, but at runtime, not at compile time. If, at runtime, the type of the object is not String, you'll get a ClassCastException.

like image 142
Jesper Avatar answered Sep 28 '22 17:09

Jesper


The first one casts the o reference, whose declared type is Object, and whose actual, concrete type is String, to a String. This is the canonical way of casting. That will only work if the object referenced by o is actually of type String. If it isn't, you'll have a ClassCastException.

The second one doesn't cast at all. It calls the toString() on the object referenced by o. That will always work, but it's really really different from a cast.

The third one uses reflection to do a cast. That will have the same effect as the first one, and will work in the same circumstances. But this will generally only be used when the code doesn't actually know the type of the class to cast to:

Class<?> someClassToCastTo = ...; // could be String.class or anything else, you don't know)

String str3 = someClassToCastTo.cast(o);

Nothing happens to the object when it's cast. Casting checks that the object is indeed of class String, and fails otherwise, that's all. But once cast to a variable of type String, you can access the methods that exist in String, and that you couldn't have called when you had a variable of type Object.

like image 27
JB Nizet Avatar answered Sep 28 '22 19:09

JB Nizet


Here it goes:

Object o = "str";

String str1 = (String) o;

This works only when the object actually is a string.

String str2 = o.toString();

When you use toString() on a String object, you obtain the string itself. It will throw an exception when object o is null.

String str3 = String.class.cast(o);

Mainly used when using reflection, i.e. when you want to retrieve the Class token via reflection.

like image 20
Rahul Tripathi Avatar answered Sep 28 '22 19:09

Rahul Tripathi