GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE  >  Core Java
Go To First  |  Previous Question  |  Next Question 
 Core Java  |  Question 498 of 502    Print  
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));
}
}



  
Total Answers and Comments: 10 Last Update: October 30, 2009     Asked by: aruneverb4u 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: priya.java
 
12 true,

s2=s1.toString(), ref to same object.

if s2=new String(s1);---Object ref will be diffrent , ans ans will be false

Above answer was rated as good by the following members:
ae8166558de88, geeta.jagan
July 21, 2009 21:25:04   #1  
myogeshchavan97 Member Since: December 2008   Contribution: 28    

RE: Find the Output
The output of this program is 12.
explanation:
String.valueOf() is used to convert to string type.Thus 1 is converted to string and using + sign 2 is concatenated to 1 (after converting 2 to string)
Hence the result.

 
Is this answer useful? Yes | No
July 23, 2009 02:23:45   #2  
rave123nitb Member Since: July 2009   Contribution: 1    

RE: Find the Output
12 true
 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
July 27, 2009 03:24:12   #3  
mukuldhali Member Since: June 2009   Contribution: 1    

RE: Find the Output

There will be a compiler error if the class A is not public. If the class is public then the out put will be:
12 true


 
Is this answer useful? Yes | NoAnswer is useful 0   Answer is not useful 1Overall Rating: -1    
August 12, 2009 03:10:15   #4  
Sarje Member Since: August 2009   Contribution: 62    

RE: Find the Output
12 false
false because operator checks reference equality not object content equality and s1 and s2 are two different references.

 
Is this answer useful? Yes | No
August 26, 2009 00:48:42   #5  
priya.java Member Since: August 2009   Contribution: 1    

RE: Find the Output
12 true

s2 s1.toString() ref to same object.

if s2 new String(s1);---Object ref will be diffrent ans ans will be false

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 0Overall Rating: +1    
September 18, 2009 01:45:36   #6  
vittal.kv Member Since: September 2009   Contribution: 1    

RE: Find the Output
12 true.

1+2 gives 12 as string values will be appended
and both the string are reffering to same instance

 
Is this answer useful? Yes | No
September 28, 2009 10:32:36   #7  
Muthu Kumaran Arunachalam Member Since: September 2009   Contribution: 1    

RE: Find the Output
12 True.
Since the String s1 s2 are String constants and they pointing the same reference in String Constant Pool the
comparison s1 s2 will print true.

 
Is this answer useful? Yes | No
October 14, 2009 19:26:40   #8  
vdkrav Member Since: October 2009   Contribution: 1    

RE: Find the Output
12 true

Nobody noticed comma!

 
Is this answer useful? Yes | No
October 21, 2009 05:57:44   #9  
ae8166558de88 Member Since: October 2009   Contribution: 3    

RE: Find the Output
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

 
Is this answer useful? Yes | No
October 29, 2009 08:05:59   #10  
StewartS Member Since: October 2009   Contribution: 1    

RE: Find the Output
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".


 
Is this answer useful? Yes | No


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape