3

My question is about: Public String(char[] value). Can anybody help me: does it internally loops for every value[i] or not. Specifically,

does Public String(char[] value) means:

for each char[i]
returnedSTRING = returnedSTRING + char[i] 

or not ?

Naman
  • 27,789
  • 26
  • 218
  • 353
Arpssss
  • 3,850
  • 6
  • 36
  • 80
  • 1
    It copies the contents of the array into a newly-constructed String's internal buffer; that will necessarily involve a loop somewhere... – Oliver Charlesworth Jun 26 '12 at 13:34
  • @OliCharlesworth, That should be. However, actually I try to get which one is faster and why ? – Arpssss Jun 26 '12 at 13:36
  • Which one between what and what? – JB Nizet Jun 26 '12 at 13:40
  • @OliCharlesworth it doesn't need a loop, since it calls System.arraycopy to do the heavy lifting, which just copies whole blocks of memory at a time; it doesn't have to iterate through each tiny element – Andrei Bârsan Jun 26 '12 at 13:49
  • @AndreiBârsan: Indeed, but there will be a loop inside `arrayCopy`. – Oliver Charlesworth Jun 26 '12 at 13:55
  • @OliCharlesworth not necessarily; for chars and bytes definitely not! see http://stackoverflow.com/questions/2772152/why-is-system-arraycopy-native-in-java – Andrei Bârsan Jun 26 '12 at 14:04
  • @AndreiBârsan: My point was that *somewhere* there must necessarily be a loop. – Oliver Charlesworth Jun 26 '12 at 14:45
  • @OliCharlesworth I've been diggin through the source of the JVM for about an hour now, and it seems that, at least in the Winx86 implementation, it *does* iterate through the data; strange - this might constitute a whole separate question. The method is pd_conjoint_oops_atomic, and it's located in `openjdk\hotspot\src\os_cpu\windows_x86\vm\copy_windows_x86.inline.hpp` – Andrei Bârsan Jun 26 '12 at 14:58

4 Answers4

6

Java is open source and if you attach the source to Eclipse you can always use F3 to go check the functions. In this case the String class has the following constructor which is what you are looking for:

/**
 * Allocates a new {@code 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.
 *
 * @param  value
 *         The initial value of the string
 */
public String(char value[]) {
    int size = value.length;
    this.offset = 0;
    this.count = size;
    this.value = Arrays.copyOf(value, size);
}

Edit: If you are wondering, Arrays.copyOf calls System.arraycopy.

Chris911
  • 4,131
  • 1
  • 23
  • 33
2

String object save all string character in char[] array internally. This constructor just copy the whole array to internal representation. See the sources:

public String(char value[]) {
        int size = value.length;
        this.offset = 0;
        this.count = size;
        this.value = Arrays.copyOf(value, size);
}
mishadoff
  • 10,719
  • 2
  • 33
  • 55
2

From the docs:

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.

The contents of the character array are copied. According to the source code, like mishadoff pointed out, Arrays.copyOf(value, size) is used. However, Arrays.copyOf(value, size) calls System.arraycopy, in turn, which doesn't actually iterate & allocate but actually copies the memory, similarly to what a memcpy() call would do in C/C++. This is done internally by Java since it's much faster than a normal loop. System.arraycopy is a native method that takes advantage of the host OS's memory management features.

So to answer your question, the chars are not iterated over in a for, rather the whole memory block on which they are situated gets copied "in bulk" by Java.

Andrei Bârsan
  • 3,473
  • 2
  • 22
  • 46
1

See String class source code

However, it can be different depending on version of Java.

Andy
  • 1,618
  • 11
  • 13