cause
The following exception was encountered while performing a type conversion:
An Integer cannot be cast to a String.
My test code looks something like this:
public static void testClassCastException(a) {
Integer i1 = Integer.valueOf(getT()); // throw exception
Integer i2 = (Integer) getT(); / / (2) normal
Integer i3 = Integer.valueOf(getT().toString()); / / (3) is normal
}
private static <T> T getT(a) { return (T) new Integer(10); } // The difference between ① and ③ is the type of the argument in integer.valueof (). Integer.valueof () requires a String. // The difference between ① and ② lies in the way the type is cast. Copy the code
(String), toString, string.valueof
- ToString () toString() is defined in Object, so any class that inherits Object has this method.
ToString () is described in the API as:
Returns a string representation of the object. Normally, the toString method returns a string that “represents” the object as text. The result should be a concise but easy-to-read expression of information. It is recommended that all subclasses override this method.
The toString method of the Object class returns a string consisting of the name of the class (the Object is an instance of that class), the marker “@”, and an unsigned hexadecimal representation of the Object’s hash code. In other words, the method returns a string whose value is equal to: GetClass ().getName()+’@’+ inteste.tohexString (hashCode()) when defining a class, we can override the toString() method inherited from an Object to represent the basic information of the Object. However, using the toString () object cannot be null, or throws an exception. Java lang. NullPointerException.
- String.valueof () solves the problem of toString() using objects that cannot be empty. In fact, the method still uses toString() underneath. The following source code:
public static String valueOf(Object obj) {
return (obj == null)?"null" : obj.toString();
}
Copy the code
Note that if object null is used, the result returned is the string “NULL”, not the object NULL. In many cases, using string.valueof () reduces the chance of an error than using toString(), because null is not the case. If (string.valueof (o) == null){… } code like this is bound to be problematic because it will never be executed.
- (String) (String) differs from the above two methods because it is a cast. When an object is created, its type cannot be changed. A cast is just A cosmetic conversion of an object to another type, which is equivalent to A being used as B. If A can be used as B, there is no problem.
Integer o = new Integer(100);
System.out.println((String)o);
Copy the code
Cannot cast Integer to String, indicating that Integer Cannot be cast to String. And the code we started with:
Object x = new Integer(0);
System.out.println((String)x);
Copy the code
This is true at compile time, but throws an exception at run time. This is because x is ostensibly Object but actually Integer. Object can be cast to a String. So, at compile time, x is treated as Object and everyone is fine, but when it actually runs, x is detected as an Integer and of course a ClassCastException is thrown.
The safest method: Instanceof
If you do not know the type of the object, you can use the instanceof keyword to check the type of the object to perform specific services.
The result of “PS:” a instanceof b is the same as that of b.lass. IsInstance (a), which can achieve the same function and is completely equivalent.
Why can’t you cast an Integer to a String in Java
Because String and Integer are not in the same object hierarchy.
Object
/ \
/ \
String Integer
Copy the code
When you try to cast, you only cast in the same object class. Such as:
Object
/
/
A
/
/
B
Copy the code
In this case, (A)objB or (Object)objB or (Object)objA can be converted.
As others have already mentioned, integer can be converted to String using the following methods:
ValueOf (integer) or integer.tostring (integer) are used to convert primitive integers to strings. Integer object type: integer.tostring () converts toString
Reference:
Why translation | Integer can not be forced into the String in the Java | PostgreSQL DBA
Java.tostring (),(String), string.valueof difference _javA_springk column -CSDN blog
This article is formatted using MDNICE