aspose file tools*
The moose likes Beginning Java and the fly likes Difference between String.copyValueOf(char[]) and String.valueOf(char[]) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Difference between String.copyValueOf(char[]) and String.valueOf(char[])" Watch "Difference between String.copyValueOf(char[]) and String.valueOf(char[])" New topic
Author

Difference between String.copyValueOf(char[]) and String.valueOf(char[])

Rohan Kayan
Ranch Hand

Joined: Sep 17, 2004
Posts: 123
What is the difference between String.copyValueOf(char[]) and String.valueOf(char[])??


SCWCD 1.4, SCJP 1.4
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Just see what's there in API....

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.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by Chetan Parekh:
Just see what's there in API....


Uhh, from the quoted JavaDoc, I actually don't see any implied difference in behaviour. Am I missing something?


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
Marilyn de Queiroz
Sheriff

Joined: Jul 22, 2000
Posts: 9053
    
  12
Looking at the source code I see:


JavaBeginnersFaq
"Yesterday is history, tomorrow is a mystery, and today is a gift; that's why they call it the present." Eleanor Roosevelt
Hentay Duke
Ranch Hand

Joined: Oct 27, 2004
Posts: 198
Looking at the API and source code there doesn't seem to be any difference in functionality. Is someone saying there is a noticable difference? How about adding a comment to the copied API or source code you post here, it's hard to tell what your trying to say or point out.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

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 ]

[Jess in Action][AskingGoodQuestions]
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Interesting! So one of those methods probably should be deprecated...
Rohan Kayan
Ranch Hand

Joined: Sep 17, 2004
Posts: 123
But none of the methods is deprecated from java 1.3 . I tried with on example to check the functionality


public class StringCopyValueOf
{
public static void main(String[] args)
{
char name[]= {'r','o','h','a','n'};
String s1 = String.valueOf(name);
String s2 =String.copyValueOf(name);
System.out.println(s1);
System.out.println(s2);
name[3]='k';
System.out.println(s1);
System.out.println(s2);
System.out.println(name);

}
}

Output is
rohan
rohan
rohan
rohan
rohkn

so it seems that there is not difference in the funcationality.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Difference between String.copyValueOf(char[]) and String.valueOf(char[])