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
Joined: Feb 04, 2001
Posts: 400
posted
0
False
Fazal Ahmad
Greenhorn
Joined: Mar 06, 2001
Posts: 18
posted
0
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
Joined: Jan 10, 2001
Posts: 42
posted
0
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
Joined: Mar 06, 2001
Posts: 18
posted
0
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
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.