String Comparisions - 4 Examples with == and equals( )

String Comparisions - 4 Examples with == and equals( )

Hi Folks,

Welcome to the third Edition of the LinkedIn Newsletter - Learn with Swaroop Nadella.


Today let's learn about the String Comparisions concept with == and equals( ) method.

It is an important concept to be aware in Java String and would be important for interview perspective as well


Do practice this on Coding IDE to understand it better for the Java Strings concepts.

Whenever we create a String in Java, it is either placed in String Constant Pool or Heap Memory.

  • Heap Memory is the larger space of memory allocated by JVM during the run time, to store Java Objects created during the program execution
  • String Constant Pool is a Special memory inside the Heap Memory, to store all the unique String Constants or Literals.


Image credits - snapshot from Swaroop Nadella live classes


//Approach 1 -- Placed in the String Constant Pool

String str = "Swaroop";

When we create more String variables with assigning the value "Swaroop" as in Approach 1, all would refer to the same address in the SCP.


//Approach 2 -- Placed in the Heap Memory outside SCP

String str1 = new String("SwaroopN");

When we create more String variables using the new keyword as in Approach 2, it would create new Objects each time in the Heap memory outside SCP.


Image credits - snapshot from Swaroop Nadella live classes


Now let's learn the String Comparisions using the concepts learnt above for SCP and Heap Memory

Some Important points to note about == and equals() method

  • == would check for the address where it is placed in the memory (SCP or Heap Memory)
  • equals( ) inbuilt String method would check for the value inside the String variables.


Example 1:

String str1 = "Welcome"; //Placed in SCP

String str2 = "Welcome"; //Refers to same Constant created earlier in SCP


Here str1 and str2 - address refers to same String Constant "Welcome" in the SCP.

So str1==str2 --> checks for the address reference and it returns boolean value true.

And str1.equals(str2) --> checks for the value "Welcome" and it returns boolean value true


Now let's execute this on the Eclipse IDE to get the output boolean value true for both str1 and str2.

Image credits - Swaroop Nadella coding editor - Eclipse IDE


Example 2:

String s1 = new String("Swaroop"); //Creates new String Object in the Heap Memory

String s2 = new String("Swaroop"); //Creates another new String Object in the Heap Memory


Here s1 and s2 - address refers to different String Objects in the Heap Memory.

So s1==s2 --> checks for the address references in the Memory and it returns boolean value false.

And s1.equals(s2) --> checks for the value "Swaroop" and it returns boolean value true


Now let's execute this on the Eclipse IDE to get the output boolean value - false and true.

Image credits - Swaroop Nadella coding editor - Eclipse IDE


Example 3:

String str1 = "Welcome"; //Created in SCP

String str2 = new String("Welcome"); //Created in Heap memory - outside SCP


Here str1 refers to Constant in SCP and str2 refers to String Object in the Heap Memory.

So str1==str2 --> checks for the address references in the Memory and it returns boolean value false.

And s1.equals(s2) --> checks for the value "Welcome" and it returns boolean value true


Now let's execute this on the Eclipse IDE to get the output boolean value - false and true.

Image credits - Swaroop Nadella coding editor - Eclipse IDE


Example 4:

String s1 = "Welcome"; // Placed in the SCP

String s2 = new String("Welcome"); //Heap Memory object

String s3 = s2; //Refers to Heap memory object - s2


Here s1 refers to Constant in SCP, s2 refers to String Object in the Heap Memory and s3 refers to the same Object s2 in the Heap Memory.

So s1==s3 --> checks for the address references in the Memory and it returns boolean value false.

And s1.equals(s3) --> checks for the value "Welcome" and it returns boolean value true

And s2==s3 --> checks for the address references in the Memory and it returns boolean value true.

And s2.equals(s3) --> checks for the value "Welcome" and it returns boolean value true


Image credits - Swaroop Nadella coding editor - Eclipse IDE


Do practice these concepts in Coding IDE and it would help to understand these better.

Hope you found this article useful.

You can learn the similar concepts - Complete Core Java from my Udemy course in 30 days, which could help you in Automation Testing with any tool (Selenium, Appium, RestAssured, Playwright, etc.)


Like, Comment, Repost to help reach more people learn String Comparisions concepts.

Happy Weekend!


Swaroop Nadella

Test Automation Engineer, Tech Educator

Core Java and Coding for Automation Testers — Udemy Course (available for Udemy Business users)

Swaroop Nadella Academy Store — Quizzes and Courses

Liju Raju

Experienced QA Engineer | Automation Testing | SDET | Manual Testing | Selenium | Java | BDD Cucumber | Agile Enthusiast | Rest Assured | API Testing |

1 个月

Very informative

Akash Chaudhary

?? Full Stack QA | Architecting Excellence in Software Quality | "I'm the one who tests"

1 个月

That’s a really detailed explanation. However, it has one critical point missing. The ‘==‘ checks for memory reference which is totally correct but the ‘equals(object)’ doesn’t only check the content. It checks both, the address + content. If and only if the address is SAME then the equals() method returns true without even checking the content of the string. If not, then it checks the content. In the very first example, the ‘equals()’ method and the ‘==‘ are doing same thing and that is why it is true for both. The content of ‘Welcome’ is ignored.

Hussain Ahmed

Passionate about Software testing, QA and technology.

1 个月

Thank you for sharing such insightful content on Java Strings. ??

Swaroop Nadella

Teaching testers how to code and automate

1 个月

Subscribe to my LinkedIn Newsletter: New Post published every week. https://www.dhirubhai.net/newsletters/learn-with-swaroop-nadella-7245633953175363584/

回复

要查看或添加评论,请登录

社区洞察

其他会员也浏览了