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.

Pshemo
  • 122,468
  • 25
  • 185
  • 269
TonyW
  • 18,375
  • 42
  • 110
  • 183
  • 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 – MadProgrammer Nov 01 '13 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 – Richard Tingle Nov 01 '13 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 ? – njzk2 Nov 01 '13 at 19:45
  • @njzk2 http://meta.stackexchange.com/q/4283/133242 – Matt Ball Nov 01 '13 at 19:56

6 Answers6

4

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

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

igr
  • 3,409
  • 1
  • 20
  • 25
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!"

Matt Ball
  • 354,903
  • 100
  • 647
  • 710
  • 1
    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). – Surveon Nov 01 '13 at 19:45
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.

kviiri
  • 3,282
  • 1
  • 21
  • 30
  • 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. – TonyW Nov 01 '13 at 19:47
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 "".

Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
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.

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.

Math
  • 3,334
  • 4
  • 36
  • 51