字符串类的subString()函数如何工作 [英] how the subString() function of string class works

查看:103
本文介绍了字符串类的subString()函数如何工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请参阅以下代码。

String s = "Monday";
if(s.subString(0,3).equals("Mon"){}

String s2 = new String(s.subString(0,3));
String s3 = s.subString(0,3);  

我知道第2行仍将指向到星期一并有一个新的String对象,其偏移量和计数设置为0,3。

I know that line 2 will still point to "Monday" and have a new String object with the offset and count set to 0,3.

第4行将在字符串池中创建一个新的字符串Mon并且指向它。

The line 4 will create a new String "Mon" in string pool and point to it.

但不确定第5行是否会像第2行或第4行那样。

But not sure what about line 5 whether it will behave like line 2 or line 4.

如果第2行或第4行我错了,请更正..
谢谢

If i am wrong for line 2 or 4 also please correct.. Thanks

推荐答案


我知道第2行仍将指向星期一,并且有一个新的String对象,其偏移量和计数设置为0,3。

I know that line 2 will still point to "Monday" and have a new String object with the offset and count set to 0,3.

目前Sun JRE的实现是正确的。我似乎记得过去的Sun实现并不是这样,而且JVM的其他实现也不是这样。不要依赖于不具体编辑GNU类路径可能会复制数组(我不记得手头用什么比率决定何时复制,但是如果副本是原始的一小部分就会复制,这会使一个很好的O(N)算法到O(N ^ 2))。

That is currently true of the Sun JRE implementation. I seem to recall that was not true of the Sun implementation in the past, and is not true of other implementations of the JVM. Do not rely on behaviour which is not specified. GNU classpath might copy the array (I can't remember off hand what ratio is uses to decide when to copy, but it does copy if the copy is a small enough fraction of the original, which turned one nice O(N) algorithm to O(N^2)).


第4行将在字符串池中创建一个新的字符串Mon并指向它。

The line 4 will create a new String "Mon" in string pool and point to it.

不,它在堆中创建一个新的字符串对象,受到与任何其他对象相同的垃圾收集规则的约束。它是否共享相同的底层字符数组是依赖于实现的。不要依赖未指定的行为。

No, it creates a new string object in the heap, subject to the same garbage collection rules as any other object. Whether or not it shares the same underlying character array is implementation dependant. Do not rely on behaviour which is not specified.

String(String) 构造函数说:

The String(String) constructor says:


初始化一个新创建的String对象,使其表示与参数相同的字符序列;换句话说,新创建的字符串是参数字符串的副本。

Initializes a newly created String object so that it represents the same sequence of characters as the argument; in other words, the newly created string is a copy of the argument string.

String(char []) 构造函数说:

The String(char[]) constructor says:


分配一个新的String,使其表示当前包含在字符数组参数中的字符序列。复制字符数组的内容;后续修改字符数组不会影响新创建的字符串。

Allocates a new String so that it represents the sequence of characters currently contained in the character array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.

遵循良好的OO原则, String 的方法实际上并不要求它是使用字符数组实现,因此 String 规范的任何部分都不需要对字符数组进行操作。将数组作为输入的那些操作指定将数组的内容复制到String中使用的任何内部存储。字符串可以在内部使用UTF-8或LZ压缩并符合API。

Following good OO principles, no method of String actually requires that it is implemented using a character array, so no part of the specification of String requires operations on an character array. Those operations which take an array as input specify that the contents of the array are copied to whatever internal storage is used in the String. A string could use UTF-8 or LZ compression internally and conform to the API.

但是,如果您的JVM没有进行小比例子字符串优化,那么当你使用 new String(String)时,它有可能只复制相关部分,所以如果它改善了内存使用,就会尝试查看它。并非影响Java运行时的所有内容都是由Java定义的。

However, if your JVM doesn't make the small-ratio sub-string optimisation, then there's a chance that it does copy only the relevant portion when you use new String(String), so it's a case of trying it a seeing if it improves the memory use. Not everything which effects Java runtimes is defined by Java.

获取字符串池中的字符串等于到字符串,使用 intern() 方法。这将从池中检索一个字符串(如果已经实例化了一个字符串),或者创建一个新字符串并将其放入池中。请注意,池化字符串具有不同的(再次依赖于实现)垃圾收集行为。

To obtain a string in the string pool which is equal to a string, use the intern() method. This will either retrieve a string from the pool if one with the value already has been interned, or create a new string and put it in the pool. Note that pooled strings have different (again implementation dependent) garbage collection behaviour.

这篇关于字符串类的subString()函数如何工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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