Mithun Sasidharan
Mithun Sasidharan

Reputation: 20920

Not working String comparison in Java

This code is not working:

String name = "Bob";
String name1 = "Anne";
if name = name1;
System.out.println ("Hello");

I am a beginner in Java, please help we with this code. I am trying to compare two strings.

Upvotes: 1

Views: 1419

Answers (5)

gprathour
gprathour

Reputation: 15333

Your Mistakes :

  1. You are using "=" operator to compare strings. It is not a conditional operator, it is an Assignment operator. The conditional operator in Java is "==" which is used to compare two values if they are equal or not. You even cannot use this one for Strings.

  2. You are writing like this :

    if name = name1; 
    System.out.println ("Hello"); 
    

You have put a semi-colon at the end of if statement. So it will do nothing (if your condition is supposed to be right, which is not in this case) however the condition is true or not.

  1. You are missing parantheses around the condition given in if statement.

    Synatx of if statement is : if(condition)
    

So it is must to write "()" around your condition.

Now, for comparing Strings, String class gives us methods like :

stringOne.equals(stringTwo)

It checks for exactly the same string.

or

stringOne.equalsIgnoreCase(stringTwo)

It will ignore Caps-Small letter case.

Upvotes: 3

Mr1159pm
Mr1159pm

Reputation: 774

if(name.equals(name1))
  System.out.println("Hello");

== works only when you compare primitives like int or long. If you want to compare String you have to use either equals() or compareTo(). Single = is an assignment not comparison by doing name=name1 you essentially assign string name1 to variable name.

Upvotes: 1

Mithun Sasidharan
Mithun Sasidharan

Reputation: 20920

You must compare the two variables like this

if (name.equals(name1)) 

This should work and not the way you did it!!!

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500525

You want:

if (name.equals(name1))

Note that you don't want

if (name == name1)

which would be syntactically correct, but would compare the two string references for equality, rather than comparing whether the objects involved represent the same sequence of characters

Further, note that even the top version will simply perform an ordinal comparison of the UTF-16 code units in the string. If the two strings logically represent the same characters but are in different forms, you may not get the result you expect. If you want to do a culture-sensitive comparison, you should look at Collator.

Additionally, I'd recommend that if you're really new to Java, you start off with console apps and/or unit tests to explore the language instead of JSP - it'll give you a much smoother cycle while you're learning the basics, and a simpler environment to work in.

Even more additionally, the code given at the top will throw a NullPointerException if name is a null reference. You should consider what you want to happen at that point - if the string being null would represent a bug anyway, then the exception is probably appropriate; otherwise, you might want to change the code. One useful method in Guava (which is chock full of good stuff) is Objects.equal:

if (Objects.equal(name, name1))

The method return true if both arguments are null, false if exactly one argument is null, and the result of calling equals() if they're both non-null. Very handy.

Upvotes: 14

Edwin Buck
Edwin Buck

Reputation: 70909

Your posted code isn't really Java. In addition, you don't compare '==' with the assignment operator '='. Finally, to do proper comparison of 'Object's or anything descended from Objects you need to use the .equals(...) method.

Comparing with == means "is the same object", not "is an object with the same value". The difference seems to be small; but, if you opt to compare objects by their value, it is not small at all. Two Objects can be created with identical "values", and only .equals(...) allows you to consider those two Objects to be the same.

Upvotes: 0

Related Questions