I'm new in java
I want to compare 2 string
Can I check if they are the same? like this?
Code:String t1;
String t2;
if (t1 == t2)
Printable View
I'm new in java
I want to compare 2 string
Can I check if they are the same? like this?
Code:String t1;
String t2;
if (t1 == t2)
try this
Make sure t1 is not null or you will get a null pointer exception.Code:if (t1.equals(t2))
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 : )
Actually your second example will be equal as well. This is an under the covers Java thing. Take the following code
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.Code:String one = "hello";
String two = "hello";
if (one == two)
System.out.println("The same");
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.
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
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?Code:String one = "hello";
String two = "hello";
if (one == two)
System.out.println("The same");
Is the compiler capability to index Strings came with Java 6?
Or is it already that way from Java1.0?
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:
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:Code:public static final String VALUE_ONE = "one";
public static final String VALUE_TWO = "two";
private String myValue;
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.Code:if (myValue = VALUE_ONE)
I hope I didn't confuse anyone with that.
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?
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?Code:if (myValue = VALUE_ONE)
How come u were able to use the logical statement in an if clause then if it didn't return a boolean?:confused:
thanks to all of you for the explanation