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

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)

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.

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



I am a Java / J2EE Consultant. Happy to learn.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store