与字符串文字相比,字符串对象的优点是什么 [英] what is the advantage of string object as compared to string literal

查看:148
本文介绍了与字符串文字相比,字符串对象的优点是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道在哪里使用字符串对象(在我的java代码中的哪种情况下)。
确定我理解了diff btwn字符串文字和字符串对象,但我想知道,因为java给了我们制作字符串对象的能力,所以必须有一些原因,在某些时候字符串对象创建会很有用。所以我想知道在哪种情况下我们可以选择字符串对象来代替字符串文字。

i want to know where to use string object(in which scenario in my java code). ok i understood the diff btwn string literal and string object, but i want to know that since java has given us the power to make string object, there must be some reason, at some point string object creation would be useful. so i want to know in which scenario can we prefer string object in place of string literal.

推荐答案

在大多数情况下,你应该使用字符串文字来避免创建不必要的对象。这实际上是项目5:避免创建有效Java的不必要的对象

In most situations, you should use String literals to avoid creating unnecessary objects. This is actually Item 5: Avoid creating unnecessary objects of Effective Java:


项目5:避免创建不必要的对象



通常适合重复使用
单个对象,而不是每次需要时创建
新的功能等效对象
。重复使用可以更快,更时尚
。如果
不可变,则可以始终重用
对象(第15项)。作为一个极端的
不做的例子,请考虑
这句话:

Item 5: Avoid creating unnecessary objects

It is often appropriate to reuse a single object instead of creating a new functionally equivalent object each time it is needed. Reuse can be both faster and more stylish. An object can always be reused if it is immutable (Item 15). As an extreme example of what not to do, consider this statement:

String s = new String("stringette"); // DON'T DO THIS!

该语句创建一个新的字符串 $ b每次执行$ b实例,
这些对象创建都不是必需的
。 String
构造函数的参数(stringette)本身就是
String实例,功能上
与所有对象$相同b $ b由构造函数创建。如果这个
用法发生在循环或
频繁调用的方法中,则可以不必要地创建数百万
的String实例
。改进版本是
,简单如下:

The statement creates a new String instance each time it is executed, and none of those object creations is necessary. The argument to the String constructor ("stringette") is itself a String instance, functionally identical to all of the objects created by the constructor. If this usage occurs in a loop or in a frequently invoked method, millions of String instances can be created needlessly. The improved version is simply the following:

String s = "stringette";

此版本使用单个字符串
实例,而不是每次执行时创建一个新的

此外,保证
对象将被在同一个虚拟
机器中运行的任何其他
代码重用,恰好包含
相同的字符串literal [JLS,3.10.5]

This version uses a single String instance, rather than creating a new one each time it is executed. Furthermore, it is guaranteed that the object will be reused by any other code running in the same virtual machine that happens to con- tain the same string literal [JLS, 3.10.5]

但是有一种情况你想使用 new String (字符串)构造函数:当你想强制子字符串复制到新的基础字符数组时,如:

There is however one situation where you want to use the new String(String) constructor: when you want to force a substring to copy to a new underlying character array like in:

String tiny = new String(huge.substring(0, 10));

这将允许大的底层 char [] 从原来的巨大的字符串中回收GC。

This will allow the big underlying char[] from the original huge String to be recycled by the GC.

这篇关于与字符串文字相比,字符串对象的优点是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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