java如何初始化字符串,Java中的字符串初始化

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?

解决方案

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值