[Java] Convert Object type null to String type

[Java] Convert Object type null to String type

It is necessary to convert the value acquired by Object type to String type toString (), String.valueOf (), cast conversion, etc. There are several ways to convert, but are there any differences? Next, And what I was most interested in was when the acquired Object type value was null. I wondered if an error would occur, so I looked it up. If you put the result first ...

[Convert Object type null by toString (), String.valueOf (), cast conversion]
toString() String.valueOf() Cast conversion
NullPointerException String"null" null

If you check with the program below,


public static void main(String[] args) {
	Object testNull = null; //Object type null creation
	String testNullString1 = testNull.toString(); // toString()Conversion by
	String testNullString2 = String.valueOf(testNull); // String.valueOf()Conversion by
	String testNullString3 = (String) testNull; //Cast conversion

	//Store each converted value in Map
	LinkedHashMap<String,String> map= new LinkedHashMap<>();
	map.put("Cast conversion",testNullString3);

	//Output each conversion result
	for(Map.Entry<String, String> mapString : map.entrySet() ) {
		if(mapString.getValue() == null) {
			System.out.println(mapString.getKey() +"Converted with, it becomes null.");
		}else if(mapString.getValue().equals("null")) {
			System.out.println(mapString.getKey() +"Converted with\"null\"become.");
		}else {
			System.out.println(mapString.getKey() +"Converting with is something else.");

Execution result 1

Exception in thread "main" java.lang.NullPointerException
	at Test_CastNull.main(Test_CastNull.java:11)

"NullPointerException" occurs in toString () conversion. It seems safe not to use toString (). Then, comment out the toString () part of the above test code and execute it again.

Execution result 2

String.valueOf()Converted with"null"become.
Converted by cast conversion, it becomes null.

So, of course, you need to be careful when handling null (the one that can be).

Recommended Posts

[Java] Convert Object type null to String type
Convert String type to Timestamp type
[Java] How to convert a character string from String type to byte type
I tried to convert a string to a LocalDate type in Java
Type conversion from java BigDecimal type to String type
[Java] How to convert from String to Path type and get the path
Convert from C String pointer to Swift String type
<java> Read Zip file and convert directly to string
[Java] How to convert one element of a String type array to an Int type
Sample code to convert List to List <String> in Java Stream
Convert Java Powerpoint to XPS
Convert to Ruby Leet string
Convert Serializable Object to byte []
How to convert Java radix
[Java] Convert ArrayList to array
Convert a Java byte array to a string in hexadecimal notation
Initialization with an empty string to an instance of Java String type
Convert ruby object to JSON format
I want to convert InputStream to String
[Java] Convert 1-to-N List to Map
Convert iso-2022-jp character string to utf-8
[Java] Correct comparison of String type
[Android] Convert Android Java code to Kotlin
[Java] Convert array to ArrayList * Caution
How to use Java enum type
Java string
[Convenient to remember !!!] How to convert from LocalDate type to character string and from character string to LocalDate type
Java date data type conversion (Date, Calendar, String)
Regarding String type equivalence comparison in Java
Convert all Android apps (Java) to Kotlin
[Java] Data type / string class cheat sheet
Notes on operators using Java ~ String type ~
How to output Java string to console screen
Convert SVG files to PNG files in Java
[Java] Comparison of String type character strings
Kotlin's Null safety to send to Java developers
Java 8 LocalDateTime type conversion stuff (String, java.util.Date)
Java type conversion
[Java] How to test for null with JUnit
Convert to a tag to URL string in Rails
[Introduction to Java] About type conversion (cast, promotion)
[Android] How to convert a character string to resourceId
[Java] String padding
[Java] Enumeration type
Java Optional type
Code to escape a JSON string in Java
[Java] Convert DB code to code value using enum
Convert a string to a character-by-character array with swift
How to convert a solidity contract to a Java contract class
How to hide null fields in response in Java
Java double type
Java string processing
[Java] Introduction to Java
JAVA NULL checker
Convert Java nested beans to aaa.bbb [0] .ccc format
Java type conversion (String, int, Date, Calendar, etc.)
Introduction to java
[Java] Object class
[Java] char type can be cast to int type
Split string (Java)
How to write Java String # getBytes in Kotlin?