0

Possible Duplicate:
How do I compare strings in Java?

i have written some code which compares two strings "abc" and "de". The string abc is parsed and returned to "doc" to ext and then it is compared. Although it seems that if condition is true but still the else part is executing. where i am not getting plz help me ....thanks a lot.

public class xyz{

    String abc="doc2.doc";
    String de="doc";

    public static void main(String arg[]){

    xyz c=new xyz();

    String ext = null;
    String s =c.abc;
        String d =c.de;
    int i = s.lastIndexOf('.');

    if (i > 0 && i < s.length() - 1){
    ext = s.substring(i+1).toLowerCase();
    }
    System.out.println(ext);
    if(ext==d){

    System.out.println("true");
    }
    else{
    System.out.println("false");
    }


    }


    }
Community
  • 1
  • 1
sandy
  • 33
  • 6

7 Answers7

3

You cannot compare Strings with == operator.

You will have to use String.equals()

In your case it would be

if (ext.equals(d)) {
    System.out.println("true");
} else {
    System.out.println("false");
}
maba
  • 47,113
  • 10
  • 108
  • 118
1

== tests for reference equality.

.equals() tests for value equality.

Consequently, if you actually want to test whether two strings have the same value you should use .equals()

// These two have the same value
new String("test").equals("test") ==> true 

// ... but they are not the same object
new String("test") == "test" ==> false 

// ... neither are these
new String("test") == new String("test") ==> false 

In this case

if(ext.equals(d)) {
    System.out.println("true");
}
else {
    System.out.println("false");
}
Pez Cuckow
  • 14,048
  • 16
  • 80
  • 130
0

Use the equals operator to compare strings:

ext.equals(d)
codebox
  • 19,927
  • 9
  • 63
  • 81
0

you can write it with .equals()..

private String a = "lol";

public boolean isEqual(String test){
  return this.a.equals(test);
}
Cédric Verstraeten
  • 574
  • 1
  • 4
  • 15
0

You cannot compare strings with == as they are different objects. The contents may be the same, but that is not what == looks at. Use the equals method on one of the strings to compare it to the other string.

In your code, use:

if(d.equals(ext)){

If you ever need to compare two strings, and only one of them is a variable, I suggest the following approach:

"foo".equals(variableString)

This way you can be sure that you will not be calling equals on a Null object.

Another possibility is to use the compareTo method instead of the equals method which is also found in some other Java classes. The compareTo method returns 0 when the strings match.

You can find more information about strings in Java here.

Derecho
  • 161
  • 4
  • Thanks to every body for their valuable suggestion. but again i am little confused. I have written following code and it executes properly. public class xyz{ public static void main(String arg[]){ String abc="doc2.doc"; String de="doc2.doc"; if(abc==de){ System.out.println("true"); } else{ System.out.println("false"); } } } plz help me what to use whether == or equals() as i am going to compare with multiple strings. – sandy Oct 03 '12 at 11:12
  • If you want to compare strings, never use ==. It's not supposed to work as you intend it to. – Derecho Oct 03 '12 at 11:51
0

You should write

if(ext.equals(d))

instead of 

if(ext==d)
Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
0
String str1, str2, str; // References to instances of String or null

str1 = "hello"; // The reference str1 points to an instance of "hello" string
str2 = "hello"; // The reference str2 points to another instance of "hello" string
str3 = str2;    // The reference str3 points to the same instance which str2 does.

assert str1.equals( str2 ); // Both string instances are identical.
assert str1 != str2;        // Both references point to independent instances of string.
assert str2 == str3;        // Both references point to the same instance of string.
user1714889
  • 93
  • 1
  • 4