๋ฐ์ํ
Java Switch NPE ํผํ๊ธฐ
String str = null;
switch (str) {
case "one":
System.out.println("1");
break;
case "two":
System.out.println("2");
break;
default:
System.out.println("default");
break;
}
- ์์ ๊ฐ์ ์ฝ๋์์ switch(์กฐ๊ฑด) -> (์กฐ๊ฑด)์ ๋ค์ด๊ฐ๋ ๋ณ์๊ฐ null์ผ ๊ฒฝ์ฐ NPE๊ฐ ๋ฐ์ํ๋ค.
- ๋น์ฐํ ๊ฒฐ๊ณผ์ผ์๋ ์์ง๋ง, ์๊ฐ๋ณด๋ค ๋์น ์ ์๋ ๋ถ๋ถ์ด๋ผ ๊ธ๋ก ๋จ๊ฒจ ๋์ผ๋ ค๊ณ ํ๋ค.
- ๋ํ, NPE๋ฅผ ํผํ๊ธฐ ์ํ ๋ฐฉ๋ฒ ๋ช๊ฐ์ง๋ฅผ ์๊ฐํ๋ค.
- ์ฌ์ค ๋ค ์ฉ ๋ง์ ๋๋ ๋ฐฉ๋ฒ์ ์๋๋ผ, Pattern Matching ๊ธฐ๋ฅ์ด ๋์ ๋ switch๋ฌธ์ ์ฌ์ฉ(java 17 ์ดํ ๋ฒ์ ์ฌ์ฉ ๊ฐ๋ฅ. ๋จ preview ์ํ)ํ๋ ๊ฒ ์๋๋ผ๋ฉด NPE์ด ๋ฐ์ํ ์ ์๋ ์ํฉ์๋ if๋ฌธ์ ์ฌ์ฉํ๋ ๊ฒ ์ณ์ ๋ณด์ธ๋ค.
1. if ์ฌ์ฉ (์ด๋ด๊ฑฐ๋ฉด ๊ทธ๋ฅ if๋ฌธ ์ธ๊ฑธ..)
null์ด๋ผ๋ฉด ๋ค๋ฅธ ๊ฐ์ผ๋ก ์ค์ ํ ํ switch๋ฌธ์ ์ํํ๋ค.
String str = null;
// default ๊ฐ์ผ๋ก ๋น ์ง๊ฒ if๋ฌธ์์ ๊ฐ ์ง์ ํด์ฃผ๊ธฐ
if (str == null) {
str = "default";
}
switch (str) {
case "one":
System.out.println("1");
break;
case "two":
System.out.println("2");
break;
default:
System.out.println("default");
break;
}
2. ์ผํญ์ฐ์ฐ์ ์ฌ์ฉ
null์ด๋ผ๋ฉด ๋ค๋ฅธ ๊ฐ์ผ๋ก ์ค์ ํ ํ switch๋ฌธ์ ์ํํ๋ค.
String str = null;
switch (str == null ? "NULL" : str) { // ์ผํญ์ฐ์ฐ์
case "one":
System.out.println("1");
break;
case "two":
System.out.println("2");
break;
default:
System.out.println("default");
break;
}
3. String.valueOf
String.valueOf ๋ฉ์๋๋ฅผ ์ฌ์ฉํด null ๊ฐ์ "null"๋ก ๋ณํํ๋ค.
/**
* Returns the string representation of the {@code Object} argument.
*
* @param obj an {@code Object}.
* @return if the argument is {@code null}, then a string equal to
* {@code "null"}; otherwise, the value of
* {@code obj.toString()} is returned.
* @see java.lang.Object#toString()
*/
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
String str = null;
switch (String.valueOf(str)) { // ์ผํญ์ฐ์ฐ์
case "one":
System.out.println("1");
break;
case "two":
System.out.println("2");
break;
case "null":
default:
System.out.println("default");
break;
}
4. pattern matching for switch (Java 17 - preview feature)
case ๋ฌธ์ null์ธ ๊ฒฝ์ฐ๋ฅผ ์ถ๊ฐํด ํด๊ฒฐํ๋ค.
static void testFooBar(String s) {
switch (s) {
case null -> System.out.println("Oops");
case "Foo", "Bar" -> System.out.println("Great");
default -> System.out.println("Ok");
}
}
static String formatterJava17(Object o) {
return switch (o) {
case Integer i -> String.format("int %d", i);
case Long l -> String.format("long %d", l);
case Double d -> String.format("double %f", d);
case String s -> String.format("String %s", s);
default -> o.toString();
};
}
static void testTriangle2(Shape s) {
switch (s) {
case null ->
{}
case Triangle t && (t.calculateArea() > 100) ->
System.out.println("Large triangle");
case Triangle t ->
System.out.println("Triangle");
default ->
System.out.println("Unknown!");
}
}
๐ Reference
๋๋ณด๊ธฐ
https://velog.io/@ljo_0920/java-%EB%B2%84%EC%A0%84%EB%B3%84-%EC%B0%A8%EC%9D%B4-%ED%8A%B9%EC%A7%95
https://mkyong.com/java/java-12-switch-expressions/
https://mkyong.com/java/java-13-switch-expressions/
https://mkyong.com/java/what-is-new-in-java-17/
https://blog.naver.com/PostView.naver?blogId=wideeyed&logNo=222405432233
https://stackoverflow.com/questions/10332132/how-to-use-null-in-switch
๋ฐ์ํ