Results 1 to 8 of 8
  1. #1
    elizabeth is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

    Default how to compare two strings

    I'm new in java
    I want to compare 2 string
    Can I check if they are the same? like this?
    Java Code:
    String t1;
    String t2;
     if (t1 == t2)

  2. #2
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default

    try this
    Java Code:
    if (t1.equals(t2))
    Make sure t1 is not null or you will get a null pointer exception.

  3. #3
    yiweiang is offline Member
    Join Date
    Aug 2007
    Posts
    15
    Rep Power
    0

    Default

    Hi there,

    The reason why you cannot compare the two strings like that is because when you set a string, which is an object "==" to another object String, what happens is that it compares to see if the strings are in the same memory location, and not whether their values are the same.

    This is an example where "==" works.

    String one="hello";
    string two=one;

    if(one==two)
    {
    System.out.println("the same");
    }

    The result of the code above will print out "The Same", because both strings are pointing to the same memory location.

    However, when you do:

    String one="hello";
    String two="hello";

    You are creating two objects of type string. In your head, picture two things being created. So when you ask if they are "==" the computer goes "no?" because they are two different objects at two different memory locations. To see if their VALUES are the same, use the method that brianhks suggested : )

  4. #4
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default

    Actually your second example will be equal as well. This is an under the covers Java thing. Take the following code
    Java Code:
    String one = "hello";
    String two = "hello";
    
    if (one == two)
      System.out.println("The same");
    Will that print out "The same"? Yes. The reason is when the file is compiled the static strings "hello" are placed in a static string table within the class file. And because it is efficient it only creates one "hello" string. So when it is ran both variables one and two are pointing to the same static string object.

    Now to clarify for those I may have confused. Always use .equals() to compare the contents of two strings, == may give you strange behavior you didn't expect.

  5. #5
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    Wow, that i just found out brianhks :)
    I didn't know that the compiler indexes the Strings we used in a class when its compiled and taht it create the same refereneces if it finds a similar String input

    From the previous examples ive done and see, that statement
    Java Code:
    String one = "hello";
    String two = "hello";
    
    if (one == two)
      System.out.println("The same");
    wouldn't print "The same" since they refer to different memory location, but from what u just said, it does refer to the same memory location is it?
    Is the compiler capability to index Strings came with Java 6?
    Or is it already that way from Java1.0?

  6. #6
    brianhks is offline Senior Member
    Join Date
    Jul 2007
    Posts
    135
    Rep Power
    0

    Default

    It has been that way for as long as I know. The interesting thing is that it doesn't actually create a String object for the static strings such as "hello" in the previous example. The string object is compiled into the class file and the variables one and two just point to the part of the class file that is loaded into memory. This works because String are immutable. Knowing this fact lets you do some other tricks. If you create some static final string members and your variable is set to them you can tell what your variable is set to by using the "=" instead of .equals() method. For example:
    Java Code:
    public static final String VALUE_ONE = "one";
    public static final String VALUE_TWO = "two";
    
    private String myValue;
    Now if I know that myValue is only set by assigning it to either VALUE_ONE or VALUE_TWO I can create an if statement like this:
    Java Code:
    if (myValue = VALUE_ONE)
    and it will work just as I want because, if myValue is set to "one" it is because it is pointing to the same string object as VALUE_ONE is. The only reason for doing this is because the "=" comparison is a lot faster then the .equals() method call.

    I hope I didn't confuse anyone with that.

  7. #7
    cruxblack is offline Senior Member
    Join Date
    Jul 2007
    Posts
    130
    Rep Power
    0

    Default

    Hmm, that i don't get
    I thought Java had get rid of the '=' use as a logical operator?:confused:

    Could u explain the logical steps please?
    myValue is a String, in this case a reference for the actual String, while VALUE_ONE is a constant, but still, from the statement, what i get is it actually create a reference for a class instance right?

    Java Code:
    if (myValue = VALUE_ONE)
    Then (myValue = VALUE_ONE) means putting the reference of VALUE_ONE into (in this term, as) the reference of myValue, but that doesnt return a Boolean right?
    How come u were able to use the logical statement in an if clause then if it didn't return a boolean?:confused:

  8. #8
    elizabeth is offline Member
    Join Date
    Jul 2007
    Posts
    40
    Rep Power
    0

Similar Threads

  1. How to Compare two Dates
    By Java Tip in forum java.util
    Replies: 1
    Last Post: 06-24-2008, 08:05 AM
  2. Compare 5 numbers
    By Snowboardmylife in forum New To Java
    Replies: 5
    Last Post: 04-15-2008, 08:04 PM
  3. Compare lists
    By JavaNoob in forum New To Java
    Replies: 2
    Last Post: 08-08-2007, 04:11 PM
  4. Compare 2 XML
    By Peter in forum XML
    Replies: 1
    Last Post: 07-05-2007, 03:58 AM
  5. compare speed
    By bbq in forum JDBC
    Replies: 1
    Last Post: 06-28-2007, 06:34 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
  •