/**
*
* TODO Description go here.
*
* @author <a href="mailto:varra@outlook.com">Rajakrishna V. Reddy</a>
* @version 1.0
*
*/
public class StringManual
{
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String[] args)
{
String s1 = "foo"; //Creates a string in String constant pool rather in heap. [Happens the same for all strings created as ""]
String s2 = "foo"; // String constant pool will not create a new string as there is already a string with the same content S1, so s2 points to the s1 i.e s2=s1.
System.out.println(s1 == s2); // TRUE: because s2=s1, both references are same i.e s1.
System.out.println(s1.equals(s2)); // TRUE: based on content.
String s3 = new String("foo"); //Creates a string in heap. [Happens the same for all strings created as new String.]
System.out.println(s1 == s3); // FALSE: because both references are not same i.e s1 is in constant pool and s3 is in heap.
System.out.println(s1.equals(s3)); // TRUE: based on content.
}
}
*
* TODO Description go here.
*
* @author <a href="mailto:varra@outlook.com">Rajakrishna V. Reddy</a>
* @version 1.0
*
*/
public class StringManual
{
/**
* The main method.
*
* @param args
* the arguments
*/
public static void main(String[] args)
{
String s1 = "foo"; //Creates a string in String constant pool rather in heap. [Happens the same for all strings created as ""]
String s2 = "foo"; // String constant pool will not create a new string as there is already a string with the same content S1, so s2 points to the s1 i.e s2=s1.
System.out.println(s1 == s2); // TRUE: because s2=s1, both references are same i.e s1.
System.out.println(s1.equals(s2)); // TRUE: based on content.
String s3 = new String("foo"); //Creates a string in heap. [Happens the same for all strings created as new String.]
System.out.println(s1 == s3); // FALSE: because both references are not same i.e s1 is in constant pool and s3 is in heap.
System.out.println(s1.equals(s3)); // TRUE: based on content.
}
}
No comments:
Post a Comment