0

these's something strangly i found that is when String length in java exceeding a value, then it shown odd.

there is my test code

public static void main(String[] args) {
    int length = 4096;
    char[] chars = new char[length];
    for (char c : chars) {
        c = 'c';
    }
    String str = new String(chars);
    System.out.println(str.length());
    System.out.println(str);
}

when i run above code on my computer, i get 4096 space character output from console.

then i change the length variable to 4095, this time output is correct, 4095 c character.

but on another computer, the output does not correct unless the length variable less than 2900.

i just can't figure out why?

EDIT

i think i figured out what's going on, i try to run it again in command window, even though the length value is big enough , it's print correct.

so it seemed like some limit about eclipse's console.

but i checked my eclipse console buffer size, its 800000

Joe
  • 3,581
  • 4
  • 23
  • 28
  • 4
    http://stackoverflow.com/questions/816142/strings-maximum-length-in-java-calling-length-method. There they are talking about max len, which is much more than few thousand. – Gjordis Nov 01 '13 at 07:35
  • this will not printing 4095 nor 4096 'c' character – Baby Nov 01 '13 at 08:03
  • the length variable value is imprecise. its 4095 just on my computer, i suggest you try another value – Joe Nov 01 '13 at 08:06
  • i mean, do you just want to print the length? because the value 'c' never inserted into chars – Baby Nov 01 '13 at 08:11
  • @Refa oh yes.. i just found it too, i was stupid.i run it again with amit's method to assignment. and the result is same – Joe Nov 01 '13 at 08:17
  • @Refa you can change length to a smaller value, like 10. that will print correct result – Joe Nov 01 '13 at 08:19
  • @Joe It will never print c...c - regardless of the length, unless you are using a flawed compiler/jre. – amit Nov 01 '13 at 08:49
  • @amit im using `jdk1.7.0_25` and `Eclipse EE Indigo Service Release 2`,does this matter? – Joe Nov 01 '13 at 09:07
  • @Joe “i run it again with amit's method to assignment. and the result is same” I don’t believe you. – Holger Nov 01 '13 at 09:27
  • @Holger my meaning of 'result is same' is when the length greater than 4095, it also just print some space character – Joe Nov 01 '13 at 13:14

1 Answers1

6

You are only changing the value of the local variable c and not the values of the entries in the char[].

To change the actual values, use a "regular" for loop, and not a for-reach loop:

for (int i = 0; i < chars.length; i++) {
    chars[i] = 'c';
}

As a side note, a String in java is NOT a char[] - it's a String object.

amit
  • 175,853
  • 27
  • 231
  • 333
  • i can make sure `String` could structured by `char[]`, and i also make sure that way of my code to assignment value to `char[]` is effective – Joe Nov 01 '13 at 07:42
  • Amit, it still does not answer the question that when the length of the array is 4096, the assignment has no effect. All it stores is the space character. On the other hand if the length is changed to 4095, everything works as expected. – Santosh Nov 01 '13 at 07:47
  • @Santosh My best guess is the code was different, there is no way it would work with any length of an array, since by modifying the local variable `c` - it does not apply to the actual `chars[]`. – amit Nov 01 '13 at 08:47
  • @Santosh: since the code as posted by the questioner cannot work on any computer his question is obsolete. He can’t have observed the described behavior with this code so we can’t explain that behavior. – Holger Nov 01 '13 at 09:14
  • @amit, I get your point.Even if I replace the `for` loop of OP by your suggestion, I can still reproduce the problem on my machine. Even if the problem is not uniform across computers, its a problem nonetheless. – Santosh Nov 01 '13 at 10:14