1

I don'd understand the difference between a null String and an empty String in Java.

Suppose String a = null, and I want to see if a is null by using a.isEmpty() or a.equals("null"). Neither of these works. I have to use a == null to tell that if a is null or not.

From my understanding, if you use == between two strings, you are actually comparing their addresses or references in memory? Is this understanding right? I hope someone can clarify my confusion. Thanks in advance.

4
  • value == null tests the object reference to see if its null or not. If it's null, you cannot access the object properties like equals or isEmpty, as the reference points to nothing. The best option is to check for null first to avoid the possibility of a NullPointerException Commented Nov 1, 2013 at 19:44
  • See the duplicate, but isEmpty is the same as a string of length 0, null means there is no string at all Commented Nov 1, 2013 at 19:44
  • 1
    off topic, but why do people answer to this question, since it has been asked, properly answered, and correctly identified as duplicate ? Commented Nov 1, 2013 at 19:45
  • @njzk2 meta.stackexchange.com/q/4283/133242 Commented Nov 1, 2013 at 19:56

6 Answers 6

4

null ==> has no reference (no String is referenced)

"" ==> has a reference that points to string (which happen to be empty)

Sign up to request clarification or add additional context in comments.

Comments

1

You always use == when checking for any null reference. You can't use Object#equals() because, you can't call an instance method if you don't have an instance, and null specifically means "there is no instance!"

1 Comment

Noteworthy is that you CAN pass a null reference to equals() since by convention it should be able to handle null (evaluate it to false).
0

You're correct: == tests whether two Strings are in fact references to the same String object.

An empty String object is exactly what it says on the tin, while null refers to the lack of an object reference whatsoever. With an empty String, you can call all the String methods and generally treat it like any other String - but with null there is no object to call the methods on so you get a NullPointerException.

1 Comment

NullPointerException was exactly what I got when running my code. This exception was not gone until I check by a == null. Thanks for all the replies! will check the duplicate thread.
0

This:

a.equals("null")

compares the strin with the string "null".

null is a reserved keyword in Java, that means that variable doesn't refer to anything.

a.isEmpty()

checks if the string has no characters. i.e. it is equal to "".

Comments

0

An empty string is literally just "" whereas null is a reference to nothing. So anything that is null has not been instantiated but something that is empty exists but just doesn't contain any values.

The thing to remember in Java is that String is an object not a raw datatype like int or double. The "==" only compares equality between values not object references so for Strings and other objects you need to use .equals.

Comments

0

String is a reference variable to a String object in the memory. If the String is null it means it is not referencing any String object.

In a.equals("null"); you are comparing the value of the String object in the memory to the literal "null". To check whether a String variable is null (means not reference a String object in the memory) use a == null.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.