String object creation with and without new

What is the difference between below statements?
1. String str = “Hello”;
In this case, “Hello” memory is allocated during compile time itself, and str refers to “Hello” String literal address. Also note that

String str_a = “Hello”;
String str_b = “Hello”;
Both of these refers to same String object, and another object does not get created for str_b.

2. String str1 = new String(“Hello”);

In case of new version, “Hello” is dynamically allocated during run time, and “Hello” gets memory allocate on Heap.
String str1 = new String(“Hello”);
String str2 = new String(“Hello”);
Both of these refers to different String object, and another object gets created for str2

Below is an example showing the same


public class Stringnew {
public static void main(String args[])
{
	String str_a = "Hello";
	String str_b = "Hello";

	String str1 = new String("Hello");
	String str2 = new String("Hello");

	if(str_a==str_b)//== checks address location
	{
		System.out.println("if(str_a==str_b)");
	}
	else
	{
		System.out.println("else (str_a==str_b)");
	}

	if(str_a.equals(str_b))//equals() checks content
	{
		System.out.println("if str_a.equals(str_b)");
	}
	else
	{
		System.out.println("else str_a.equals(str_b)");
	}

	if(str1==str2)//== checks address location
	{
		System.out.println("if str1.equals(str2)");
	}
	else
	{
		System.out.println("else (str_a==str_b)");
	}

	if(str1.equals(str2))//equals() checks content
	{
		System.out.println("if str1.equals(str2)");
	}
	else
	{
		System.out.println("else str1.equals(str2)");
	}
}
}

Output:
if(str_a==str_b)
if str_a.equals(str_b)
else (str_a==str_b)
if str1.equals(str2)

You may also like to read:
Difference between == and equals()
Can String be inherited?
How to override toString()

Difference between == and equals() method for a String

Result of using == and equals() method between two String objects is different.
== checks whether both String objects are referring to same address location.
equals() method checks whether the contents of both String objects are same.

public class Stringequals{

public static void main(String args[])
{
	String s1 =  new String("Hello");
	String s2 = new String("Hello");

	String str="Hello";

	String str1="Hello";

	//s2 and s3 refer to same address location
	String s3 = s2;

	if(s1==s2)//== checks address location
	{
		System.out.println("if(s1==s2)");
	}
	else
	{
		System.out.println("else (s1==s2)");
	}

	if(s2==s3)//== checks address location
	{
		System.out.println("if(s2==s3)");
	}
	else
	{
		System.out.println("else (s2==s3)");
	}

	if(s1.equals(s2))//equals checks contents of s1, s2
	{
		System.out.println("if s1 equals s2");
	}
	else
	{
		System.out.println("else s1 equals s2");
	}

	if(str==str1){
		System.out.println("if(str==str1)");
	}
}
}


Output:
else (s1==s2)
if(s2==s3)
if s1 equals s2
if(str==str1)

You may also like to read:
Inherit From String class
How to override toString() method
Difference between String and StringBuilder
String object creation with and without new

inherit String class

String class can never be a base class, as it is declared final. Due to some optimizations taking place while creating a String object, which Developers of Java do not want Application developers to inherit from String class or override its methods. Hence String class is declared as final.

Click here for detailed description of final class

Also note that all wrapper classes like java.lang.Integer, java.lang.Float, java.lang.Short, etc… are final as well. Hence they cannot be base classes.