String new vs equal operator

What is the difference between
creating a string without using a new operator and creating a String using a new Operator

Suresh

--

if we declare a String variable with a value, the Java store the String value in the String pool area. if we declare another String variable, if we assign the same value as below, Java never allocates a new address for the second variable because, since the value is already available in the String pool area, java assigns the “Suresh” ‘s reference to the second variable that's it. so now both variables pointing the same reference variable. This process happens only if we declare a string variable like below. if check these two-variable using the “==” operator, it returns “true” because both are posting the same memory location(same reference)

String name1 = "Suresh";
String name2 = "Suresh";
System.out.println(name1 == name2); // true

Instead of declaring like above, if we declare a String variable like a blow, it never creates the same reference for both variables. the below comparison returns false because. when we use a new Operator to initialize the String variable, we are creating a new reference for each time so it will create two different references (Memory address) so, it returns false.

String name1 =  new String("Suresh");
String name2 = new String("Suresh");
System.out.println(name1 == name2); // false

I hope you understand of creating string without using the new operator and creating String by using a new Operator

--

--