一个String的大小大于10M,会否出现问题?

解决方案 »

  1.   

    会否出问题取决于你当前JVM可用内存大小。
    如果可用内存不足,自然会出现OutOfMemoryError可以通过java命令的 -Xmx参数来制定允许JVM使用的最大Heap大小(记得默认是64M来着)。例如,允许JVM使用的最大heap改为512MB,命令如下
    java -Xmx512m className
      

  2.   

    不会,只不过,你需要一些耐心,且如king_play所说的,将jvm设大一些
      

  3.   

    应该没有问题,关键是虚拟机能分出多少HEAP给你
      

  4.   

    字符串长度是一个int型的变量,你的长度能超过他就算你赢
      

  5.   

    字符串长度是一个int型的变量,你的长度能超过他就算你赢--------------------------这个搞笑。娃哈哈哈哈哈。
      

  6.   

    //只要你的内存够大,几百G都不是问题!
    //没那么大。
    我只想说明String的大小不是受程序限制,而是其它外界原因的限制(内存,时间等)。
      

  7.   

    String 只支持65535个字符, 楼上的兄弟们,你们有没有真的试过给String赋值??????
    String 源代码里有个长度的变量65535个字符最多.
    再多就用流吧
      

  8.   

    我重新作了测试
    如果
    String s = "xxxx..."; // 1 数据长度大于65535就会出错char[] c = new char[123456];
    ...
    String s = new Stirng(c); // 2 数值再大也不出错。我查了JDK的String类的sourceCode如下:
    1使用的构造方法如下:
        public String(String original) {
    int size = original.count;
    char[] originalValue = original.value;
    char[] v;
       if (originalValue.length > size) {
          // The array representing the String is bigger than the new
          // String itself.  Perhaps this constructor is being called
          // in order to trim the baggage, so make a copy of the array.
        v = new char[size];
          System.arraycopy(originalValue, original.offset, v, 0, size);
      } else {
          // The array representing the String is the same
          // size as the String, so no point in making a copy.
        v = originalValue;
      }
    this.offset = 0;
    this.count = size;
    this.value = v;
        }2. 构造方法如下:    public String(char value[]) {
    int size = value.length;
    char[] v = new char[size];
    System.arraycopy(value, 0, v, 0, size);
    this.offset = 0;
    this.count = size;
    this.value = v;
        }
    这两个构造方法都是把数据String or char[] 转化赋值给自己的char[] value
    为什么出问题,我也解释不清楚,等待高手答复。
      

  9.   

    String类应该没有长度限制才对
      

  10.   

    String 内部用char数组存储,而数组大小用int存储。所以有限制
    当然如果你配给jvm的heapsize小,还没到int值,就已经outofmemoryto blackpark:
       jvm缺省heapsize在64m左右。 需要调整heapsize大小