Results 1 to 9 of 9
  1. #1
    artemff is offline Member
    Join Date
    Dec 2009
    Posts
    41
    Rep Power
    0

    Question How compare two String?

    How compare two String? S1==S2 don't always work correctly.

  2. #2
    adz
    adz is offline Member
    Join Date
    Jul 2009
    Posts
    35
    Rep Power
    0

    Default

    use equals
    Java Code:
    String a = "hahahahaahha";
    String b = "hshasdasdsadsa";
    if (a.equals(b)) {
    ...
    }
    May as well note, since you may not know if you have to check a vars equality against a String that you know ALWAYS do it with the var in the equals like the following:
    Java Code:
    String whatever = "turkey";
    
    if ("chicken".equals(whatever)) {
    ...
    }
    as opposed to:
    Java Code:
    if (whatever.equals("chicken")) {..}
    Reason is nullys but yeah

  3. #3
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    Reason you can't use String1 == String2 is because they are objects, not primitive data. Since String is an object and not primitive data, String1 == String2 compares their address instead of their values (which is what you're trying to do). You can do int1 == int2 or char1 == char2 or double1 == double2 to compare values because int, char, and double are primitive data.

  4. #4
    artemff is offline Member
    Join Date
    Dec 2009
    Posts
    41
    Rep Power
    0

    Default

    Lil_Aziz1, adz, Thanks.

  5. #5
    Lil_Aziz1's Avatar
    Lil_Aziz1 is offline Senior Member
    Join Date
    Dec 2009
    Location
    United States
    Posts
    343
    Rep Power
    5

    Default

    ur welcome

  6. #6
    j2me64's Avatar
    j2me64 is offline Senior Member
    Join Date
    Sep 2009
    Location
    Zurich, Switzerland
    Posts
    962
    Rep Power
    6

    Default

    Quote Originally Posted by Lil_Aziz1 View Post
    Reason you can't use String1 == String2 is because they are objects, not primitive data. Since String is an object and not primitive data, String1 == String2 compares their address instead of their values (which is what you're trying to do). You can do int1 == int2 or char1 == char2 or double1 == double2 to compare values because int, char, and double are primitive data.
    but why does the following code returns true:

    Java Code:
    		String s1 = "hello";
    		String s2 = "hello";
    		System.out.println(s1==s2);
    Last edited by j2me64; 08-28-2011 at 10:34 AM.

  7. #7
    lok pun is offline Member
    Join Date
    Aug 2011
    Posts
    5
    Rep Power
    0

    Default

    String a , b;

    a.equal(b);

    a==b

    those are applicable try it

  8. #8
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,798
    Rep Power
    7

    Default

    Quote Originally Posted by j2me64 View Post
    but why does the following code returns true:
    Are you asking because you do not know or are you asking to improve the knowledge of n00bs?

  9. #9
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,250
    Rep Power
    19

    Default

    Zombie alert. j2me64, please stop resurrecting zombies. This is the second I've seen today.

    db

Similar Threads

  1. Replies: 8
    Last Post: 02-22-2014, 08:06 AM
  2. String compare issues?
    By emp in forum New To Java
    Replies: 3
    Last Post: 04-21-2009, 07:16 AM
  3. Compare value in array
    By cassysumandak in forum New To Java
    Replies: 5
    Last Post: 04-04-2009, 11:43 PM
  4. Compare 2 XML
    By Peter in forum XML
    Replies: 1
    Last Post: 07-05-2007, 02:58 AM
  5. String Compare not working
    By Revelation in forum New To Java
    Replies: 3
    Last Post: 06-30-2007, 06:43 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •