9

What is the difference between valueOf and copyValueOf. I looked on GrepCode, only to find that both return the exact same thing.

copyValueOf:

Parameters: data the character array.

Returns: a String that contains the characters of the character array.

public static String copyValueOf(char data[]) { return new String(data); }

valueOf:

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 returned string.

Parameters: data the character array.

Returns: a String that contains the characters of the character array.

public static String valueOf(char data[]) { return new String(data); }

So if both do the same thing, then how come one isn't deprecated?

StarCoder
  • 251
  • 1
  • 3
  • 16
  • 1
    Read this - https://coderanch.com/t/399871/java/Difference-String-copyValueOf-char-String – vinS Jan 07 '18 at 05:11
  • @vinS That's from 12 years ago. So I would think there would be an update on it. – StarCoder Jan 07 '18 at 05:15
  • 1
    Why would there be an update on it? Standards are standards for a reason and they're very wary about breaking legacy code. – George Jan 07 '18 at 05:18
  • @George More than likely nobody really uses `copyValueOf`. And if they do it probably would be an old program, they won't use Java 8+. – StarCoder Jan 07 '18 at 05:38
  • 3
    @StarCoder old programs aren't relevant, but old code is either re-compiled or compiled as program abstraction all the time with the latest versions of java. Changing the purpose of or removing a method would be a pain for anyone using a legacy library, and a huge pain for anyone just trying to recompile for performance (without wanting/expecting to make any code changes). – George Jan 07 '18 at 05:44
  • @George I see your point. – StarCoder Jan 07 '18 at 05:46
  • @George Then why don't they make a *superseded* annotation, to let users know that there is a replacement? – StarCoder Jan 07 '18 at 05:50
  • @StarCoder Assuming that post @vinS linked to is to be believed, `valueOf` is the function that lost its original intent, and was never a replacement for `copyValueOf`. I suppose neither will ever be removed and there's no strong reason to use one over the other. – George Jan 07 '18 at 06:02

3 Answers3

4

As others have pointed out:

  • The two methods are equivalent.
  • The javadocs clearly state that the two methods are equivalent. And copyValueOf clearly points the reader to the (mildly) preferred valueOf method.
  • There is NO performance difference between the two versions. The implementations are identical.
  • Deprecating one or other method would be counter-productive because it would prompt people to "fix" code that isn't broken. That is (arguably) a waste of time, and it would annoy a lot of people.
  • Removing one or other method would break backwards compatibility ... for no good reason. That would really annoy a lot of people.

The only other issue is why there isn't an annotation to flag a method as "out of date". I think that the answer to that is that it doesn't matter if you use an API method that is out of date. Certainly, it doesn't matter enough for the Java team to implement such a mechanism ... and then spend a lot of time deciding whether such-and-such an API is "out of date enough" to warrant flagging, etc.

(Most folks would not want the Java team to waste their time on such things. We would prefer them to spend their time on delivering improvements to Java that will make a real difference to program performance and programmer productivity.)

A more appropriate way to deal with this issue would for someone to write or enhance 3rd-party a style checker or bug checker tool to flag use of (so-called) out of date methods. This is clearly not Oracle's problem, but if you (Dear Reader) are really concerned about this, you could make it your problem.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
2

A program element annotated @Deprecated is one that programmers are discouraged from using, typically because it is dangerous, or because a better alternative exists. Compilers warn when a deprecated program element is used or overridden in non-deprecated code (Annotation Type Deprecated).

The two methods literally do the same thing; meaning that neither method is dangerous or better. This is perhaps the reason they haven't bothered to deprecate either method.

Zachary
  • 1,693
  • 1
  • 9
  • 13
  • 2
    Perhaps, but still not really sure why we need both. Sure for legacy code, but being that old. I doubt those programs use Java 8+. So it shouldn't effect the legacy programs. – StarCoder Jan 07 '18 at 05:36
  • 1
    @StarCoder It doesn't make much sense to me, it seems more like a "if it's not broke - don't fix" approach. Besides, I'm sure it would spark a debate about which method should be kept – Zachary Jan 07 '18 at 05:37
  • 1
    It may spark debate, but why would you want keep the longer one? Instead of a quicker one to type, is readable, and does the exact same thing? – StarCoder Jan 07 '18 at 05:41
  • 1
    Because Java coders are the people who brought us such short and concise names as `UnsupportedLookAndFeelException`, `CompositeDataInvocationHandler`, and `ObjectReferenceTemplateHolder`. "quicker to type" isn't exactly a priority for the language designers. – Silvio Mayolo Jan 07 '18 at 05:56
2

Both of the methods serves same purpose, but they differ a little in their inner implementation (according comments in String.java - older implementations):

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.

valueOf(char data[])

Returns a string that is equivalent to the specified character array. Uses the original array as the body of the string (ie. it does not copy it to a new array).

The older version has redudndency in its code. But in newer versions of Java, implementations as same. Older method is made available so that old programmers who are unaware of newer version can use it.

Lii
  • 11,553
  • 8
  • 64
  • 88
Nithin
  • 748
  • 1
  • 10
  • 27
  • `public static String valueOf(char data[]) { return new String(data); }` and `public static String copyValueOf(char data[]) { return new String(data); }`. – Zachary Jan 07 '18 at 05:49
  • Yeah forgot the fact that newer implementations are same. older JDK source code have mentioned that their implementations differ. – Nithin Jan 07 '18 at 06:07