• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What is the difference between String.copyValueOf(char[]) and String.valueOf(char[])??
 
Ranch Hand
Posts: 3640
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
Sheriff
Posts: 9109
12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looking at the source code I see:
 
Ranch Hand
Posts: 198
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ilja Preuss
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Interesting! So one of those methods probably should be deprecated...
 
Rohan Kayan
Ranch Hand
Posts: 123
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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 ]



I think the coffee guys might want to differenciate them and wanted to implement these two methods in different ways, but then they forget about it and instead, they invented another methods named



From its Javadoc we read:


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.




So that's the method you guessed. Now one of these two methods is obsolete. It's not for the backward compability, as I had guessed.

 
Marshal
Posts: 79153
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Let's look and see what the latest version of Java® uses, remembering that the previous posts are eleven years old. Because of backwards compatibility, the two methods are still in the String class: valueOf() and copyValueOf(). If you look through those two links, you will find that the distinction has been abolished. Both do the same thing. Obviously Ernest's β link shows code which created a potentially mutable form of a String instance with valueOf(), and that has since been changed. I don't know when it was changed.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic