Forums Register Login

Byte to string

+Pie Number of slices to send: Send
majji exam 1 qno 9
Byte b1 = new Byte("127");
2:
3: if(b1.toString() == b1.toString())
4: System.out.println("True");
5: else
6: System.out.println("False");
what will be the output???
+Pie Number of slices to send: Send
False
+Pie Number of slices to send: Send
Hi Preeti:
The output would be 'False'. The reasoning behind is that the b1.toString() would return a String object each time and we all know that if we use '==' on 2 String objects they would not be equal (until and unless they were constructed using the same literal, however thats not the case here)
Hope the above helps.
Fazal

+Pie Number of slices to send: Send
Hi Fazal,
Byte b1 = new Byte("127"); //line
if(b1.toString() == b1.toString())
System.out.println("True");
else
System.out.println("False");

in place of line 1 if u change
String b1 = new String("Java");
it will print True. Why ? diff behavior betn Byte(Wrapper class) and String.
i think in String : b1.toString() return same obj ref.

Avi.
+Pie Number of slices to send: Send
Hi Arvind:
Quite true; if you call the toString() on a String object, it would return itself i.e. a String object.
However, if you call the toString() on a Byte object, it would return a new String object representing the Byte object's value.
Fazal
If I'd had more time, I would have written a shorter letter. -T.S. Eliot such a short, tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 686 times.
Similar Threads
Why is " == " on different strings with same content is returning "false"
Interesting Question
toString( ) method
byteRef.toString() == byteRef.toString() ?
* Byte == comparision
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 14:36:58.