java 如何为字符串分配内存?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11416542/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 05:01:45  来源:igfitidea点击:

How is memory allocated for a string?

javac++stringmemory-management

提问by user1210233

How is memory allocated for a string, say in Java or C++? This might be silly, so please excuse me. I'm wondering because a string is of unknown size.

在 Java 或 C++ 中,如何为字符串分配内存?这可能是愚蠢的,所以请原谅。我想知道因为字符串的大小未知。

回答by Romain

In Java, Stringis an immutable Object, so the "size" of the Stringhasto be known at time of allocation. It'll end up allocated in a shared object pool if it's "static" (e.g. a Stringlitteral, like "Hey, I'm a String litteral!"), or on the heap if it's constructed using new String(...).

在Java中,String是一个不可变对象,因此“大小”的String具有在分配的时间是已知的。如果它是“静态的”(例如一个Stringlitteral,像"Hey, I'm a String litteral!"),则它最终会分配在共享对象池中,或者如果它是使用new String(...).

回答by kosa

Java Strings are immutable objects. In a way each time you create a String, there will be char[] memory allocated with number of chars in String. If you do any manipulations on that String it will be brand new object and with the length of chars there will be memory allocation done.

Java 字符串是不可变对象。在某种程度上,每次创建 String 时,都会为 String 中的字符数分配 char[] 内存。如果您对该字符串进行任何操作,它将是全新的对象,并且随着字符的长度,将完成内存分配。

回答by Tudor

Internally, a java Stringis nothing more than a char array with a known length. Here are the class members of String:

在内部,javaString只不过是一个已知长度的字符数组。以下是String的类成员:

  110   public final class String
  111       implements java.io.Serializable, Comparable<String>, CharSequence
  112   {
  113       /** The value is used for character storage. */
  114       private final char value[];
  115   
  116       /** The offset is the first index of the storage that is used. */
  117       private final int offset;
  118   
  119       /** The count is the number of characters in the String. */
  120       private final int count;
  121   
  122       /** Cache the hash code for the string */
  123       private int hash; // Default to 0
  124   
  125       /** use serialVersionUID from JDK 1.0.2 for interoperability */
  126       private static final long serialVersionUID = -6849794470754667710L;
            ...

回答by Durandal

You confuse variable sizewith unknown size. A concrete string in any language has always a known size, its just that each instance of a string may have a different size. How languages deal with the variable length can be very different and is implementation specific.

您将可变大小未知大小混淆。任何语言中的具体字符串始终具有已知大小,只是字符串的每个实例可能具有不同的大小。语言处理可变长度的方式可能非常不同,并且是特定于实现的。

回答by Darko Rodic

Just to add to previous answers.
In Java strings can be allocated in two ways depending on how string is created. For example if string is created with String s = "some string";JVM will put this string in so called literal pool (something left behind from time when memory was problem) and if you create string with String s = new String("some string");JVM will put this on heap ...
only significant difference is that you may use operator == to compare strings if they are both in literal pool, but this is never recomended.

只是为了添加到以前的答案。
在 Java 中,根据字符串的创建方式,可以通过两种方式分配字符串。例如,如果使用String s = "some string";JVM创建字符串会将这个字符串放入所谓的文字池(内存出现问题时留下的东西),如果您使用String s = new String("some string");JVM创建字符串会将它放在堆上......
唯一的显着区别是您可能使用运算符 == 来比较字符串,如果它们都在文字池中,但从不推荐这样做。

regards

问候

回答by Jakob Weisblat

It is dynamically allocated like a vector. When it becomes too big, it is resized automatically by an internal method (C++). In Java, as thinksteep already mentioned, Strings are immutable.

它像向量一样动态分配。当它变得太大时,它会通过内部方法 (C++) 自动调整大小。在 Java 中,正如 thinksteep 已经提到的,字符串是不可变的。