Originally posted by Ramesh Sahu:
import java.io.*;
class StringTest {
public static void main(String args[]) throws IOException{
System.out.println("s12"==((" s12 ".trim())));
System.out.println( "String" == "String".replace('t','t') );
}
}
You should always remember that
Besides String literals and compile time constant expressions
all String objects are created at run-time same as other objects.
When you say " s12 ".trim() then " s12 " object goes String pool
whereas string created on " s12 ".trim() invocation is created at
run-time hence distinct.
Hence "12"==((" s12 ".trim()))
returns false
When you invoke replace() , it returns same object reference if both
arguments are same.
Since both arguments are same hence replace method returns same reference
hence it returns true.