#Help with strings

17 messages · Page 1 of 1 (latest)

true steeple
#

Hey guys, so I'm trying to run a program, and it wouldn't work if I used "s1 == s2;" (where s1 and s2 are strings). I need to use "s1.equals(s2);". Can somebody tell me the difference between the two?

lucid muskBOT
#

This post has been reserved for your question.

Hey @true steeple! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

cold jewel
#

The main difference is that == is used for reference comparison

#

But .equals is used for content operation

lucid muskBOT
#

Looks like you're having some trouble comparing strings, check out this stackoverflow question for help.

true steeple
cold jewel
#

You can test it by doing this trick

#

String s1 = "HELLO";

    String s2 = "HELLO";

    String s3 =  new String("HELLO");


    System.out.println(s1 == s2); 

    System.out.println(s1 == s3); 

System.out.println(s1.equals(s2)); System.out.println(s1.equals(s3));

#

s3 and s1 content is same when we use .equal it will return true

#

But s3 have new string object with new memory of string HELLO so when you do s1 == s3 it will return false

true steeple
cold jewel
#

Address means the memory which the operating system assigned to store the variable

#

When we use new keyword it creates new memory for it

lucid muskBOT
# true steeple Oh okay, thank you so much!

If you are finished with your post, please close it.
If you are not, please ignore this message.
Note that you will not be able to send further messages here after this post have been closed but you will be able to create new posts.