String中"+"的问题(nu)

原来缘来 2011-08-17 01:00:26

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
}
}
...全文
267 24 打赏 收藏 转发到动态 举报
写回复
用AI写文章
24 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
君麻吕
  • 打赏
  • 举报
回复
guoyang842 2011-08-18
  • 打赏
  • 举报
回复
他蔡依林怎么补知道你是周杰伦呢
原来缘来 2011-08-17
  • 打赏
  • 举报
回复
你怎么知道我是周杰伦的
原来缘来 2011-08-17
  • 打赏
  • 举报
回复
第二题答案~~~~不是吧
原来缘来 2011-08-17
  • 打赏
  • 举报
回复
我是柯南啊
飞跃颠峰 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 17 楼 jayyounger 的回复:]

引用 10 楼 ticmy 的回复:
String str1=null;
String str2=null;
String str3=str1+str2;


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


append(String)……
[/Quote]

周杰伦同学,身为第一个答且答对两题的人,我竟然1分都木有
原来缘来 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 ticmy 的回复:]
String str1=null;
String str2=null;
String str3=str1+str2;


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


append(String)的源码:

Java code……
[/Quote]

Java 语言提供对字符串串联符号("+")以及将其他对象转换为字符串的特殊支持。字符串串联是通过 StringBuilder(或 StringBuffer)类及其 append 方法实现的。字符串转换是通过 toString 方法实现的,该方法由 Object 类定义,并可被 Java 中的所有类继承。有关字符串串联和转换的更多信息,请参阅 Gosling、Joy 和 Steele 合著的 The Java Language Specification。

我竟然刚知道
晓风o残月 2011-08-17
  • 打赏
  • 举报
回复
应该是null
小石榴 2011-08-17
  • 打赏
  • 举报
回复
qybao 2011-08-17
  • 打赏
  • 举报
回复
1
String对象相加,内部会使用StringBuilder来操作,就像10L说的
所以 str1+str2 会变成 StringBuilder.apennd("null").append("null")

2
str3保存在堆中

3
从1可以知道,str3是由StringBuilder对象的toString方法在堆中生成的对象,除了str3外没有其他引用变量引用该对象,所以只能自己==自己,才能返回 true

kevin_520_kevin 2011-08-17
  • 打赏
  • 举报
回复
怎么想起来这样的问题了,存储的问题就是涉及到堆栈,只要清楚就差不多吧
jiahaibing880701 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 yxk19890903 的回复:]

引用 4 楼 tkd03072010 的回复:
1.nullnull
2.存在你的机机的内内上
3.str3

存在你JJ的内内上 你真搞笑 哈哈
[/Quote]
这位哥太有才了
风火轮 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tkd03072010 的回复:]
1.nullnull
2.存在你的机机的内内上
3.str3
[/Quote]
存在你JJ的内内上 你真搞笑 哈哈
龙四 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;
}




http://so.csdn.net/search?q=String+%E7%9B%B8%E7%AD%89&sort=&rt=h&t=thread
guoyang842 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 tkd03072010 的回复:]
1.nullnull
2.存在你的机机的内内上
3.str3
[/Quote]
+1
senlinzhiwang 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 peng_hao1988 的回复:]
null代表内存地址上为空,没有存放东西,但是null却也是一个值,所以str1+str2就是将两个值拼接起来,故为nullnull,str3是在内存中另一块内存中,存放的是str1+str2 的拷贝值。
[/Quote]

+1
桃园闲人 2011-08-17
  • 打赏
  • 举报
回复
null代表内存地址上为空,没有存放东西,但是null却也是一个值,所以str1+str2就是将两个值拼接起来,故为nullnull,str3是在内存中另一块内存中,存放的是str1+str2 的拷贝值。
softroad 2011-08-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 alexandertech 的回复:]

1 nullnull
2 管他存在哪:)
3 str3
[/Quote]

+1
zhao88148201 2011-08-17
  • 打赏
  • 举报
回复
插旗 等下来看
加载更多回复(4)

67,513

社区成员

发帖
与我相关
我的任务
社区描述
J2EE只是Java企业应用。我们需要一个跨J2SE/WEB/EJB的微容器,保护我们的业务核心组件(中间件),以延续它的生命力,而不是依赖J2SE/J2EE版本。
社区管理员
  • Java EE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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