This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes toString() behaviour? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "toString() behaviour?" Watch "toString() behaviour?" New topic

toString() behaviour?

ashok devaram

Joined: Oct 25, 2005
Posts: 6
class Test
public static void main(String args[])
Byte b1=new Byte("127");
when attempt to run this code ,the output shown is "false"
can u clarify me why it is printing "false".
patrick J. d'cousta

Joined: Oct 25, 2005
Posts: 24
Ashok for such kind of question we have to actually look at the inplementation.The Byte toString method in turn calls Integer toString method the same is given below.As u can see a new String object is created everytime toString is called.

public static String toString(int i) {
switch(i) {
case Integer.MIN_VALUE: return "-2147483648";
case -3: return "-3";
case -2: return "-2";
case -1: return "-1";
case 0: return "0";
case 1: return "1";
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10: return "10";
char[] buf = (char[])(perThreadBuffer.get());
int charPos = getChars(i, buf);
return new String(buf, charPos, 12 - charPos);
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979

I think this implementation is till JDK 1.4

and they have changed it in jdk 1.5

jQuery in Action, 2nd edition
subject: toString() behaviour?
Similar Threads
Interesting Question
* Byte == comparision
byteRef.toString() == byteRef.toString() ?
toString( ) method
Why is " == " on different strings with same content is returning "false"