What is the difference between creating a string without using a new operator and creating a String using a new 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 creating string without using the new operator and creating String by using a new Operator


Please follow our ITGARDEN YouTube channel for Billing System Project development work using Spring Boot. The Project source code absolutely free. This project is very good for those who are looking for real-time project experience in Spring Boot. To understand the Billing System Project code, watch all Billing System-related YouTube videos from this link. Click Here
Our YouTube Channel
https://www.youtube.com/channel/UChj5CeuWaHIFr4RkXoG3iJA?view_as=subscriber
Please follow me on Medium.com for all billing system related stories and Java-based technical Articles
https://suresh-stalin.medium.com/
Note: To find all billing System-related stories find “billing system itgarden” in medium.com

Close Menu
×
×

Cart