Why can't Integer be distinguished from String in java?

I found a strange exception:

java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String 

How is this possible? Each object can be sent to String, right?

The code:

 String myString = (String) myIntegerObject; 

Thank.

+75
java string casting integer
Jan 23 2018-12-12T00:
source share
11 answers

Why this is not possible:

Because String and Integer are not in the same hierarchy of objects.

  Object / \ / \ String Integer 

The casting you are trying to work only if it is in the same hierarchy, for example

  Object / / A / / B 

In this case, (A) objB or (Object) objB or (Object) objA .

Therefore, as mentioned earlier, to convert an integer to using a string:

String.valueOf(integer) or Integer.toString(integer) for the primitive,

or

Integer.toString() for the object.

+125
Jan 23 2018-12-23T00:
source share

No, Integer and String are different types. To convert an integer to a string, use: String.valueOf(integer) or Integer.toString(integer) for the primitive or Integer.toString() for the object.

+44
Jan 23 '12 at 14:45
source share

For int types, use:

 int myInteger = 1; String myString = Integer.toString(myInteger); 

For Integer types, use:

 Integer myIntegerObject = new Integer(1); String myString = myIntegerObject.toString(); 
+19
Jan 23 2018-12-23T00:
source share

No. Each object can be sent to java.lang.Object , and not to String . If you need a string representation of any object, you must call the toString() method; this is not the same as pouring an object into a string.

+5
Jan 23 '12 at 14:45
source share

You cannot explicitly pass anything to a String that is not a String . You should use either:

 "" + myInt; 

or

 Integer.toString(myInt); 

or

 String.valueOf(myInt); 

I prefer the second form, but I consider it a personal choice.

Edit OK, that's why I prefer the second form. The first form at compilation could create an instance of StringBuffer (in Java 1.4) or StringBuilder in 1.5; one more thing is to collect garbage. As far as I can tell, the compiler is not optimizing this. The second form also has an analog Integer.toString(myInt, radix) , which allows you to specify whether you want hexadecimal, octal, etc. If you want to be consistent in your code (purely aesthetically, I suppose), the second form can be used in more places.

Edit 2 I assumed that you meant that your integer was int , not Integer . If it already has an Integer , just use toString() on it and do it.

+5
Jan 23 2018-12-23T00:
source share

You should call myIntegerObject.toString () if you need a string representation.

+4
Jan 23 '12 at 14:45
source share

Objects can be converted to a string using the toString() method:

 String myString = myIntegerObject.toString(); 

There is no such rule about casting. In order for casting to work, the object must be of the type you are using.

+3
Jan 23 2018-12-23T00:
source share

Casting is different from converting to Java to use informal terminology.

Listing an object means that the object is already the one you are attaching it to, and you just tell the compiler about it. For example, if I have a Foo reference that I know is an instance of FooSubclass , then (FooSubclass)Foo tells the compiler: "Do not modify the instance, just know that it is actually a FooSubclass .

Integer , on the other hand, is not a String , although (as you point out) there are methods for getting a String that represents an Integer . Since no Integer instance can be a String , you cannot drop Integer to String .

+1
Jan 23 2018-12-23T00:
source share

In your case, casting is not required, you need to call toString ().

 Integer i = 33; String s = i.toString(); //or s = String.valueOf(i); //or s = "" + i; 

Casting. How it works?

Given:

 class A {} class B extends A {} 

(BUT)
|
(B)

 B b = new B(); //no cast A a = b; //upcast with no explicit cast a = (A)b; //upcast with an explicit cast b = (B)a; //downcast 

A and B in the same inheritance tree, and we can do this:

 a = new A(); b = (B)a; // again downcast. Compiles but fails later, at runtime: java.lang.ClassCastException 

The compiler must allow anything that can run at runtime. However, if the compiler knows with 100% that the throw cannot work, compilation will fail.
Given:

 class A {} class B1 extends A {} class B2 extends A {} 

(BUT)
/ \
(B1) (B2)

 B1 b1 = new B1(); B2 b2 = (B2)b1; // B1 can't ever be a B2 

Error: Non-convertible types B1 and B2. The compiler knows with 100% that the throw cannot work. But you can trick the compiler:

 B2 b2 = (B2)(A)b1; 

but anyway at runtime:

Exception in thread "main" java.lang.ClassCastException: B1 cannot be attributed to B2

in your case:

(an object)
/ \
(Integer) (String)

 Integer i = 33; //String s = (String)i; - compiler error String s = (String)(Object)i; 

at runtime: exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be passed to java.lang.String

+1
Dec 21 '16 at 7:57
source share

Use String.valueOf (integer) .

It returns a string representation of an integer.

0
Jan 23 2018-12-23T00:
source share

Use .toString as shown below:

 String myString = myIntegerObject.toString(); 
0
Mar 02 '17 at 23:33
source share



All Articles