Loading...

Methods of Converting Java Objects To String

Methods of Converting Java Objects To String

Methods of Converting Java Objects To String

Method 1: Use the Object#toString() method – Vancouver app Developer

Consider the following example:

Object object = getObject();  
System.out.println(object.toString());

In this method, there is already a public method .toString() in java. lang. Object class, this method can be called on any java object in the strict sense. But be careful when using it, you must ensure that the object is not a null value, otherwise, a NullPointerException will be thrown. When using this method, usually derived classes will override the toString() method in Object.

Method 2: Use type conversion (String) object method

This is a standard type conversion, which converts the object into a String type value. When using this method, it is important to note that the type must be convertible to the String type. Therefore, it is best to use an instance of to do a type check to determine whether it can be converted. Otherwise, it is easy to throw CalssCastException. In addition, you need to be especially careful because the syntax check will not report an error when an object defined as an Object type is converted into a String, which may lead to potential errors. Be extra careful at this time. like:

Object obj = new Integer(100);
String strVal = (String)obj;

An error will occur at runtime because the cast of the Integer type to the String type cannot be passed.

Integer obj = new Integer(100);
String strVal = (String)obj;

If it is a format code, a syntax error will be reported.

In addition, because the null value can be coerced to any java class type, (String)null is also legal, so there will be no null pointer problem.

Method 3: Use String.valueOf(Object) method – Mobile App Development

The basis of String.valueOf(Object) is Object#toString(). But it is different from Object#toString(). As mentioned in the analysis of Method 1 above, it is necessary to ensure that it is not null when using the latter. But when the third method is adopted, there is no need to worry about whether the object is a null value. In order to explain the problem, let’s analyze the relevant source code. The source code of String# valueOf(Object) in Jdk is as follows:

public static String valueOf(Object obj) {
return (obj == null) ? “null” : obj.toString();
}

From the above source code, you can clearly see the reason why you don’t need to worry about null values. However, this also gives us hidden dangers. We should note that when the object is null, the value of String.valueOf(object) is the string “null”, not null! Remember to pay attention during use. Imagine if we use if(String.valueOf(object)==null){System.out.println(“The value passed in is null!”);} what problems might happen. Think about it again, when outputting to the console, visually, what is the difference in the execution result of the following statement:

System.out.println(String.valueOf(null));
System.out.println(null);

The output we see will be exactly the same thing: null, but they have different meanings

 

Leave a Reply

Your email address will not be published.