File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes one more abhilash mock question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of JavaScript Promises Essentials this week in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "one more abhilash mock question" Watch "one more abhilash mock question" New topic
Author

one more abhilash mock question

Don Bosco
Ranch Hand

Joined: Oct 31, 2002
Posts: 108
if("String".trim() == "String".trim())
System.out.println("Equal");
else
System.out.println("Not Equal");

output is Equal.
Can anybody explain me what is happening behind the scenes.


SCJP 1.4<p>Wingardium Leviosa!!
Dinesh Kumar
Ranch Hand

Joined: Jul 03, 2002
Posts: 54
According to Java API for the trim method,
"if there is no character with a code greater than '\u0020' in the string, then a new String object representing an empty string is created and returned."
So in this case it is just returning an empty string. Since, java optimises String usage by putting them on a common shared pool wherever possible, there is actually only one empty String object created and both point to the same object. That is why the comparison returns Equal.
Hope it helps.
Dinesh
SCJP 1.4
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
But all the characters in "String" have codes larger than \u0020.
Since "String" neither begins nor ends with a whitespace character, String.trim("String") returns its argument.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: one more abhilash mock question