简体   繁体   中英

Why can't we cast an integer object to a string object?

Why can't we cast an integer object to a string object? Please Explain in detail.

Integer obj = new Integer(10);
System.out.println((String)obj);

Does covariant return types come into the picture here?

You can't cast an Integer to a String because they are not related. You can only cast a referece to one of its superclasses, ie: upcasting, in which case the cast operator is not needed, or its subclasses (downcasting).

Integer is neither a subclass nor a superclass of String , so you can't do that. If you were allowed to arbitrarily cast an object to whatever type you wanted, why bother having types at all?

All Java objects have a .toString() method (inherited from Object ), so all you have to do is String s = obj.toString(), although System.out.println() calls that automatically for you.

And no, this doesn't have anything to do with covariant return types.

You cannot do this because the integer object is not of type string. Alternatively, you may invoke the toString() method on the integer obj (obj.toString()) since the toString() method is defined in the object class and integer is a subclass of the object class.

You don't actually need to cast the String. This would work just fine:

Integer obj = new Integer(10); System.out.println(obj);

In this code, java is explicitly calling the toString() method which is available on all objects and that's why it works. You can only cast and Object if you actually have an instance of that object. So, if you had this code:

Object s = "A String";

You could cast that to a string:

System.out.println((String) s);

But, if you had:

Object o = new SomeObject();

You could only case that back to SomeObject or a type that SomeObject extends.

Covariant return types do not come into the picture here for your example. However, if you are allowed to override a method using a covariant return type, you will also be able to cast both ways. So if you had a method:

public A doSomething();

and you overrode it in a subclass as:

public B doSomething();

It will always be the case that you can perform either of these successfully:

(A) doSomething();
(B) doSomething();

That cast does not work because it is not an is a kind of relation. Eg you cannot cast an object of type car with that of a vehicle but not to a fruit. I hope that gives you some idea. In summary, an integer is not a string.

Because they are different object types, and the Java language does not allow casting across two different object types (unless of course they are inherited) - am not sure what else you need to be elaborated upon. You can use Integer.toString() to stringify your Integer object.

String provides methods for converting other objects.

Integer obj = new Integer(10); 
String stringWasInt = String.valueOf(obj);
System.out.println(stringWasInt );

Java is a strongly typed programming language because every variable must be declared with a data type before it can be used. Once it is declared, the data type of the variable cannot change.

See the following:

Types, Values, and Variables

Strong typing

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM