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.
//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.
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
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.
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.
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.
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
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
Swaroop Nadella Academy Store — Quizzes and Courses
Experienced QA Engineer | Automation Testing | SDET | Manual Testing | Selenium | Java | BDD Cucumber | Agile Enthusiast | Rest Assured | API Testing |
1 个月Very informative
?? 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.
Passionate about Software testing, QA and technology.
1 个月Thank you for sharing such insightful content on Java Strings. ??
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/