[프로그래밍언어][Java(자바)] Null 처리 (Casting, Object.toString(), String.toString() 비교)

728x90

 

class Main {

    public static String valueOf2(Object obj) {
        return obj == null ? "null" : obj.toString();
    }

    public static String defaultValueOf(Object obj, String defaultValue) {
        return obj == null ? defaultValue : obj.toString();
    }

    public static String defaultEmptyValueOf(Object obj) {
        return obj == null ? "" : obj.toString();
    }

    public static String defaultEmptyValueOf2(Object obj, String defaultValue) {
        return String.valueOf(obj).replaceAll("null","");
    }
    
    public static void main(String[] args) {

        Object obj = null;
        String str1 = (String) obj;        // null
        String str2 = obj.toString();      // NullPointerException
        String str3 = String.valueOf(obj); // "null"
        
        String str4 = null;
        
        String str5 = "";
        
        System.out.println(str1);  // "null"
        System.out.println(str2);  // NullPointerException
        System.out.println(str3);  // "null"
        System.out.println(str4);  // "null"
        System.out.println(str5);  // ""
        
        System.out.println(str1.isEmpty());  // NullPointerException
        System.out.println(str1.length());   // NullPointerException
        
        System.out.println(str2.isEmpty());  // NullPointerException
        System.out.println(str2.length());   // NullPointerException
        
        System.out.println(str3.isEmpty());  // false
        System.out.println(str3.length());   // 4
        
        System.out.println(str4.isEmpty());  // NullPointerException
        System.out.println(str4.length());   // NullPointerException
        
        System.out.println(str5.isEmpty());  // true
        System.out.println(str5.length());   // 0
        
    }
}

 

1. Casting

 

2. Object.toString()

https://docs.oracle.com/javase/8/docs/api/

 

Java Platform SE 8

 

docs.oracle.com

Modifier and Type Method and Description
String toString()
Returns a string representation of the object.

Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

 getClass().getName() + '@' + Integer.toHexString(hashCode())

 

3. String.valueOf(Object obj)

https://docs.oracle.com/javase/8/docs/api/java/lang/String.html

 

String (Java Platform SE 8 )

Compares two strings lexicographically. The comparison is based on the Unicode value of each character in the strings. The character sequence represented by this String object is compared lexicographically to the character sequence represented by the argum

docs.oracle.com

Modifier and Type Method and Description
static String valueOf(Object obj)
Returns the string representation of the Object argument.

Parameters:
    obj - an Object.

Returns:
    if the argument is null, then a string equal to "null"; otherwise, the value of obj.toString() is returned.

 

반응형