8
  1. String str1;
  2. String str2 = null;
  3. String str3 = "";
  4. String str4 = new String();
  5. String str5 = new String("");

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.

What is the difference between 1. and 2.? If I consider str1 as a pointer variable, what it stores is a particular memory address that is never used by the JVM or OS?

Is there a difference between 4. and 5.?

When I print str1 and str2 directly by System.out.println(str1) and System.out.println(str2), for str1, I can't even pass the compilation.For str2, compilation is OK and I get "null" and the output in the console window. Why?

Edited after the answer of @aioobe: more questions:

I would like to know more about "null". Since str2(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 str2 by str2.toString(), 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 str2?

One more question about null in Java: concatenation of null and a string literal

3
  • Updated my answer with a response to you edit.
    – aioobe
    Jun 7, 2015 at 14:56
  • Can anyone who voted down give some reason? cant figure it out... Jun 10, 2015 at 6:21
  • I think people believe that this has been asked before. Part of your question has been asked before, part of your question has not.
    – aioobe
    Jun 10, 2015 at 6:23

2 Answers 2

17

What is the difference between 1. and 2.? If I consider str1 as a pointer variable, what it stores is a particular memory address that is never used by the JVM or OS?

If these are fields in a class, there's no difference, since the default value for a field of a reference type (such as String) is already null.

If these are local variables (i.e. variables declared in a method) str1 will not be initialized to anything, while str2 will be initialized to null. The difference here is that a local variable can't be used until it has been initialized, so (as you seem to have discovered) you can't print str1, but you can print str2.

Is there a difference between 4. and 5.?

No, not semantically. You'll get slightly different byte code though.

When I print str1 and str2 directly by System.out.println(str1) and System.out.println(str2), for str1, I can't even pass the compilation. For str2, compilation is OK and I get "null" and the output in the console window. Why?

This seems to indicate that these are local variables. Local variables needs to be initialized before they are used.

I would like to know more about "null". Since str2 (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?

This has already been asked (and answered):

Another question is that if I call the method toString() on str2 by str2.toString(), I got a NullPointer Error at runtime. So it is JVM that checks if the reference variable is null?

Yes.

How can JVM know that it is null? JVM checks the bits in str2?

Yes.

1
  • This question is already too big (and the answer too!). Would you mind cutting/pasting that last one as a new question? I'll promise to do my best to answer... (Post a link to it here in the comments.)
    – aioobe
    Jun 8, 2015 at 9:00
1

Every variable in Java that represents an object is in fact not a value, but pointer to (address of) some place in memory that stores that variable. null is a special type of pointer that means the variable(pointer) is initialized but points to nothing. Since any two String variables are pointers if your compare them like str1 == str2 you'll check it variables points to the same instance. To compare content the pointers point to you need to use equals: str1.equals(str2), but in this case we must be sure that str1 is not null.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.