In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
Today, I will talk to you about how to use Object to String. Many people may not know much about it. In order to let you know more, Xiaobian summarizes the following contents for you. I hope you can gain something according to this article.
During development, small partners must often encounter converting an Object type field to a String type field. For example, let's start with a type called Map.
There are several ways to convert Object to String:
(String)object
object.toString()
Stirng.valueOf(object)
""+object
Let's not discuss which is good and which is not good for the time being, but let's seriously consider the value value obtained from the map.
If this map is given to us by the upstream system, adhering to the principle of not believing the upstream system, then the value corresponding to a key type may exist in the following cases:
value is a character string, which meets the transmission requirements
value is of another type, such as Integer
value is empty string
value is a space
value No value is passed, default is null
Map does not contain this key.
For these cases, let's analyze how Object is converted to String in detail.
(String)object
(String)object is a cast of an object of type Object to type String.
For spaces, empty strings, null can be converted, but when the value type of the Object object is not a string, such as Integer type, there will be type conversion exception errors.
public static void main(String[] args) { /** * In order to be compatible with various parameters, the value value of map is defined as Object type. * For a value of key type, its value exists in the following cases: * 1) value is a string * 2) value is an empty string * 3) Value is a space * 4) value No value is passed, default is null * 5) The map does not contain this key. */ Map mapParams = new HashMap(); TypeEntity entity = new TypeEntity(); /** * Forced type conversion */ mapParams.put("type",1); System.out.println("The result of using cast is: " + (String) mapParams.get("type")) ; }
The results of the run were:
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String at com.alipay.demo.StringTest.main(StringTest.java:30)
Summary:
If the upstream system is not your own team, try not to use this method. It is easy to report errors due to inconsistent conversion of data types.
object.toString()
object.toString() converts spaces, empty strings, and other data types. Note that null pointer exceptions occur when the value is null.
/** * toString() */ mapParams.put("type", null); System.out.println("toString() The result of conversion is: " + mapParams.get("type").toString());
The results of the run were:
Exception in thread "main" java.lang.NullPointerException at com.alipay.demo.StringTest.main(StringTest.java:39)
Summary:
We use object.toString() to determine null before converting the value obtained. There are two scenarios where the value is null: the value corresponding to the key is null or the key does not exist in the map.
Stirng.valueOf(object)
Stirng.valueOf(object) can convert null, spaces, empty strings, and other data types, and is a relatively safe conversion method.
It should be noted that when the value is null, the method will be converted to "null", which will affect the subsequent non-null judgment of the business.
Let's look at the source code for the Stirng.valueOf() method:
public static String valueOf(Object obj) { return (obj == null) ? "null" : obj.toString(); }
The valueOf() method essentially uses obj.toString(), which circumvents possible null pointer exceptions.
However, the "null" of the conversion may interfere with our business. When using this method, we need to determine whether the result is null before converting. Of course, if there is no business permission, we can not determine.
/** * Stirng.valueOf(object) */ mapParams.put("type", null); System.out.println("Stirng.valueOf(object) conversion result is: " + String.valueOf(mapParams.get("type"))); System.out.println(String.valueOf(mapParams.get("type")) == null); System.out.println("null".equals(String.valueOf(mapParams.get("type"))));
The results of the run were:
Stirng.valueOf(object) conversion results in: null false true
Summary:
Stirng.valueOf(object) is a safer conversion method, but special attention should be paid to converting it to "null" when the value is null to avoid interference with the service. It is best to add judgment when using it.
""+object
""+object This method converts other types of data to strings by using the concatenation feature of strings. It is similar to Stirng.valueOf(object). It does not consider spaces, empty strings, null, and other data types, but it needs to be noted that null will be converted to "null" when the value is null.
/** * ""+object */ mapParams.put("type", null); System.out.println(""+mapParams.get("type")); System.out.println((""+mapParams.get("type")) == null); System.out.println("null".equals((""+mapParams.get("type"))));
The results of the run were:
null false true After reading the above, do you have any further understanding of how to use Object to String? If you still want to know more knowledge or related content, please pay attention to the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.