在Java中使用String(String)构造函数 [英] Use of the String(String) constructor in Java

查看:411
本文介绍了在Java中使用String(String)构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我读过文章和书籍,使用 String s = new String(...); 应该避免几乎所有的时间。我明白为什么,但是有什么用于使用String(String)构造函数吗?我不认为有,也没有看到任何证据,但我想知道如果任何人在SO社区知道使用。

I've read on articles and books that the use of String s = new String("..."); should be avoided pretty much all the time. I understand why that is, but is there any use for using the String(String) constructor whatsoever? I don't think there is, and don't see any evidence otherwise, but I'm wondering if anyone in the SO commmunity knows of a use.

确切重复: 在java中表达式new string的目的是什么?

推荐答案

这是一篇很好的文章:字符串构造函数被认为是无用的,原来是有用的!

This is a good article: String constructor considered useless turns out to be useful after all!


事实证明,这个构造函数在至少一种情况下实际上是有用的。如果你曾经偷看过String源代码,你会发现它不仅仅具有char数组值和字符计数的字段,而且还有String开头的偏移量。这是为了使字符串可以与其他字符串共享字符数组值,通常是调用substring()方法之一。 Java是一个非常受欢迎的工具,因为jwz从几年前开始使用Java:

It turns out that this constructor can actually be useful in at least one circumstance. If you've ever peeked at the String source code, you'll have seen that it doesn't just have fields for the char array value and the count of characters, but also for the offset to the beginning of the String. This is so that Strings can share the char array value with other Strings, usually results from calling one of the substring() methods. Java was famously chastised for this in jwz' Java rant from years back:

这个开销的唯一原因是String.substring()可以返回共享相同值数组。这样做的代价是为每个字符串对象添加8个字节不是一个净节省...

The only reason for this overhead is so that String.substring() can return strings which share the same value array. Doing this at the cost of adding 8 bytes to each and every String object is not a net savings...

除了节省空间之外,如果你有一些代码:

Byte savings aside, if you have some code like this:

// imagine a multi-megabyte string here  
String s = "0123456789012345678901234567890123456789";  
String s2 = s.substring(0, 1);  
s = null;

现在你将有一个String s2,虽然它似乎是一个单字符字符串,保存对在String中创建的巨大字符数组的引用。这意味着数组不会被垃圾回收,即使我们已经显式地清除了String s!

You'll now have a String s2 which, although it seems to be a one-character string, holds a reference to the gigantic char array created in the String s. This means the array won't be garbage collected, even though we've explicitly nulled out the String s!

这样做的修复是使用我们前面提到的无用String构造函数,如下所示:

The fix for this is to use our previously mentioned "useless" String constructor like this:

String s2 = new String(s.substring(0, 1));  

这个构造函数实际上是将旧内容复制到新数组大于字符串中的字符数。这意味着旧的String内容将按预期进行垃圾回收。愉快的愉快的喜悦喜悦。

It's not well-known that this constructor actually copies that old contents to a new array if the old array is larger than the count of characters in the string. This means the old String contents will be garbage collected as intended. Happy happy joy joy.

最后, Kat Marsen 提出这些要点


首先,字符串常量从不会被垃圾回收。其次,字符串常量是intern'd,这意味着它们在整个VM共享。这会节省内存。但它并不总是你想要的。

First of all, string constants are never garbage collected. Second of all, string constants are intern'd, which means they are shared across the entire VM. This saves memory. But it is not always what you desire.

String上的复制构造函数允许你从String文字创建一个私有的String实例。这对于构造有意义的互斥对象(为了同步而言)是非常有价值的。

The copy constructor on String allows you to create a private String instance from a String literal. This can be very valuable for constructing meaningful mutex objects (for the purposes of synchronization).

这篇关于在Java中使用String(String)构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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