Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 11 of 11

Thread: Why do these two lines of code not produce the same results? ("==" vs. ".equals()")

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Why do these two lines of code not produce the same results? ("==" vs. ".equals()")

    Hello,
    This is my very first post. I'm working on a codingBat problem and would like to know why the arguments in each if statement below produce different results. Can anyone help?

    if (str.substring(1,4) == "del") This if statement returns 'false'
    if (str.substring(1,4).equals("del")) This if statement returns 'true'

    here is an example with the line above where the if statement returns false...

    public class DelDel {
    	public static void main(String[] args) {
    		String str = "adel";
    		if (str.substring(1,4) == "del"){
    			System.out.println(str.substring(0, 1) + str.substring(4));
    	        } else {
    		System.out.println(str);
    	        }
    	}
    }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why do these two lines of code not produce the same results? ("==" vs. ".equals()")

    == compares the values in the variables
    equals() compares the values in the objects

    String x = "X";
    x is a variable that has the address of the object that contains the value: "X"
    If you don't understand my answer, don't ignore it, ask a question.

  3. The Following User Says Thank You to Norm For This Useful Post:

    Kalagor (September 3rd, 2014)

  4. #3
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Why do these two lines of code not produce the same results? ("==" vs. ".equals()")

    Look at the following code:
    String a = "abc";
    String b = "abc";
    boolean equal = a == b;
    Here you have 2 variables of type String, one of them is a and one of them is b.
    Both variables are referencing a different Object of Type String. The variable a is referencing a String with the contents "abc". The variable b is referencing a String with the contents "abc" as well. But both reference different String Objects.

    If you use the == operator in java you compare the references of variables. The statement "equal = a == b;" is evaluating whether the variables a and b are referencing the same Object or not. Since I said that both reference different objects which just happen to have the same contents it should be obvious that the variable equal will have the value false.

    If I was to write:
    String a = "abc";
    String b = a;
    boolean equal = a == b;
    Then the result of the evaluation would be true and the variable equal would have the value true assigned to it.


    If we use the method equals(Object other) then we call whatever code is defined in the particular class. For the String class the equals method will check whether the contents of 2 Objects are the same.

  5. The Following User Says Thank You to Cornix For This Useful Post:

    Kalagor (September 3rd, 2014)

  6. #4
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why do these two lines of code not produce the same results? ("==" vs. ".equals()")

    Wow... nice first experience. Thanks Norm and Cornix... both answers helped me understand better. Appreciate it.

  7. #5
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Why do these two lines of code not produce the same results? ("==" vs. ".equals()")

    Quote Originally Posted by Cornix View Post
    the variable equal will have the value false.
    Have you tried to run that code? The result will be true due to the String Literal Pool.
    Improving the world one idiot at a time!

  8. #6
    Senior Member
    Join Date
    Jul 2013
    Location
    Europe
    Posts
    666
    Thanks
    0
    Thanked 121 Times in 105 Posts

    Default Re: Why do these two lines of code not produce the same results? ("==" vs. ".equals()")

    I am trying to teach theory, not compiler optimization.

  9. #7
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why do these two lines of code not produce the same results? ("==" vs. ".equals()")

    Hello Cornix,

    I just tried the code you mentioned (here below) and the variable equal actually give me a value of true instead of false. Can you help to see if I did something wrong.

    		String a = "abc";
    		String b = "abc";
    		boolean equal = a == b;
    		System.out.println(equal);

  10. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Why do these two lines of code not produce the same results? ("==" vs. ".equals()")

    The compiler does special magic with Strings defined at compile time to optimize. Do some research on the String Literal Pool. See post#5
    If you don't understand my answer, don't ignore it, ask a question.

  11. #9
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: Why do these two lines of code not produce the same results? ("==" vs. ".equals()")

    Quote Originally Posted by Kalagor View Post
    I just tried the code you mentioned (here below) and the variable equal actually give me a value of true instead of false. Can you help to see if I did something wrong.
    Post the _entire_ test program you used, and make sure you use a good text editor. The only reason that can be false is if the strings are different.

    In this case, the comparison is very related to hashCode(). Specifically, in this case, "abc".hashCode().

  12. #10
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Why do these two lines of code not produce the same results? ("==" vs. ".equals()")

    Quote Originally Posted by Cornix View Post
    I am trying to teach theory, not compiler optimization.
    In that case use better examples. As reply #7 shows you just confused the poor bugger.
    Improving the world one idiot at a time!

  13. #11
    Junior Member
    Join Date
    Sep 2014
    Posts
    4
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: Why do these two lines of code not produce the same results? ("==" vs. ".equals()")

    Excellent. Thanks guys. It's ok with me because I can use it all for learning... probably a little deeper than needed at this point in my learning, but there is definitely a benefit. I'm so thankful you guys are willing to help out. I found a nice explanation based on your suggestion Norm. Thanks. Here it is in case anyone cares... What is String literal pool?

    I'll set this thread back to solved.

Similar Threads

  1. Replies: 4
    Last Post: July 18th, 2014, 02:04 AM
  2. Replies: 1
    Last Post: July 16th, 2014, 04:16 AM
  3. Replies: 2
    Last Post: May 22nd, 2014, 01:17 PM
  4. Replies: 2
    Last Post: June 22nd, 2013, 10:30 AM
  5. Replies: 3
    Last Post: December 7th, 2011, 02:03 AM

Tags for this Thread