Java: valueOf vs copyValueOf

StarCoder :

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?

Stephen C :

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 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 actually make a real difference.)

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=463144&siteId=1