When a call is made to the toString() method of the b1, the method calls that take place are
given below
generic : Byte.toString()-> String.valueOf(int i)-> Integer.toString(int i, int radix)
specific values: Byte.toString()-> String.valueOf(127)-> Integer.toString(127, 10)
..the thing to understand here is that everytime you make a call to the "Byte.toString()", you get a new instance of
"String" with the same value (127 to be specific)..
what happens in Integer.toString() is evident in the return statement shown here
Integer.toString(int i, int radix){
...
...
...
return new String(some stuff);
}
so,it is obvious that they(1st call to "b1.toString()" and 2nd call to "b1.toString()") are not the same. I have tried to illustrate this by tweaking your code a bit
1: Byte b1 = new Byte("127");
2: String s = b1.toString();
3: String s1 = b1.toString();
4: if(s == s1)
5: System.out.println("True");
6: else
7: System.out.println("False");
s is one instance of "String" class
s1 is another instance "String" class
so if you ask a question is (s==s1)?
the answer would be "NO"(or false) as they are pointing to different locations
hope this helps.