Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

I created a "Color Chooser" with three textboxes where the user defines rgb values.
To check if values entered are correct (only numbers between 0-255) I'm using the following:

public Color getColor() {     if (tfRed.getText().equals("") || tfGreen.getText().equals("") || tfBlue.getText().equals("")) {                 return new Color(0, 0, 0, 0);     } else {         if (tfRed.getText().matches("\\d+") && tfGreen.getText().matches("\\d+") && tfBlue.getText().matches("\\d+")) {             // ...         } else {             return new Color(0, 0, 0, 0);         }     } } 

What I'm asking: is it better to use String.isEmpty()? I never found a satisfying answer and I've always wondered if there is any difference.

like image 262
mastaH Avatar asked Jul 26 '11 10:07

mastaH


People also ask

What is the difference between isEmpty and null?

The Java programming language distinguishes between null and empty strings. An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters.

What is string isEmpty?

The isEmpty() method checks whether a string is empty or not. This method returns true if the string is empty (length() is 0), and false if not.

What is the difference between isBlank and isEmpty?

isBlank() vs isEmpty() The difference between both methods is that isEmpty() method returns true if, and only if, string length is 0. isBlank() method only checks for non-whitespace characters. It does not check the string length.


1 Answers

I think isEmpty() is a bit more efficient. However a smart compiler may optimize the equals("") call anyway. From the OpenJDK source:

  671     public boolean isEmpty() {   672         return count == 0;   673     }   1013     public boolean equals(Object anObject) {  1014         if (this == anObject) {  1015             return true;  1016         }  1017         if (anObject instanceof String) {  1018             String anotherString = (String)anObject;  1019             int n = count;  1020             if (n == anotherString.count) {  1021                 char v1[] = value;  1022                 char v2[] = anotherString.value;  1023                 int i = offset;  1024                 int j = anotherString.offset;  1025                 while (n-- != 0) {  1026                     if (v1[i++] != v2[j++])  1027                         return false;  1028                 }  1029                 return true;  1030             }  1031         }  1032         return false;  1033     } 

Also the answer here on whether to use str.isEmpty() or "".equals(str) is spot on:

The main benefit of "".equals(s) is you don't need the null check (equals will check its argument and return false if it's null), which you seem to not care about. If you're not worried about s being null (or are otherwise checking for it), I would definitely use s.isEmpty(); it shows exactly what you're checking, you care whether or not s is empty, not whether it equals the empty string

like image 191
Charles Goodwin Avatar answered Nov 07 '22 11:11

Charles Goodwin