SCWCD 1.4, SCJP 1.4
public static String valueOf(char[] data)
Returns the string representation of the char array argument. The contents of the character array are copied; subsequent modification of the character array does not affect the newly created string.
Parameters:
data - a char array.
Returns:
a newly allocated string representing the same sequence of characters contained in the character array argument
public static String copyValueOf(char[] data)
Returns a String that is equivalent to the specified character array. It creates a new array and copies the characters into it.
Parameters:
data - the character array.
Returns:
a String that contains the characters of the character array.
My blood is tested +ve for Java.
Originally posted by Chetan Parekh:
Just see what's there in API....
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
SCWCD 1.4, SCJP 1.4
Ernest Friedman-Hill wrote:Marilyn got me thinking. I rooted around for the oldest JDK source code I have -- JDK 1.0 beta for sparc -- and in String.java, you find this (emphasis mine
But the comments lie, actually. The array ends up being copied in both cases, although copyValueOf() does it explicitly and then calls "new String()", and valueOf calls "new String()" directly. The String constructor also copies the array, so this old version of copyValueOf results in the char[] being copied twice, for no reason.
The very strong implication is that, sometime in the history of Java Strings, before the 1.0 release, Strings weren't immutable, and they had constructors and a factory method which let you create instances that used a specific char[] to hold their contents! By the 1.0 release, that capability had been removed, but these two methods both remained -- even though copyValueOf() was redundant and could easily have been removed.
Chalk it up to... a mistake.
[ June 07, 2005: Message edited by: Ernest Friedman-Hill ]
String java.lang.String.valueOf(char[] data, int offset, int count)
Returns the string representation of a specific subarray of the char array argument.
The offset argument is the index of the first character of the subarray. The count argument specifies the length of the subarray. The contents of the subarray are copied; subsequent modification of the character array does not affect the newly created string.
Parameters:
data the character array.
offset the initial offset into the value of the String.
count the length of the value of the String.
Returns:
a string representing the sequence of characters contained in the subarray of the character array argument.
Throws:
IndexOutOfBoundsException - if offset is negative, or count is negative, or offset+count is larger than data.length.
Consider Paul's rocket mass heater. |