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.