title: String initialization in Java
body: <ol>
  <li><code>String str1;</code></li>
  <li><code>String str2 = null;</code></li>
  <li><code>String str3 = "";</code></li>
  <li><code>String str4 = new String();</code></li>
  <li><code>String str5 = new String("");</code></li>
  </ol>
  
  <p>I know that for 3rd initialization above, the the string object is initialized in the string pool and the 4th has nothing to do with the string pool.</p>
  
  <p>What is the difference between <strong>1. and 2.</strong>? If I consider <code>str1</code> as a pointer variable, what it stores is a particular memory address that is never used by the JVM or OS?</p>
  
  <p>Is there a difference between <strong>4. and 5.</strong>?</p>
  
  <p>When I print <code>str1</code> and <code>str2</code> directly by <code>System.out.println(str1)</code> and <code>System.out.println(str2)</code>, for <code>str1</code>, I can't even pass the compilation.For <code>str2</code>, compilation is OK and I get "null" and the output in the console window. Why?</p>
  
  <p>Edited after the answer of @aioobe: more questions:</p>
  
  <p>I would like to know more about "null". Since <code>str2</code>(reference variable) is like a pointer variable, there should be something (0/1 bits) in it (in the memory occupied by this pointer variable). As it is initialized as null, is it all-0-bits or the bytecode of null is all-zero? Another question is that if I call the method toString() on <code>str2</code> by <code>str2.toString()</code>, I got a NullPointer Error at runtime. So it is JVM that checks if the reference variable is null? How can JVM know that it is null? JVM checks the bits in <code>str2</code>?</p>
  
  <p>One more question about <strong>null</strong> in Java:
  <a href="https://stackoverflow.com/questions/30708308/concatenation-of-a-null-string-object-and-a-string-literal/30708333#30708333">concatenation of null and a string literal</a></p>