• Post Reply Bookmark Topic Watch Topic
  • New Topic

Two String Methods that Appear to do the same Thing  RSS feed

 
Kevin Simonson
Ranch Hand
Posts: 223
2
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm taking a look at "http://docs.oracle.com/javase/7/docs/api/" for class {String}; I've scrolled down a ways; I see under label "valueOf":

| public static 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.

Then immediately below that under label "copyValueOf" I see:

| public static String copyValueOf(char[] data,
| int offset,
| int count)
|
| Returns a String that represents the character sequence in the array specified.
|
| Parameters:
|
| data - the character array.
|
| offset - initial offset of the subarray.
|
| count - length of the subarray.
|
| Returns:
|
| a String that contains the characters of the specified subarray of the character array.

What exactly is the difference between these two methods, {valueOf()} and {copyValueOf()}? It looks to me like they return precisely the same thing given the same arguments. Am I missing something?

Kevin S
 
Kevin Simonson
Ranch Hand
Posts: 223
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Furthermore, if I scroll up to the section on constructors I see:

| public String(char[] value,
| int offset,
| int count)
|
| Allocates a new String that contains characters from a subarray of the character array argument.
| The offset argument is the index of the first character of the subarray and 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:
|
| value - Array that is the source of characters
|
| offset - The initial offset
|
| count - The length
|
| Throws:
|
| IndexOutOfBoundsException - If the offset and count arguments index characters outside the
| bounds of the value array

Once again, what is the essential difference between this constructor and either of the other two methods, {valueOf()} and {copyValueOf()}? Don't all three do precisely the same thing?

Kevin S
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The answer would appear to be: "all 3 are identical", as this is the source code of the java.lang.String class in JDK 6:

I'd never heard of the copyValueOf method before now, and am at a loss what its purpose is given the valueOf method.

Interestingly, the javadocs of copyValueOf do not mention the exception that valueOf mentions - although seeing this code, clearly it would do the same thing.

Another point to note is that the javadocs of the two methods do not mention that a new object is created. Clearly, looking at the source, the object will be new, but the javadocs don't guarantee it. That may sound esoteric, but in some cases it might be important. The constructor, on the other hand, will by its very nature always create a new object.
 
Martin Vajsar
Sheriff
Posts: 3752
62
Chrome Netbeans IDE Oracle
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
These methods are identical even in JDK 5.

I *guess* that these methods might have actually differed in some ancient version of Java, in the sense that valueOf() used the passed array as the backing array of the new String instance, while copyValueOf() made a copy of the array. The first approach would have an advantage of not copying the backing array, which saves time (and perhaps memory depending on the circumstances), but would have a terrible drawback that the value of the formally immutable String might be changed by modifying the contents of the original array.

It's just a wild speculation inspired by the names of these methods and also by the way in which the substring method implementation changed in Java 7. I didn't succeed to get my hands on some really ancient version of Java source codes to verify this speculation.
 
Kevin Simonson
Ranch Hand
Posts: 223
2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the input! I guess I'll just use the constructor and cut out either middleman.

Kevin S
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!