Find the Output

Class A{
public static void main(String[] a){
System.out.print(String.valueOf(1)+String.valueOf(2));
String s1="s1";
String s2=s1.toString();
System.out.print(","+(s1==s2));
}
}

Questions by aruneverb4u

Editorial / Best Answer

rave123nitb  

  • Member Since Jul-2009 | Jul 23rd, 2009


12,true

Showing Answers 1 - 0 of 0 Answers

Sarje

  • Aug 12th, 2009
 

12 false
false because == operator checks reference equality not object content equality and s1 and s2 are two different references.

  Was this answer useful?  Yes

First SOP is like every one is saying but for second this is what actually written in String class:

public String toString() {
return this;
}

so when compared using (==) same reference values are compared and it gives true

StewartS

  • Oct 29th, 2009
 

The output is:

       12,true

Explanation:
The easy bit is that the first call to print() displays "12".

The Javadoc for String.toString() says the return value is:

       "This object (which is already a string!) is itself returned."

So s1 and s2 refer to the same value. This means that the (s1 == s2) comparison is true. Therefore the second call to print displays ",true".

java indian

  • Mar 10th, 2010
 

String.valueof(int )  = Returns the string representation of the int argument. so 12,
and s2=s1.stringto() is assigning the same value. so true.
The output is 12, true :)

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions