JDK-6480728 : Byte.valueOf(byte) returns a cached value but Byte.valueOf(String)
  • Type: Enhancement
  • Component: core-libs
  • Sub-Component: java.lang
  • Affected Version: 5.0,6
  • Priority: P5
  • Status: Resolved
  • Resolution: Fixed
  • OS:
    generic,solaris_8,solaris_10,windows_xp generic,solaris_8,solaris_10,windows_xp
  • CPU: generic,x86,sparc
  • Submitted: 2006-10-11
  • Updated: 2010-04-26
  • Resolved: 2009-10-09
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7 b74Fixed
Related Reports
Duplicate :  
Duplicate :  
Relates :  
Description
A DESCRIPTION OF THE REQUEST :
Byte.valueOf(byte) notes
     * If a new <tt>Byte</tt> instance is not required, this method
     * should generally be used in preference to the constructor
     * {@link #Byte(byte)}, as this method is likely to yield
     * significantly better space and time performance by caching
     * frequently requested values.

Please provide a version of valueOf which takes a String and does the same thing. This also holds for Short, Integer and Long.

JUSTIFICATION :
If it is a good idea for converting byte to Byte it should also be a good idea from converting String to Byte.
I understand there may be backward compatibility issues but there should be an elegant way to resolve this.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
valueOf(String) behaves in the same way as valueOf(byte)
ACTUAL -
valueOf(String) does the same as new Byte(String)
valueOf(byte) notes that using the cache has many benifits.

---------- BEGIN SOURCE ----------
public static Byte valueOf(String s, int radix)	throws NumberFormatException {
	return valueOf(parseByte(s, radix));
    }
---------- END SOURCE ----------

CUSTOMER SUBMITTED WORKAROUND :
Call Byte.valueOf(Byte.parseByte(String))
This is tricky to do with reflections in an elegant way!

Comments
SUGGESTED FIX # HG changeset patch # User darcy # Date 1254949460 25200 # Node ID e7ad502130baae09e4edc34dbf4f129fc5f3946d # Parent 777714bd992a2c54d87e49fce451bdfae6eb5faa 6480728: Byte.valueOf(byte) returns a cached value but Byte.valueOf(String) 6655735: Integer.toString() and String.valueOf(int) contain slow delegations Reviewed-by: lancea --- a/src/share/classes/java/lang/Byte.java Wed Oct 07 13:53:11 2009 -0700 +++ b/src/share/classes/java/lang/Byte.java Wed Oct 07 14:04:20 2009 -0700 @@ -201,7 +201,7 @@ public final class Byte extends Number i */ public static Byte valueOf(String s, int radix) throws NumberFormatException { - return new Byte(parseByte(s, radix)); + return valueOf(parseByte(s, radix)); } /** @@ -277,7 +277,7 @@ public final class Byte extends Number i if (i < MIN_VALUE || i > MAX_VALUE) throw new NumberFormatException( "Value " + i + " out of range from input " + nm); - return (byte)i; + return valueOf((byte)i); } /** @@ -374,7 +374,7 @@ public final class Byte extends Number i * base&nbsp;10. */ public String toString() { - return String.valueOf((int)value); + return Integer.toString((int)value); } /** --- a/src/share/classes/java/lang/Double.java Wed Oct 07 13:53:11 2009 -0700 +++ b/src/share/classes/java/lang/Double.java Wed Oct 07 14:04:20 2009 -0700 @@ -629,7 +629,7 @@ public final class Double extends Number * @see java.lang.Double#toString(double) */ public String toString() { - return String.valueOf(value); + return toString(value); } /** --- a/src/share/classes/java/lang/Float.java Wed Oct 07 13:53:11 2009 -0700 +++ b/src/share/classes/java/lang/Float.java Wed Oct 07 14:04:20 2009 -0700 @@ -551,7 +551,7 @@ public final class Float extends Number * @see java.lang.Float#toString(float) */ public String toString() { - return String.valueOf(value); + return Float.toString(value); } /** --- a/src/share/classes/java/lang/Integer.java Wed Oct 07 13:53:11 2009 -0700 +++ b/src/share/classes/java/lang/Integer.java Wed Oct 07 14:04:20 2009 -0700 @@ -746,7 +746,7 @@ public final class Integer extends Numbe * base&nbsp;10. */ public String toString() { - return String.valueOf(value); + return toString(value); } /** --- a/src/share/classes/java/lang/Long.java Wed Oct 07 13:53:11 2009 -0700 +++ b/src/share/classes/java/lang/Long.java Wed Oct 07 14:04:20 2009 -0700 @@ -761,7 +761,7 @@ public final class Long extends Number i * base&nbsp;10. */ public String toString() { - return String.valueOf(value); + return toString(value); } /** --- a/src/share/classes/java/lang/Short.java Wed Oct 07 13:53:11 2009 -0700 +++ b/src/share/classes/java/lang/Short.java Wed Oct 07 14:04:20 2009 -0700 @@ -170,7 +170,7 @@ public final class Short extends Number */ public static Short valueOf(String s, int radix) throws NumberFormatException { - return new Short(parseShort(s, radix)); + return valueOf(parseShort(s, radix)); } /** @@ -282,7 +282,7 @@ public final class Short extends Number if (i < MIN_VALUE || i > MAX_VALUE) throw new NumberFormatException( "Value " + i + " out of range from input " + nm); - return (short)i; + return valueOf((short)i); } /** @@ -379,7 +379,7 @@ public final class Short extends Number * base&nbsp;10. */ public String toString() { - return String.valueOf((int)value); + return Integer.toString((int)value); } /** --- a/src/share/classes/java/lang/String.java Wed Oct 07 13:53:11 2009 -0700 +++ b/src/share/classes/java/lang/String.java Wed Oct 07 14:04:20 2009 -0700 @@ -2995,7 +2995,7 @@ public final class String * @see java.lang.Integer#toString(int, int) */ public static String valueOf(int i) { - return Integer.toString(i, 10); + return Integer.toString(i); } /** @@ -3009,7 +3009,7 @@ public final class String * @see java.lang.Long#toString(long) */ public static String valueOf(long l) { - return Long.toString(l, 10); + return Long.toString(l); } /**
07-10-2009

PUBLIC COMMENTS See http://hg.openjdk.java.net/jdk7/tl/jdk/rev/e7ad502130ba
07-10-2009

EVALUATION Caching issues in the wrapper classes should be examined.
11-10-2006