hello, i have made the code below. however, when comparing the two strings they are not equal when in theory i thought they would be.

 String word="abc";
        String d="";
        d += "a";
        d += "b";
        d+= "c";
        if(word==d)
{
System.out.println("IS EQUAL");
}

When i just print out string d it prints out abc, but for some reason its not REALLY abc. why?

Recommended Answers

You're comparing references of string objects. Use String.equals() method to compare strings:

if(word.equals(d))
  //
else
 //
Jump to Post

All 2 Replies

Re: Adding to a string then comparing 80 80

You're comparing references of string objects. Use String.equals() method to compare strings:

if(word.equals(d))
  //
else
 //
Re: Adding to a string then comparing 80 80

i knew it was something like that >.<

thanks a ton

Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.19 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.