Naresh,
I just added few print out statements to show how the strings work. s0 is a new string having reference to the string "java", s1, s2, s3 are the strings which are used to work on the string "java" but they are loast as soon as they are done and you can see that in the below listed code. Strings are immutable.
*----------------------------code----------------------------------------*
String s0 = new String("Java");
System.out.println(
" The String is a new string and it has the reference to the string s0 ="
+ s0);
String s1 = s0.trim();
System.out.println(" The string s1 = s0.trim() = " + s0);
String s2 = s0.substring(0, 4);
System.out.println(" The string s2 = s0.substring(0, 4) = " + s2);
String s3 = s0.replace('K', 'a');
System.out.println(" The string s3 = s0.replace('K', 'a') = " + s3);
System.out.println(" The String s0 =" + s0);
System.out.println(" The String s0 =" + s1);
System.out.println(" The String s0 =" + s2);
System.out.println(" The String s0 =" + s3);
System.out.print("s0 == s1 = ");
System.out.println(s0 == s1);
System.out.print("s0 == s2 = ");
System.out.println(s0 == s2);
System.out.print("s0 == s3 = ");
System.out.println(s0 == s3);
*---------------------------------------------------------------------*
The String is a new string and it has the reference to the string s0 =Java
The string s1 = s0.trim() = Java
The string s2 = s0.substring(0, 4) = Java
The string s3 = s0.replace('K', 'a') = Java
The String s0 =Java
The String s0 =Java
The String s0 =Java
The String s0 =Java
s0 == s1 = true
s0 == s2 = true
s0 == s3 = true
*-------------------------------------------------------*
hope this helps to understand