Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Byte to string

 
preeti dengri
Ranch Hand
Posts: 111
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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???
 
Stevie Kaligis
Ranch Hand
Posts: 400
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
False
 
Fazal Ahmad
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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

 
Vegad Arvind
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Fazal Ahmad
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic