aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Strings Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Strings" Watch "Strings" New topic
Author

Strings

prarthana reddy
Ranch Hand

Joined: Jul 24, 2006
Posts: 48
public class Test {

public static void main(String args[])
{
String s0 = new String("Java");
String s1 = s0.trim();
String s2 = s0.substring(0, 4);

String s3 = s0.toString();

System.out.println(s0 == s1);
System.out.println(s0 == s2);
System.out.println(s0 == s3);
}
}
In the above code , wont the trim(), substring() return a new string? But the answer is true for all the cases..
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
If these functions do not change string then the origanal reference will be return.
For example: public String trim() API:

If this String object represents an empty character sequence, or the first and last characters of character sequence represented by this String object both have codes greater than '\u0020' (the space character), then a reference to this String object is returned.

[ August 15, 2006: Message edited by: wise owen ]
Simeon Cherniy
Greenhorn

Joined: Jul 26, 2006
Posts: 21
Method trim (source code) . See 1471-1473 line (your case)


And the same for method substring (line 1125-1126):


[ August 15, 2006: Message edited by: Simeon Cherniy ]

5-year Student of <br />Moscow Institute of Physics and Technology.
prarthana reddy
Ranch Hand

Joined: Jul 24, 2006
Posts: 48
Thankyou for your answer..but am not very clear about it
wise owen
Ranch Hand

Joined: Feb 02, 2006
Posts: 2023
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strings