NULL을 체크할때 유용한 지식 [Java]

Kyle_Kim·2022년 4월 27일
0

Question

Difference Between String.isEmpty() and String.equals("")

isEmpty는 그저 String의 길이를 체크한다.(len이 0일경우 true)

Since isEmpty() checks if the length of the String is 0 and "" is the only String with length 0, each String for which isEmpty() returns true would also return true to .equals(""). So technically, they do the same thing.

성능의 차이는 많지 않다

There might be a minimal difference in performance, but I wouldn't bother about that at all (I'd be very surprised if it were noticeable in production code).

isEmpty() is null-safe

Another difference is if you wrote "".equals(someString), then it would be "null-safe". In other words: if someString was null, this construct would simply evaluate to false and not throw a NullPointerException. If, however, you have someString.equals("") then this would not apply.

The most important difference is how it's read: isEmpty() makes the intention very clear: you want to treat empty strings differently. .equals("") is ever so slightly less clear ("if that string equals that other string, that happens to be empty").

profile
Make Things Right

0개의 댓글