用新的运算符创建多少个对象? [英] How many object creates with new operator?

查看:44
本文介绍了用新的运算符创建多少个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能重复:
Java字符串:< String s = new String(” silly“);”

如果我写

String s= new String("how many object b created by this method ");

与通过这种方式创建的对象相比,将创建多少参考对象和对象:

how many reference objects and objects will be created in comparison to doing it this way:

Sting s1="Is this method is good as compare to upper"; 

推荐答案

使用 String s = new String(此方法创建了多少个对象b"); 创建了一个新对象's'字符串类,然后将字符串此方法创建的对象b的数量"传递给其构造函数.

Using String s= new String("how many object b created by this method "); creates a new object 's' of String class, and you are passing the string "how many object b created by this method" to its constructor.

String s1 ="Instant方法比上方法好".'s1'是字符串文字.关于字符串文字:

In String s1="Is this method is good as compare to upper"; 's1' is a string literal. On string literals:

每次输入您的代码创建一个字符串文字,JVM首先检查字符串文字池.如果该字符串已经存在于池,对池的引用实例返回.如果字符串确实池中不存在的新字符串对象实例化,然后放置在游泳池.Java可以做到这一点优化,因为字符串是不变的,可以共享而无需担心数据损坏.

Each time your code create a string literal, the JVM checks the string literal pool first. If the string already exists in the pool, a reference to the pooled instance returns. If the string does not exist in the pool, a new String object instantiates, then is placed in the pool. Java can make this optimization since strings are immutable and can be shared without fear of data corruption.

上述概念与字符串实习有关;所有文字字符串和字符串值常量表达式都用Java [源代码] .因此,基本上,使用 String s1 =此方法是否与上层比较好"; 仅在此方法是否与上层比较好"时才会创建新对象尚未在池中.

The above concept is related to string interning; all literal strings and string-valued constant expressions are interned in Java [source]. So basically, using String s1="Is this method is good as compare to upper"; will create a new object only if "Is this method is good as compare to upper" is not already in the pool.

这篇关于用新的运算符创建多少个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆