String str1;
String str2 = null;
String str3 = "";
String str4 = new String();
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
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 asnull
, is it all-0-bits or the bytecode ofnull
is all-zero?
This has already been asked (and answered):
Another question is that if I call the method
toString()
onstr2
bystr2.toString()
, I got a NullPointer Error at runtime. So it is JVM that checks if the reference variable isnull
?
Yes.
How can JVM know that it is
null
? JVM checks the bits instr2
?
Yes.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With