String中"+"的问题(null+null)

原来缘来 2011-08-17 01:03:08

public class NullTest {
public static void main(String[] args) {
String str1=null;
String str2=null;
String str3=str1+str2;
System.out.println(str3);//问题1,别运行,打印什么?
//问题2,str3存储在哪?
//问题3System.out.println(str3==?);这里的?写什么才会打印true
}
}


刚才不小心发到javaee板块去了
...全文
207 10 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
橙子伯爵 2011-08-17
  • 打赏
  • 举报
回复

1null
2内存的某个角落,无需知道,纯属给自己找麻烦
3null试试
龙四 2011-08-17
  • 打赏
  • 举报
回复
龙四 2011-08-17
  • 打赏
  • 举报
回复
这问题又是那个月经贴问题了。。。。


在论坛那个搜索框里搜索,会发现每月至少两贴问这个问题


[Quote=引用 6 楼 jayyounger 的回复:]

第一题答案
引用 3 楼 goldenfish1919 的回复:
Any type may be converted to type String by string conversion.
A value x of primitive type T is first converted to a reference value as if by
giving it as an argu……
[/Quote]
龙四 2011-08-17
  • 打赏
  • 举报
回复
String str1=null;
String str2=null;
String str3=str1+str2;


等价于:
String str3 = new StringBuilder().append((String)null).append((String)null).toString();


append(String)的源码:

public AbstractStringBuilder append(String str) {
if (str == null) str = "null";
int len = str.length();
ensureCapacityInternal(count + len);
str.getChars(0, len, value, count);
count += len;
return this;
}
原来缘来 2011-08-17
  • 打赏
  • 举报
回复
第一题答案
[Quote=引用 3 楼 goldenfish1919 的回复:]
Any type may be converted to type String by string conversion.
A value x of primitive type T is first converted to a reference value as if by
giving it as an argument to an appropriate class instanc……
[/Quote]

String str1="a";
String str2="b";
String str3=str1+str2;
str3=="ab";为什么是false
龙四 2011-08-17
  • 打赏
  • 举报
回复
//问题3System.out.println(str3.intern()=="nullnull");这里的?写什么才会打印true
softroad 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 goldenfish1919 的回复:]

Any type may be converted to type String by string conversion.
A value x of primitive type T is first converted to a reference value as if by
giving it as an argument to an appropriate class instan……
[/Quote]

学习了。
若鱼1919 2011-08-17
  • 打赏
  • 举报
回复
Any type may be converted to type String by string conversion.
A value x of primitive type T is first converted to a reference value as if by
giving it as an argument to an appropriate class instance creation expression:
• If T is boolean, then use new Boolean(x) (§20.4).
• If T is char, then use new Character(x) (§20.5).
• If T is byte, short, or int, then use new Integer(x) (§20.7).
• If T is long, then use new Long(x) (§20.8).
• If T is float, then use new Float(x) (§20.9).
• If T is double, then use new Double(x) (§20.10).
This reference value is then converted to type String by string conversion.

Now only reference values need to be considered. If the reference is null, it is
converted to the string "null" (four ASCII characters n, u, l, l). Otherwise, the
conversion is performed as if by an invocation of the toString method of the referenced
object with no arguments; but if the result of invoking the toString
method is null, then the string "null" is used instead. The toString method
(§20.1.2) is defined by the primordial class Object (§20.1); many classes override
it, notably Boolean, Character, Integer, Long, Float, Double, and String.

--java language specification 3rd p355
Mourinho 2011-08-17
  • 打赏
  • 举报
回复
1 nullnull
2 不知道
3 System.out.println(str3==?);只能填str3
如果是equals,可以填(str1+str2)和Str3
飞跃颠峰 2011-08-17
  • 打赏
  • 举报
回复
1 nullnull
2 管他存在哪:)
3 str3

62,635

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧