有多少String对象..?

编程入门 行业动态 更新时间:2024-10-14 22:18:12
本文介绍了有多少String对象..?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我和我的朋友正在讨论Strings,我们坚持这个:

Me and my friend was discussing on Strings and we stuck on this:

String str = "ObjectOne"+"ObjectTwo";

他说将创建三个对象,我说将创建一个对象。

He says total three Object will be created and I say one object will be created.

他对3个对象的逻辑是:一个用于ObjectOne,一个用于ObjectTwo,第三个是两个String对象的连接版本。

His logic behind 3 objects is: one for "ObjectOne" and one for "ObjectTwo" and third one is the concatenated version of two String objects.

我在一个对象后面的逻辑是在编译时,字符串对象将在字节代码中连接起来:

My logic behind one object is at compile time both the string objects will be concatenated in the byte code as:

String str = "ObjectOneObjectTwo";

在运行时,只会以这种方式创建一个对象。这背后的真相是什么。

And at run time only one object will be created in such a way. What is the truth behind this.

推荐答案

如果你写(文字或常量)

If you write(literals or constants)

String str = "ObjectOne"+"ObjectTwo";

相当于

String str = "ObjectOneObjectTwo"; // compiler optimize it so one Object

更多推荐

有多少String对象..?

本文发布于:2023-11-30 03:45:45,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1648610.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:有多少   对象   String

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!