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.
'프로그래밍 언어 ( Programming Language ) > Java' 카테고리의 다른 글
[프로그래밍언어][Java(자바)] 연산자 - 종류, 우선순위, 결합방향 (0) | 2021.12.23 |
---|---|
[프로그래밍언어][Java(자바)] 자료형, 변수형, 데이터 타입 - 종류, 크기, 범위 (0) | 2021.12.23 |
[프로그래밍언어][Java(자바)] (Class) BufferedReader, BufferedWriter - 입출력 (0) | 2021.12.13 |
[프로그래밍언어][Java(자바)] (Class) Scanner - 입력, 입력값이 0이면 종료, 입력값이 없으면 종료, 숫자 입력 받은 후 문자열 입력 받기 (0) | 2021.12.13 |
[프로그래밍언어][Java(자바)] (Class) Integer - 문자열로 변환, 값 비교, 진법 변환 등 (0) | 2021.12.13 |