In my computer science class we were discussing null and values when we came across a predicament. We could not figure out the value of simply 2 quotes with no space as "". Just wondering if anyone would know what the exact value of "". Thanks
-
consider `String test = ""; System.out.println(test.hashCode());` – Scary Wombat Dec 08 '16 at 04:55
-
http://stackoverflow.com/questions/30458491/double-quotes-only-in-java-string-without-space – soorapadman Dec 08 '16 at 04:56
2 Answers
It's the empty String
. JLS-3.10.5. String Literals says A string literal consists of zero or more characters enclosed in double quotes. And, then includes this example
The following are examples of string literals:
"" // the empty string
As Java String
(s) are immutable, it has a constant length of 0
and stores a sequence of 0
characters.

- 1
- 1

- 198,278
- 20
- 158
- 249
When you initialize it with null
like this:
String s1 = null;
It means that no object is assigned to variable s1
. However, when you initialize it with empty String like this:
String s2 = "";
It means that a String object is assigned to s2
(although empty).
Now if you want to perform any operation, let's say you want to call the .equals(..)
method of string, then it will throw NullPointerException
in the former case as there is not String object. However, it will work fine in latter because there is an object there, it doesn't matter whether it's empty or not
s1.equals("something"); // throws NullPointerException
s2.equals("something"); // works fine

- 30,180
- 9
- 58
- 71