How do I compare strings in Java?
up vote
727
down vote
favorite
I've been using the ==
operator in my program to compare all my strings so far.
However, I ran into a bug, changed one of them into .equals()
instead, and it fixed the bug.
Is ==
bad? When should it and should it not be used? What's the difference?
java string equality
locked by animuson♦ Jan 26 '14 at 20:00
This question's answers are a collaborative effort: if you see something that can be improved, just edit the answer to improve it! No additional answers can be added here
comments disabled on deleted / locked posts / reviews |
up vote
727
down vote
favorite
I've been using the ==
operator in my program to compare all my strings so far.
However, I ran into a bug, changed one of them into .equals()
instead, and it fixed the bug.
Is ==
bad? When should it and should it not be used? What's the difference?
java string equality
locked by animuson♦ Jan 26 '14 at 20:00
This question's answers are a collaborative effort: if you see something that can be improved, just edit the answer to improve it! No additional answers can be added here
12
Also its good to know that, if you are overridding .equals () method, make sure you are overridding .hashcode () method, otherwise you will end up with violating equivalence relation b/w equals and hashcode. For more info refer java doc.
– Nageswaran
Nov 6 '13 at 12:44
Leaving a link to my explanation on why==
works the way it does on Objects: stackoverflow.com/a/19966154/2284641
– Johannes H.
Nov 14 '13 at 2:00
==
will work some of the time, as java has a String pool, where it tries to reuse memory references of commonly used strings. But==
compares that objects are equal, not the values... so.equals()
is the proper use you want to use.
– James Oravec
Nov 14 '13 at 23:17
Never use == to test whether Strings are the same, unless you enjoy tracking down subtle errors and studying the intricacies of the Java String interning process."12"=="1"+2
is false (probably)
– Flight Odyssey
Dec 23 '13 at 6:04
comments disabled on deleted / locked posts / reviews |
up vote
727
down vote
favorite
up vote
727
down vote
favorite
I've been using the ==
operator in my program to compare all my strings so far.
However, I ran into a bug, changed one of them into .equals()
instead, and it fixed the bug.
Is ==
bad? When should it and should it not be used? What's the difference?
java string equality
I've been using the ==
operator in my program to compare all my strings so far.
However, I ran into a bug, changed one of them into .equals()
instead, and it fixed the bug.
Is ==
bad? When should it and should it not be used? What's the difference?
java string equality
java string equality
edited Jan 23 '13 at 13:36
community wiki
Nathan H
locked by animuson♦ Jan 26 '14 at 20:00
This question's answers are a collaborative effort: if you see something that can be improved, just edit the answer to improve it! No additional answers can be added here
locked by animuson♦ Jan 26 '14 at 20:00
This question's answers are a collaborative effort: if you see something that can be improved, just edit the answer to improve it! No additional answers can be added here
12
Also its good to know that, if you are overridding .equals () method, make sure you are overridding .hashcode () method, otherwise you will end up with violating equivalence relation b/w equals and hashcode. For more info refer java doc.
– Nageswaran
Nov 6 '13 at 12:44
Leaving a link to my explanation on why==
works the way it does on Objects: stackoverflow.com/a/19966154/2284641
– Johannes H.
Nov 14 '13 at 2:00
==
will work some of the time, as java has a String pool, where it tries to reuse memory references of commonly used strings. But==
compares that objects are equal, not the values... so.equals()
is the proper use you want to use.
– James Oravec
Nov 14 '13 at 23:17
Never use == to test whether Strings are the same, unless you enjoy tracking down subtle errors and studying the intricacies of the Java String interning process."12"=="1"+2
is false (probably)
– Flight Odyssey
Dec 23 '13 at 6:04
comments disabled on deleted / locked posts / reviews |
12
Also its good to know that, if you are overridding .equals () method, make sure you are overridding .hashcode () method, otherwise you will end up with violating equivalence relation b/w equals and hashcode. For more info refer java doc.
– Nageswaran
Nov 6 '13 at 12:44
Leaving a link to my explanation on why==
works the way it does on Objects: stackoverflow.com/a/19966154/2284641
– Johannes H.
Nov 14 '13 at 2:00
==
will work some of the time, as java has a String pool, where it tries to reuse memory references of commonly used strings. But==
compares that objects are equal, not the values... so.equals()
is the proper use you want to use.
– James Oravec
Nov 14 '13 at 23:17
Never use == to test whether Strings are the same, unless you enjoy tracking down subtle errors and studying the intricacies of the Java String interning process."12"=="1"+2
is false (probably)
– Flight Odyssey
Dec 23 '13 at 6:04
12
12
Also its good to know that, if you are overridding .equals () method, make sure you are overridding .hashcode () method, otherwise you will end up with violating equivalence relation b/w equals and hashcode. For more info refer java doc.
– Nageswaran
Nov 6 '13 at 12:44
Also its good to know that, if you are overridding .equals () method, make sure you are overridding .hashcode () method, otherwise you will end up with violating equivalence relation b/w equals and hashcode. For more info refer java doc.
– Nageswaran
Nov 6 '13 at 12:44
Leaving a link to my explanation on why
==
works the way it does on Objects: stackoverflow.com/a/19966154/2284641– Johannes H.
Nov 14 '13 at 2:00
Leaving a link to my explanation on why
==
works the way it does on Objects: stackoverflow.com/a/19966154/2284641– Johannes H.
Nov 14 '13 at 2:00
==
will work some of the time, as java has a String pool, where it tries to reuse memory references of commonly used strings. But ==
compares that objects are equal, not the values... so .equals()
is the proper use you want to use.– James Oravec
Nov 14 '13 at 23:17
==
will work some of the time, as java has a String pool, where it tries to reuse memory references of commonly used strings. But ==
compares that objects are equal, not the values... so .equals()
is the proper use you want to use.– James Oravec
Nov 14 '13 at 23:17
Never use == to test whether Strings are the same, unless you enjoy tracking down subtle errors and studying the intricacies of the Java String interning process.
"12"=="1"+2
is false (probably)– Flight Odyssey
Dec 23 '13 at 6:04
Never use == to test whether Strings are the same, unless you enjoy tracking down subtle errors and studying the intricacies of the Java String interning process.
"12"=="1"+2
is false (probably)– Flight Odyssey
Dec 23 '13 at 6:04
comments disabled on deleted / locked posts / reviews |
23 Answers
23
active
oldest
votes
up vote
5138
down vote
accepted
==
tests for reference equality (whether they are the same object).
.equals()
tests for value equality (whether they are logically "equal").
Objects.equals() checks for null
before calling .equals()
so you don't have to (available as of JDK7, also available in Guava).
String.contentEquals() compares the content of the String
with the content of any CharSequence
(available since Java 1.5).
Consequently, if you want to test whether two strings have the same value you will probably want to use Objects.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
// ... but these are because literals are interned by
// the compiler and thus refer to the same object
"test" == "test" // --> true
// ... string literals are concatenated by the compiler
// and the results are interned.
"test" == "te" + "st" // --> true
// ... but you should really just call Objects.equals()
Objects.equals("test", new String("test")) // --> true
Objects.equals(null, "test") // --> false
Objects.equals(null, null) // --> true
You almost always want to use Objects.equals()
. In the rare situation where you know you're dealing with interned strings, you can use ==
.
From JLS 3.10.5. String Literals:
Moreover, a string literal always refers to the same instance of class
String
. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the methodString.intern
.
Similar examples can also be found in JLS 3.10.5-1.
336
I guess in Java you should say "references" instead of "pointers".
– Henrik Paul
Feb 5 '09 at 11:03
32
@Xokas11: compareTo is generally used for sorting.
– Michael Myers♦
Feb 27 '09 at 15:17
127
Just a noteequals()
exactly Compares this String to another String, BUTequalsIgnoreCase()
Compares this String to another String, ignoring case considerations
– Yajli Maclo
Feb 28 '13 at 15:30
40
@SnakeDoc — We can say the opposite. Always useequals()
unless you have a good reason. What you call safety is entirely context-dependent. For instance, when comparing passwords to determine whether the user has entered the right password, it is unsafe to ignore case differences.
– Nicolas Barbulesco
Aug 22 '13 at 9:06
35
One minor edit, == is not "much cheaper" than .equals, because the first statement in String.equals reads: if (this == anObject) { return true; }.
– Torque
Apr 10 '14 at 10:42
|
show 46 more comments
up vote
651
down vote
==
tests object references, .equals()
tests the string values.
Sometimes it looks as if ==
compares values, because Java does some behind-the-scenes stuff to make sure identical in-line strings are actually the same object.
For example:
String fooString1 = new String("foo");
String fooString2 = new String("foo");
// Evaluates to false
fooString1 == fooString2;
// Evaluates to true
fooString1.equals(fooString2);
// Evaluates to true, because Java uses the same object
"bar" == "bar";
But beware of nulls!
==
handles null
strings fine, but calling .equals()
from a null string will cause an exception:
String nullString1 = null;
String nullString2 = null;
// Evaluates to true
System.out.print(nullString1 == nullString2);
// Throws a NullPointerException
System.out.print(nullString1.equals(nullString2));
So if you know that fooString1
may be null, tell the reader that by writing
System.out.print(fooString1 != null && fooString1.equals("bar"));
The following is shorter, but it’s less obvious that it checks for null (from Java 7):
System.out.print(Objects.equals(fooString1, "bar"));
81
Sometimes it looks as if "==" compares values, --==
do always compare values! (It's just that certain values are references!)
– aioobe
Jul 24 '12 at 12:44
4
Alas, there is no static method for isNullOrEmpty(), and no custom overloading of operators, which makes this part of Java clunkier than in C# or Python. And since Java doesn't have extension methods, you can't write your own utility to extend java.lang.String. Right? Any thoughts on subclassing String, adding that static utility method, and then always using MyString instead? A static method with two parameters for doing null-safe comparisons would be nice to have in that subclass too.
– Jon Coombs
Jun 20 '14 at 1:00
7
Groovy makes this a little easier with the safe navigation operator (groovy.codehaus.org/…),?.
. That would convertnullString1?.equals(nullString2);
into an entirely null statement. However, it doesn't help if you havevalidString?.equals(nullString);
-- that still throws an exception.
– Charles Wood
Jun 25 '14 at 16:28
5
Short methods to compare nullable strings in java: stackoverflow.com/questions/11271554/…
– Vadzim
Mar 12 '15 at 17:19
5
@JonCoombs Java supports the subclassing and creating own method. However few classes are marked final due to certain reasons, String is one of them so we cannot extend. We can create other class and make utility class there which take two string as arguments and implement our logic there. Also for null check some other libraries like spring and apache he good collections of methods, one can use that.
– Panther
Apr 12 '15 at 5:05
|
show 5 more comments
up vote
408
down vote
==
compares Object references.
.equals()
compares String values.
Sometimes ==
gives illusions of comparing String values, as in following cases:
String a="Test";
String b="Test";
if(a==b) ===> true
This is because when you create any String literal, the JVM first searches for that literal in the String pool, and if it finds a match, that same reference will be given to the new String. Because of this, we get:
(a==b) ===> true
String Pool
b -----------------> "test" <-----------------a
However, ==
fails in the following case:
String a="test";
String b=new String("test");
if (a==b) ===> false
In this case for new String("test")
the statement new String will be created on the heap, and that reference will be given to b
, so b
will be given a reference on the heap, not in String pool.
Now a
is pointing to a String in the String pool while b
is pointing to a String on the heap. Because of that we get:
if(a==b) ===> false.
String Pool
"test" <-------------------- a
Heap
"test" <-------------------- b
While .equals()
always compares a value of String so it gives true in both cases:
String a="Test";
String b="Test";
if(a.equals(b)) ===> true
String a="test";
String b=new String("test");
if(a.equals(b)) ===> true
So using .equals()
is always better.
3
.equals() compares the two instances however equals is implemented to compare them. That might or might not be comparing the output of toString.
– Jacob
Mar 18 '16 at 2:14
3
@Jacob Object class.equals()
method compares the instances(references/Address) where as String class.equals()
methods is overridden to compare content(chars)
– Satyadev
May 7 '16 at 10:54
1
Good pointing out String pool versus Java heap differences as they are certainly not the same. In the string pool Java tries to "cache"String
objects to save memory footprint asString
is known for being immutable (I hope, I say it correctly here). Also check stackoverflow.com/questions/3052442/…
– Roland
Nov 10 '17 at 13:34
add a comment |
up vote
210
down vote
The ==
operator checks to see if the two strings are exactly the same object.
The .equals()
method will check if the two strings have the same value.
5
Generally I strongly recommend apache commons library: commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/…, java.lang.String)
– Marcin Erbel
Oct 16 '14 at 10:32
You can also use .compareTo() method if you want to evaluate order of strings.
– MarekM
Jun 27 '17 at 14:48
add a comment |
up vote
159
down vote
Strings in Java are immutable. That means whenever you try to change/modify the string you get a new instance. You cannot change the original string. This has been done so that these string instances can be cached. A typical program contains a lot of string references and caching these instances can decrease the memory footprint and increase the performance of the program.
When using == operator for string comparison you are not comparing the contents of the string, but are actually comparing the memory address. If they are both equal it will return true and false otherwise. Whereas equals in string compares the string contents.
So the question is if all the strings are cached in the system, how come ==
returns false whereas equals return true? Well, this is possible. If you make a new string like String str = new String("Testing")
you end up creating a new string in the cache even if the cache already contains a string having the same content. In short "MyString" == new String("MyString")
will always return false.
Java also talks about the function intern() that can be used on a string to make it part of the cache so "MyString" == new String("MyString").intern()
will return true.
Note: == operator is much faster than equals just because you are comparing two memory addresses, but you need to be sure that the code isn't creating new String instances in the code. Otherwise you will encounter bugs.
add a comment |
up vote
137
down vote
String a = new String("foo");
String b = new String("foo");
System.out.println(a == b); // prints false
System.out.println(a.equals(b)); // prints true
Make sure you understand why. It's because the ==
comparison only compares references; the equals()
method does a character-by-character comparison of the contents.
When you call new for a
and b
, each one gets a new reference that points to the "foo"
in the string table. The references are different, but the content is the same.
add a comment |
up vote
120
down vote
Yea, it's bad...
==
means that your two string references are exactly the same object. You may have heard that this is the case because Java keeps sort of a literal table (which it does), but that is not always the case. Some strings are loaded in different ways, constructed from other strings, etc., so you must never assume that two identical strings are stored in the same location.
Equals does the real comparison for you.
add a comment |
up vote
115
down vote
Yes, ==
is bad for comparing Strings (any objects really, unless you know they're canonical). ==
just compares object references. .equals()
tests for equality. For Strings, often they'll be the same but as you've discovered, that's not guaranteed always.
add a comment |
up vote
108
down vote
Java have a String pool under which Java manages the memory allocation for the String objects. See String Pools in Java
When you check (compare) two objects using the ==
operator it compares the address equality into the string-pool. If the two String objects have the same address references then it returns true
, otherwise false
. But if you want to compare the contents of two String objects then you must override the equals
method.
equals
is actually the method of the Object class, but it is Overridden into the String class and a new definition is given which compares the contents of object.
Example:
stringObjectOne.equals(stringObjectTwo);
But mind it respects the case of String. If you want case insensitive compare then you must go for the equalsIgnoreCase method of the String class.
Let's See:
String one = "HELLO";
String two = "HELLO";
String three = new String("HELLO");
String four = "hello";
one == two; // TRUE
one == three; // FALSE
one == four; // FALSE
one.equals(two); // TRUE
one.equals(three); // TRUE
one.equals(four); // FALSE
one.equalsIgnoreCase(four); // TRUE
6
I see that this is a late answer to big question. May I ask what it provides that isn't already mentioned in the existing answers?
– Mysticial
Apr 2 '13 at 9:19
6
@Mysticial he has addedequalsIgnoreCase
which might be informative for the fresher.
– AmitG
Apr 4 '13 at 8:48
add a comment |
up vote
96
down vote
==
compares object references in Java, and that is no exception for String
objects.
For comparing the actual contents of objects (including String
), one must use the equals
method.
If a comparison of two String
objects using ==
turns out to be true
, that is because the String
objects were interned, and the Java Virtual Machine is having multiple references point to the same instance of String
. One should not expect that comparing one String
object containing the same contents as another String
object using ==
to evaluate as true
.
add a comment |
up vote
95
down vote
.equals()
compares the data in a class (assuming the function is implemented).
==
compares pointer locations (location of the object in memory).
==
returns true if both objects (NOT TALKING ABOUT PRIMITIVES) point to the SAME object instance.
.equals()
returns true if the two objects contain the same data equals()
Versus ==
in Java
That may help you.
add a comment |
up vote
92
down vote
I agree with the answer from zacherates.
But what you can do is to call intern()
on your non-literal strings.
From zacherates example:
// ... but they are not the same object
new String("test") == "test" ==> false
If you intern the non-literal String equality is true
new String("test").intern() == "test" ==> true
5
This is generally not a good idea. Interning is relatively expensive and can (paradoxically) >>increase<< your JVMs memory footprint and increase GC costs. In most cases, these outweigh the performance benefits from using==
for string comparison.
– Stephen C
Sep 25 '15 at 23:16
add a comment |
up vote
90
down vote
==
performs a reference equality check, whether the 2 objects (strings in this case) refer to the same object in the memory.
The equals()
method will check whether the contents or the states of 2 objects are the same.
Obviously ==
is faster, but will (might) give false results in many cases if you just want to tell if 2 String
s hold the same text.
Definitely the use of equals()
method is recommended.
Don't worry about the performance. Some things to encourage using String.equals()
:
- Implementation of
String.equals()
first checks for reference equality (using==
), and if the 2 strings are the same by reference, no further calculation is performed! - If the 2 string references are not the same,
String.equals()
will next check the lengths of the strings. This is also a fast operation because theString
class stores the length of the string, no need to count the characters or code points. If the lengths differ, no further check is performed, we know they cannot be equal. - Only if we got this far will the contents of the 2 strings be actually compared, and this will be a short-hand comparison: not all the characters will be compared, if we find a mismatching character (at the same position in the 2 strings), no further characters will be checked.
When all is said and done, even if we have guarantee that the strings are interns, using the equals()
method is still not that overhead that one might think, definitely the recommended way. If you want efficient reference check, then use enums where it is guaranteed by the language specification and implementation that the same enum value will be the same object (by reference).
1
Obviously == is faster
-- actually the implementation of.equals(String)
first checks==
before anything else so I would say the speed is about identical.
– Razzle Shazl
Jun 22 at 6:38
1
public boolean equals(Object anObject) { if (this == anObject) { return true; } ...
– Razzle Shazl
Jun 22 at 6:51
add a comment |
up vote
79
down vote
If you're like me, when I first started using Java, I wanted to use the "==" operator to test whether two String instances were equal, but for better or worse, that's not the correct way to do it in Java.
In this tutorial I'll demonstrate several different ways to correctly compare Java strings, starting with the approach I use most of the time. At the end of this Java String comparison tutorial I'll also discuss why the "==" operator doesn't work when comparing Java strings.
Option 1: Java String comparison with the equals method
Most of the time (maybe 95% of the time) I compare strings with the equals method of the Java String class, like this:
if (string1.equals(string2))
This String equals method looks at the two Java strings, and if they contain the exact same string of characters, they are considered equal.
Taking a look at a quick String comparison example with the equals method, if the following test were run, the two strings would not be considered equal because the characters are not the exactly the same (the case of the characters is different):
String string1 = "foo";
String string2 = "FOO";
if (string1.equals(string2))
{
// this line will not print because the
// java string equals method returns false:
System.out.println("The two strings are the same.")
}
But, when the two strings contain the exact same string of characters, the equals method will return true, as in this example:
String string1 = "foo";
String string2 = "foo";
// test for equality with the java string equals method
if (string1.equals(string2))
{
// this line WILL print
System.out.println("The two strings are the same.")
}
Option 2: String comparison with the equalsIgnoreCase method
In some string comparison tests you'll want to ignore whether the strings are uppercase or lowercase. When you want to test your strings for equality in this case-insensitive manner, use the equalsIgnoreCase method of the String class, like this:
String string1 = "foo";
String string2 = "FOO";
// java string compare while ignoring case
if (string1.equalsIgnoreCase(string2))
{
// this line WILL print
System.out.println("Ignoring case, the two strings are the same.")
}
Option 3: Java String comparison with the compareTo method
There is also a third, less common way to compare Java strings, and that's with the String class compareTo method. If the two strings are exactly the same, the compareTo method will return a value of 0 (zero). Here's a quick example of what this String comparison approach looks like:
String string1 = "foo bar";
String string2 = "foo bar";
// java string compare example
if (string1.compareTo(string2) == 0)
{
// this line WILL print
System.out.println("The two strings are the same.")
}
While I'm writing about this concept of equality in Java, it's important to note that the Java language includes an equals method in the base Java Object class. Whenever you're creating your own objects and you want to provide a means to see if two instances of your object are "equal", you should override (and implement) this equals method in your class (in the same way the Java language provides this equality/comparison behavior in the String equals method).
You may want to have a look at this ==, .equals(), compareTo(), and compare()
5
for string literals Like String string1 = "foo bar"; String string2 = "foo bar"; you can directly use == operator to test content equality
– JAVA
Sep 7 '13 at 18:11
1
In google apps script "compareTo" is not possibe. I tried instaed "equals" This was the only solution that works....
– user3887038
Apr 24 at 12:40
add a comment |
up vote
74
down vote
Function:
public float simpleSimilarity(String u, String v) {
String a = u.split(" ");
String b = v.split(" ");
long correct = 0;
int minLen = Math.min(a.length, b.length);
for (int i = 0; i < minLen; i++) {
String aa = a[i];
String bb = b[i];
int minWordLength = Math.min(aa.length(), bb.length());
for (int j = 0; j < minWordLength; j++) {
if (aa.charAt(j) == bb.charAt(j)) {
correct++;
}
}
}
return (float) (((double) correct) / Math.max(u.length(), v.length()));
}
Test:
String a = "This is the first string.";
String b = "this is not 1st string!";
// for exact string comparison, use .equals
boolean exact = a.equals(b);
// For similarity check, there are libraries for this
// Here I'll try a simple example I wrote
float similarity = simple_similarity(a,b);
5
How does this differ from other answers? and why do it the way you suggest
– Mark
Sep 10 '15 at 12:27
2
@Mark The question on difference between==
andequals
was already answered by other solutions, I just offered a different way to compare strings in a loose way
– Khaled.K
Sep 10 '15 at 20:45
add a comment |
up vote
69
down vote
The ==
operator check if the two references point to the same object or not. .equals()
check for the actual string content (value).
Note that the .equals()
method belongs to class Object
(super class of all classes). You need to override it as per you class requirement, but for String it is already implemented, and it checks whether two strings have the same value or not.
Case 1
String s1 = "Stack Overflow";
String s2 = "Stack Overflow";
s1 == s2; //true
s1.equals(s2); //true
Reason: String literals created without null are stored in the String pool in the permgen area of heap. So both s1 and s2 point to same object in the pool.
Case 2
String s1 = new String("Stack Overflow");
String s2 = new String("Stack Overflow");
s1 == s2; //false
s1.equals(s2); //true
Reason: If you create a String object using the
new
keyword a separate space is allocated to it on the heap.
add a comment |
up vote
50
down vote
==
compares the reference value of objects whereas the equals()
method present in the java.lang.String
class compares the contents of the String
object (to another object).
14
not to be nit picky, but theequals()
method forString
is actually in theString
class, not inObject
. The defaultequals()
inObject
would not compare that the contents are the same, and in fact just returns true when the reference is the same.
– Jacob Schoen
Nov 20 '12 at 17:04
add a comment |
up vote
48
down vote
I think that when you define a String
you define an object. So you need to use .equals()
. When you use primitive data types you use ==
but with String
(and any object) you must use .equals()
.
1
Also note that == doesn't work for char
– Khaled.K
Mar 24 '13 at 14:03
7
"char" isn't a primitive data type! It's an array of "char". And arrays aren't primitive data types theirselves.
– Christian
Mar 18 '15 at 21:02
add a comment |
up vote
44
down vote
If the equals()
method is present in the java.lang.Object
class, and it is expected to check for the equivalence of the state of objects! That means, the contents of the objects. Whereas the ==
operator is expected to check the actual object instances are same or not.
Example
Consider two different reference variables, str1
and str2
:
str1 = new String("abc");
str2 = new String("abc");
If you use the equals()
System.out.println((str1.equals(str2))?"TRUE":"FALSE");
You will get the output as TRUE
if you use ==
.
System.out.println((str1==str2) ? "TRUE" : "FALSE");
Now you will get the FALSE
as output, because both str1
and str2
are pointing to two different objects even though both of them share the same string content. It is because of new String()
a new object is created every time.
add a comment |
up vote
41
down vote
Operator == is always meant for object reference comparison, whereas the String class .equals() method is overridden for content comparison:
String s1 = new String("abc");
String s2 = new String("abc");
System.out.println(s1 == s2); // It prints false (reference comparison)
System.out.println(s1.equals(s2)); // It prints true (content comparison)
add a comment |
up vote
37
down vote
All objects are guaranteed to have a .equals()
method since Object contains a method, .equals()
, that returns a boolean. It is the subclass' job to override this method if a further defining definition is required. Without it (i.e. using ==
) only memory addresses are checked between two objects for equality. String overrides this .equals()
method and instead of using the memory address it returns the comparison of strings at the character level for equality.
A key note is that strings are stored in one lump pool so once a string is created it is forever stored in a program at the same address. Strings do not change, they are immutable. This is why it is a bad idea to use regular string concatenation if you have a serious of amount of string processing to do. Instead you would use the StringBuilder
classes provided. Remember the pointers to this string can change and if you were interested to see if two pointers were the same ==
would be a fine way to go. Strings themselves do not.
2
"once a string is created it is forever stored in a program at the same address" - This is flat-out wrong. Only compile-time constant string expressions (possibly involvingfinal String
variables) and strings that your program explicitly interns are stored in what you call a "lump pool". All otherString
objects are subject to garbage collection once there are no more live references to them, just like any other type of object. Also, while immutability is required for the whole interning mechanism to work, it's otherwise irrelevant to this.
– Ted Hopp
Apr 10 '14 at 18:13
String comparison is done either through equals or equalsIgnoreCase method which actually compares the contents of the string. But == sign just check the reference values. For string literals from string pool will work fine for this case. String s1 = new String("a"); String s2 = new String("a"); in this case s1==s2 is false, but s1.equals(s2) is true.
– Shailendra Singh
Jul 8 '16 at 15:31
add a comment |
up vote
37
down vote
You can also use the compareTo()
method to compare two Strings. If the compareTo result is 0, then the two strings are equal, otherwise the strings being compared are not equal.
The ==
compares the references and does not compare the actual strings. If you did create every string using new String(somestring).intern()
then you can use the ==
operator to compare two strings, otherwise equals() or compareTo methods can only be used.
add a comment |
up vote
35
down vote
In Java, when the “==” operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory. In other words, it checks to see if the 2 object names are basically references to the same memory location.
The Java String class actually overrides the default equals() implementation in the Object class – and it overrides the method so that it checks only the values of the strings, not their locations in memory.
This means that if you call the equals() method to compare 2 String objects, then as long as the actual sequence of characters is equal, both objects are considered equal.
The
==
operator checks if the two strings are exactly the same object.
The
.equals()
method check if the two strings have the same value.
1
unless one of them is null, since s.equals(s2) will crash if s is null, causing the comparison to fail. Of course, this doesn't really contradict the answer; it's just a caveat.
– Jon Coombs
Jun 20 '14 at 1:05
1
No, it won't crash, it will throw a NullPointerException, causing the comparison to not take place.
– Bludzee
Mar 4 '16 at 9:51
add a comment |
protected by Community♦ Jan 31 '14 at 5:20
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
23 Answers
23
active
oldest
votes
23 Answers
23
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5138
down vote
accepted
==
tests for reference equality (whether they are the same object).
.equals()
tests for value equality (whether they are logically "equal").
Objects.equals() checks for null
before calling .equals()
so you don't have to (available as of JDK7, also available in Guava).
String.contentEquals() compares the content of the String
with the content of any CharSequence
(available since Java 1.5).
Consequently, if you want to test whether two strings have the same value you will probably want to use Objects.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
// ... but these are because literals are interned by
// the compiler and thus refer to the same object
"test" == "test" // --> true
// ... string literals are concatenated by the compiler
// and the results are interned.
"test" == "te" + "st" // --> true
// ... but you should really just call Objects.equals()
Objects.equals("test", new String("test")) // --> true
Objects.equals(null, "test") // --> false
Objects.equals(null, null) // --> true
You almost always want to use Objects.equals()
. In the rare situation where you know you're dealing with interned strings, you can use ==
.
From JLS 3.10.5. String Literals:
Moreover, a string literal always refers to the same instance of class
String
. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the methodString.intern
.
Similar examples can also be found in JLS 3.10.5-1.
336
I guess in Java you should say "references" instead of "pointers".
– Henrik Paul
Feb 5 '09 at 11:03
32
@Xokas11: compareTo is generally used for sorting.
– Michael Myers♦
Feb 27 '09 at 15:17
127
Just a noteequals()
exactly Compares this String to another String, BUTequalsIgnoreCase()
Compares this String to another String, ignoring case considerations
– Yajli Maclo
Feb 28 '13 at 15:30
40
@SnakeDoc — We can say the opposite. Always useequals()
unless you have a good reason. What you call safety is entirely context-dependent. For instance, when comparing passwords to determine whether the user has entered the right password, it is unsafe to ignore case differences.
– Nicolas Barbulesco
Aug 22 '13 at 9:06
35
One minor edit, == is not "much cheaper" than .equals, because the first statement in String.equals reads: if (this == anObject) { return true; }.
– Torque
Apr 10 '14 at 10:42
|
show 46 more comments
up vote
5138
down vote
accepted
==
tests for reference equality (whether they are the same object).
.equals()
tests for value equality (whether they are logically "equal").
Objects.equals() checks for null
before calling .equals()
so you don't have to (available as of JDK7, also available in Guava).
String.contentEquals() compares the content of the String
with the content of any CharSequence
(available since Java 1.5).
Consequently, if you want to test whether two strings have the same value you will probably want to use Objects.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
// ... but these are because literals are interned by
// the compiler and thus refer to the same object
"test" == "test" // --> true
// ... string literals are concatenated by the compiler
// and the results are interned.
"test" == "te" + "st" // --> true
// ... but you should really just call Objects.equals()
Objects.equals("test", new String("test")) // --> true
Objects.equals(null, "test") // --> false
Objects.equals(null, null) // --> true
You almost always want to use Objects.equals()
. In the rare situation where you know you're dealing with interned strings, you can use ==
.
From JLS 3.10.5. String Literals:
Moreover, a string literal always refers to the same instance of class
String
. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the methodString.intern
.
Similar examples can also be found in JLS 3.10.5-1.
336
I guess in Java you should say "references" instead of "pointers".
– Henrik Paul
Feb 5 '09 at 11:03
32
@Xokas11: compareTo is generally used for sorting.
– Michael Myers♦
Feb 27 '09 at 15:17
127
Just a noteequals()
exactly Compares this String to another String, BUTequalsIgnoreCase()
Compares this String to another String, ignoring case considerations
– Yajli Maclo
Feb 28 '13 at 15:30
40
@SnakeDoc — We can say the opposite. Always useequals()
unless you have a good reason. What you call safety is entirely context-dependent. For instance, when comparing passwords to determine whether the user has entered the right password, it is unsafe to ignore case differences.
– Nicolas Barbulesco
Aug 22 '13 at 9:06
35
One minor edit, == is not "much cheaper" than .equals, because the first statement in String.equals reads: if (this == anObject) { return true; }.
– Torque
Apr 10 '14 at 10:42
|
show 46 more comments
up vote
5138
down vote
accepted
up vote
5138
down vote
accepted
==
tests for reference equality (whether they are the same object).
.equals()
tests for value equality (whether they are logically "equal").
Objects.equals() checks for null
before calling .equals()
so you don't have to (available as of JDK7, also available in Guava).
String.contentEquals() compares the content of the String
with the content of any CharSequence
(available since Java 1.5).
Consequently, if you want to test whether two strings have the same value you will probably want to use Objects.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
// ... but these are because literals are interned by
// the compiler and thus refer to the same object
"test" == "test" // --> true
// ... string literals are concatenated by the compiler
// and the results are interned.
"test" == "te" + "st" // --> true
// ... but you should really just call Objects.equals()
Objects.equals("test", new String("test")) // --> true
Objects.equals(null, "test") // --> false
Objects.equals(null, null) // --> true
You almost always want to use Objects.equals()
. In the rare situation where you know you're dealing with interned strings, you can use ==
.
From JLS 3.10.5. String Literals:
Moreover, a string literal always refers to the same instance of class
String
. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the methodString.intern
.
Similar examples can also be found in JLS 3.10.5-1.
==
tests for reference equality (whether they are the same object).
.equals()
tests for value equality (whether they are logically "equal").
Objects.equals() checks for null
before calling .equals()
so you don't have to (available as of JDK7, also available in Guava).
String.contentEquals() compares the content of the String
with the content of any CharSequence
(available since Java 1.5).
Consequently, if you want to test whether two strings have the same value you will probably want to use Objects.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
// ... but these are because literals are interned by
// the compiler and thus refer to the same object
"test" == "test" // --> true
// ... string literals are concatenated by the compiler
// and the results are interned.
"test" == "te" + "st" // --> true
// ... but you should really just call Objects.equals()
Objects.equals("test", new String("test")) // --> true
Objects.equals(null, "test") // --> false
Objects.equals(null, null) // --> true
You almost always want to use Objects.equals()
. In the rare situation where you know you're dealing with interned strings, you can use ==
.
From JLS 3.10.5. String Literals:
Moreover, a string literal always refers to the same instance of class
String
. This is because string literals - or, more generally, strings that are the values of constant expressions (§15.28) - are "interned" so as to share unique instances, using the methodString.intern
.
Similar examples can also be found in JLS 3.10.5-1.
edited Nov 9 at 18:31
community wiki
40 revs, 24 users 42%
Aaron Maenpaa
336
I guess in Java you should say "references" instead of "pointers".
– Henrik Paul
Feb 5 '09 at 11:03
32
@Xokas11: compareTo is generally used for sorting.
– Michael Myers♦
Feb 27 '09 at 15:17
127
Just a noteequals()
exactly Compares this String to another String, BUTequalsIgnoreCase()
Compares this String to another String, ignoring case considerations
– Yajli Maclo
Feb 28 '13 at 15:30
40
@SnakeDoc — We can say the opposite. Always useequals()
unless you have a good reason. What you call safety is entirely context-dependent. For instance, when comparing passwords to determine whether the user has entered the right password, it is unsafe to ignore case differences.
– Nicolas Barbulesco
Aug 22 '13 at 9:06
35
One minor edit, == is not "much cheaper" than .equals, because the first statement in String.equals reads: if (this == anObject) { return true; }.
– Torque
Apr 10 '14 at 10:42
|
show 46 more comments
336
I guess in Java you should say "references" instead of "pointers".
– Henrik Paul
Feb 5 '09 at 11:03
32
@Xokas11: compareTo is generally used for sorting.
– Michael Myers♦
Feb 27 '09 at 15:17
127
Just a noteequals()
exactly Compares this String to another String, BUTequalsIgnoreCase()
Compares this String to another String, ignoring case considerations
– Yajli Maclo
Feb 28 '13 at 15:30
40
@SnakeDoc — We can say the opposite. Always useequals()
unless you have a good reason. What you call safety is entirely context-dependent. For instance, when comparing passwords to determine whether the user has entered the right password, it is unsafe to ignore case differences.
– Nicolas Barbulesco
Aug 22 '13 at 9:06
35
One minor edit, == is not "much cheaper" than .equals, because the first statement in String.equals reads: if (this == anObject) { return true; }.
– Torque
Apr 10 '14 at 10:42
336
336
I guess in Java you should say "references" instead of "pointers".
– Henrik Paul
Feb 5 '09 at 11:03
I guess in Java you should say "references" instead of "pointers".
– Henrik Paul
Feb 5 '09 at 11:03
32
32
@Xokas11: compareTo is generally used for sorting.
– Michael Myers♦
Feb 27 '09 at 15:17
@Xokas11: compareTo is generally used for sorting.
– Michael Myers♦
Feb 27 '09 at 15:17
127
127
Just a note
equals()
exactly Compares this String to another String, BUT equalsIgnoreCase()
Compares this String to another String, ignoring case considerations– Yajli Maclo
Feb 28 '13 at 15:30
Just a note
equals()
exactly Compares this String to another String, BUT equalsIgnoreCase()
Compares this String to another String, ignoring case considerations– Yajli Maclo
Feb 28 '13 at 15:30
40
40
@SnakeDoc — We can say the opposite. Always use
equals()
unless you have a good reason. What you call safety is entirely context-dependent. For instance, when comparing passwords to determine whether the user has entered the right password, it is unsafe to ignore case differences.– Nicolas Barbulesco
Aug 22 '13 at 9:06
@SnakeDoc — We can say the opposite. Always use
equals()
unless you have a good reason. What you call safety is entirely context-dependent. For instance, when comparing passwords to determine whether the user has entered the right password, it is unsafe to ignore case differences.– Nicolas Barbulesco
Aug 22 '13 at 9:06
35
35
One minor edit, == is not "much cheaper" than .equals, because the first statement in String.equals reads: if (this == anObject) { return true; }.
– Torque
Apr 10 '14 at 10:42
One minor edit, == is not "much cheaper" than .equals, because the first statement in String.equals reads: if (this == anObject) { return true; }.
– Torque
Apr 10 '14 at 10:42
|
show 46 more comments
up vote
651
down vote
==
tests object references, .equals()
tests the string values.
Sometimes it looks as if ==
compares values, because Java does some behind-the-scenes stuff to make sure identical in-line strings are actually the same object.
For example:
String fooString1 = new String("foo");
String fooString2 = new String("foo");
// Evaluates to false
fooString1 == fooString2;
// Evaluates to true
fooString1.equals(fooString2);
// Evaluates to true, because Java uses the same object
"bar" == "bar";
But beware of nulls!
==
handles null
strings fine, but calling .equals()
from a null string will cause an exception:
String nullString1 = null;
String nullString2 = null;
// Evaluates to true
System.out.print(nullString1 == nullString2);
// Throws a NullPointerException
System.out.print(nullString1.equals(nullString2));
So if you know that fooString1
may be null, tell the reader that by writing
System.out.print(fooString1 != null && fooString1.equals("bar"));
The following is shorter, but it’s less obvious that it checks for null (from Java 7):
System.out.print(Objects.equals(fooString1, "bar"));
81
Sometimes it looks as if "==" compares values, --==
do always compare values! (It's just that certain values are references!)
– aioobe
Jul 24 '12 at 12:44
4
Alas, there is no static method for isNullOrEmpty(), and no custom overloading of operators, which makes this part of Java clunkier than in C# or Python. And since Java doesn't have extension methods, you can't write your own utility to extend java.lang.String. Right? Any thoughts on subclassing String, adding that static utility method, and then always using MyString instead? A static method with two parameters for doing null-safe comparisons would be nice to have in that subclass too.
– Jon Coombs
Jun 20 '14 at 1:00
7
Groovy makes this a little easier with the safe navigation operator (groovy.codehaus.org/…),?.
. That would convertnullString1?.equals(nullString2);
into an entirely null statement. However, it doesn't help if you havevalidString?.equals(nullString);
-- that still throws an exception.
– Charles Wood
Jun 25 '14 at 16:28
5
Short methods to compare nullable strings in java: stackoverflow.com/questions/11271554/…
– Vadzim
Mar 12 '15 at 17:19
5
@JonCoombs Java supports the subclassing and creating own method. However few classes are marked final due to certain reasons, String is one of them so we cannot extend. We can create other class and make utility class there which take two string as arguments and implement our logic there. Also for null check some other libraries like spring and apache he good collections of methods, one can use that.
– Panther
Apr 12 '15 at 5:05
|
show 5 more comments
up vote
651
down vote
==
tests object references, .equals()
tests the string values.
Sometimes it looks as if ==
compares values, because Java does some behind-the-scenes stuff to make sure identical in-line strings are actually the same object.
For example:
String fooString1 = new String("foo");
String fooString2 = new String("foo");
// Evaluates to false
fooString1 == fooString2;
// Evaluates to true
fooString1.equals(fooString2);
// Evaluates to true, because Java uses the same object
"bar" == "bar";
But beware of nulls!
==
handles null
strings fine, but calling .equals()
from a null string will cause an exception:
String nullString1 = null;
String nullString2 = null;
// Evaluates to true
System.out.print(nullString1 == nullString2);
// Throws a NullPointerException
System.out.print(nullString1.equals(nullString2));
So if you know that fooString1
may be null, tell the reader that by writing
System.out.print(fooString1 != null && fooString1.equals("bar"));
The following is shorter, but it’s less obvious that it checks for null (from Java 7):
System.out.print(Objects.equals(fooString1, "bar"));
81
Sometimes it looks as if "==" compares values, --==
do always compare values! (It's just that certain values are references!)
– aioobe
Jul 24 '12 at 12:44
4
Alas, there is no static method for isNullOrEmpty(), and no custom overloading of operators, which makes this part of Java clunkier than in C# or Python. And since Java doesn't have extension methods, you can't write your own utility to extend java.lang.String. Right? Any thoughts on subclassing String, adding that static utility method, and then always using MyString instead? A static method with two parameters for doing null-safe comparisons would be nice to have in that subclass too.
– Jon Coombs
Jun 20 '14 at 1:00
7
Groovy makes this a little easier with the safe navigation operator (groovy.codehaus.org/…),?.
. That would convertnullString1?.equals(nullString2);
into an entirely null statement. However, it doesn't help if you havevalidString?.equals(nullString);
-- that still throws an exception.
– Charles Wood
Jun 25 '14 at 16:28
5
Short methods to compare nullable strings in java: stackoverflow.com/questions/11271554/…
– Vadzim
Mar 12 '15 at 17:19
5
@JonCoombs Java supports the subclassing and creating own method. However few classes are marked final due to certain reasons, String is one of them so we cannot extend. We can create other class and make utility class there which take two string as arguments and implement our logic there. Also for null check some other libraries like spring and apache he good collections of methods, one can use that.
– Panther
Apr 12 '15 at 5:05
|
show 5 more comments
up vote
651
down vote
up vote
651
down vote
==
tests object references, .equals()
tests the string values.
Sometimes it looks as if ==
compares values, because Java does some behind-the-scenes stuff to make sure identical in-line strings are actually the same object.
For example:
String fooString1 = new String("foo");
String fooString2 = new String("foo");
// Evaluates to false
fooString1 == fooString2;
// Evaluates to true
fooString1.equals(fooString2);
// Evaluates to true, because Java uses the same object
"bar" == "bar";
But beware of nulls!
==
handles null
strings fine, but calling .equals()
from a null string will cause an exception:
String nullString1 = null;
String nullString2 = null;
// Evaluates to true
System.out.print(nullString1 == nullString2);
// Throws a NullPointerException
System.out.print(nullString1.equals(nullString2));
So if you know that fooString1
may be null, tell the reader that by writing
System.out.print(fooString1 != null && fooString1.equals("bar"));
The following is shorter, but it’s less obvious that it checks for null (from Java 7):
System.out.print(Objects.equals(fooString1, "bar"));
==
tests object references, .equals()
tests the string values.
Sometimes it looks as if ==
compares values, because Java does some behind-the-scenes stuff to make sure identical in-line strings are actually the same object.
For example:
String fooString1 = new String("foo");
String fooString2 = new String("foo");
// Evaluates to false
fooString1 == fooString2;
// Evaluates to true
fooString1.equals(fooString2);
// Evaluates to true, because Java uses the same object
"bar" == "bar";
But beware of nulls!
==
handles null
strings fine, but calling .equals()
from a null string will cause an exception:
String nullString1 = null;
String nullString2 = null;
// Evaluates to true
System.out.print(nullString1 == nullString2);
// Throws a NullPointerException
System.out.print(nullString1.equals(nullString2));
So if you know that fooString1
may be null, tell the reader that by writing
System.out.print(fooString1 != null && fooString1.equals("bar"));
The following is shorter, but it’s less obvious that it checks for null (from Java 7):
System.out.print(Objects.equals(fooString1, "bar"));
edited May 24 at 15:48
community wiki
6 revs, 6 users 76%
Whatsit
81
Sometimes it looks as if "==" compares values, --==
do always compare values! (It's just that certain values are references!)
– aioobe
Jul 24 '12 at 12:44
4
Alas, there is no static method for isNullOrEmpty(), and no custom overloading of operators, which makes this part of Java clunkier than in C# or Python. And since Java doesn't have extension methods, you can't write your own utility to extend java.lang.String. Right? Any thoughts on subclassing String, adding that static utility method, and then always using MyString instead? A static method with two parameters for doing null-safe comparisons would be nice to have in that subclass too.
– Jon Coombs
Jun 20 '14 at 1:00
7
Groovy makes this a little easier with the safe navigation operator (groovy.codehaus.org/…),?.
. That would convertnullString1?.equals(nullString2);
into an entirely null statement. However, it doesn't help if you havevalidString?.equals(nullString);
-- that still throws an exception.
– Charles Wood
Jun 25 '14 at 16:28
5
Short methods to compare nullable strings in java: stackoverflow.com/questions/11271554/…
– Vadzim
Mar 12 '15 at 17:19
5
@JonCoombs Java supports the subclassing and creating own method. However few classes are marked final due to certain reasons, String is one of them so we cannot extend. We can create other class and make utility class there which take two string as arguments and implement our logic there. Also for null check some other libraries like spring and apache he good collections of methods, one can use that.
– Panther
Apr 12 '15 at 5:05
|
show 5 more comments
81
Sometimes it looks as if "==" compares values, --==
do always compare values! (It's just that certain values are references!)
– aioobe
Jul 24 '12 at 12:44
4
Alas, there is no static method for isNullOrEmpty(), and no custom overloading of operators, which makes this part of Java clunkier than in C# or Python. And since Java doesn't have extension methods, you can't write your own utility to extend java.lang.String. Right? Any thoughts on subclassing String, adding that static utility method, and then always using MyString instead? A static method with two parameters for doing null-safe comparisons would be nice to have in that subclass too.
– Jon Coombs
Jun 20 '14 at 1:00
7
Groovy makes this a little easier with the safe navigation operator (groovy.codehaus.org/…),?.
. That would convertnullString1?.equals(nullString2);
into an entirely null statement. However, it doesn't help if you havevalidString?.equals(nullString);
-- that still throws an exception.
– Charles Wood
Jun 25 '14 at 16:28
5
Short methods to compare nullable strings in java: stackoverflow.com/questions/11271554/…
– Vadzim
Mar 12 '15 at 17:19
5
@JonCoombs Java supports the subclassing and creating own method. However few classes are marked final due to certain reasons, String is one of them so we cannot extend. We can create other class and make utility class there which take two string as arguments and implement our logic there. Also for null check some other libraries like spring and apache he good collections of methods, one can use that.
– Panther
Apr 12 '15 at 5:05
81
81
Sometimes it looks as if "==" compares values, --
==
do always compare values! (It's just that certain values are references!)– aioobe
Jul 24 '12 at 12:44
Sometimes it looks as if "==" compares values, --
==
do always compare values! (It's just that certain values are references!)– aioobe
Jul 24 '12 at 12:44
4
4
Alas, there is no static method for isNullOrEmpty(), and no custom overloading of operators, which makes this part of Java clunkier than in C# or Python. And since Java doesn't have extension methods, you can't write your own utility to extend java.lang.String. Right? Any thoughts on subclassing String, adding that static utility method, and then always using MyString instead? A static method with two parameters for doing null-safe comparisons would be nice to have in that subclass too.
– Jon Coombs
Jun 20 '14 at 1:00
Alas, there is no static method for isNullOrEmpty(), and no custom overloading of operators, which makes this part of Java clunkier than in C# or Python. And since Java doesn't have extension methods, you can't write your own utility to extend java.lang.String. Right? Any thoughts on subclassing String, adding that static utility method, and then always using MyString instead? A static method with two parameters for doing null-safe comparisons would be nice to have in that subclass too.
– Jon Coombs
Jun 20 '14 at 1:00
7
7
Groovy makes this a little easier with the safe navigation operator (groovy.codehaus.org/…),
?.
. That would convert nullString1?.equals(nullString2);
into an entirely null statement. However, it doesn't help if you have validString?.equals(nullString);
-- that still throws an exception.– Charles Wood
Jun 25 '14 at 16:28
Groovy makes this a little easier with the safe navigation operator (groovy.codehaus.org/…),
?.
. That would convert nullString1?.equals(nullString2);
into an entirely null statement. However, it doesn't help if you have validString?.equals(nullString);
-- that still throws an exception.– Charles Wood
Jun 25 '14 at 16:28
5
5
Short methods to compare nullable strings in java: stackoverflow.com/questions/11271554/…
– Vadzim
Mar 12 '15 at 17:19
Short methods to compare nullable strings in java: stackoverflow.com/questions/11271554/…
– Vadzim
Mar 12 '15 at 17:19
5
5
@JonCoombs Java supports the subclassing and creating own method. However few classes are marked final due to certain reasons, String is one of them so we cannot extend. We can create other class and make utility class there which take two string as arguments and implement our logic there. Also for null check some other libraries like spring and apache he good collections of methods, one can use that.
– Panther
Apr 12 '15 at 5:05
@JonCoombs Java supports the subclassing and creating own method. However few classes are marked final due to certain reasons, String is one of them so we cannot extend. We can create other class and make utility class there which take two string as arguments and implement our logic there. Also for null check some other libraries like spring and apache he good collections of methods, one can use that.
– Panther
Apr 12 '15 at 5:05
|
show 5 more comments
up vote
408
down vote
==
compares Object references.
.equals()
compares String values.
Sometimes ==
gives illusions of comparing String values, as in following cases:
String a="Test";
String b="Test";
if(a==b) ===> true
This is because when you create any String literal, the JVM first searches for that literal in the String pool, and if it finds a match, that same reference will be given to the new String. Because of this, we get:
(a==b) ===> true
String Pool
b -----------------> "test" <-----------------a
However, ==
fails in the following case:
String a="test";
String b=new String("test");
if (a==b) ===> false
In this case for new String("test")
the statement new String will be created on the heap, and that reference will be given to b
, so b
will be given a reference on the heap, not in String pool.
Now a
is pointing to a String in the String pool while b
is pointing to a String on the heap. Because of that we get:
if(a==b) ===> false.
String Pool
"test" <-------------------- a
Heap
"test" <-------------------- b
While .equals()
always compares a value of String so it gives true in both cases:
String a="Test";
String b="Test";
if(a.equals(b)) ===> true
String a="test";
String b=new String("test");
if(a.equals(b)) ===> true
So using .equals()
is always better.
3
.equals() compares the two instances however equals is implemented to compare them. That might or might not be comparing the output of toString.
– Jacob
Mar 18 '16 at 2:14
3
@Jacob Object class.equals()
method compares the instances(references/Address) where as String class.equals()
methods is overridden to compare content(chars)
– Satyadev
May 7 '16 at 10:54
1
Good pointing out String pool versus Java heap differences as they are certainly not the same. In the string pool Java tries to "cache"String
objects to save memory footprint asString
is known for being immutable (I hope, I say it correctly here). Also check stackoverflow.com/questions/3052442/…
– Roland
Nov 10 '17 at 13:34
add a comment |
up vote
408
down vote
==
compares Object references.
.equals()
compares String values.
Sometimes ==
gives illusions of comparing String values, as in following cases:
String a="Test";
String b="Test";
if(a==b) ===> true
This is because when you create any String literal, the JVM first searches for that literal in the String pool, and if it finds a match, that same reference will be given to the new String. Because of this, we get:
(a==b) ===> true
String Pool
b -----------------> "test" <-----------------a
However, ==
fails in the following case:
String a="test";
String b=new String("test");
if (a==b) ===> false
In this case for new String("test")
the statement new String will be created on the heap, and that reference will be given to b
, so b
will be given a reference on the heap, not in String pool.
Now a
is pointing to a String in the String pool while b
is pointing to a String on the heap. Because of that we get:
if(a==b) ===> false.
String Pool
"test" <-------------------- a
Heap
"test" <-------------------- b
While .equals()
always compares a value of String so it gives true in both cases:
String a="Test";
String b="Test";
if(a.equals(b)) ===> true
String a="test";
String b=new String("test");
if(a.equals(b)) ===> true
So using .equals()
is always better.
3
.equals() compares the two instances however equals is implemented to compare them. That might or might not be comparing the output of toString.
– Jacob
Mar 18 '16 at 2:14
3
@Jacob Object class.equals()
method compares the instances(references/Address) where as String class.equals()
methods is overridden to compare content(chars)
– Satyadev
May 7 '16 at 10:54
1
Good pointing out String pool versus Java heap differences as they are certainly not the same. In the string pool Java tries to "cache"String
objects to save memory footprint asString
is known for being immutable (I hope, I say it correctly here). Also check stackoverflow.com/questions/3052442/…
– Roland
Nov 10 '17 at 13:34
add a comment |
up vote
408
down vote
up vote
408
down vote
==
compares Object references.
.equals()
compares String values.
Sometimes ==
gives illusions of comparing String values, as in following cases:
String a="Test";
String b="Test";
if(a==b) ===> true
This is because when you create any String literal, the JVM first searches for that literal in the String pool, and if it finds a match, that same reference will be given to the new String. Because of this, we get:
(a==b) ===> true
String Pool
b -----------------> "test" <-----------------a
However, ==
fails in the following case:
String a="test";
String b=new String("test");
if (a==b) ===> false
In this case for new String("test")
the statement new String will be created on the heap, and that reference will be given to b
, so b
will be given a reference on the heap, not in String pool.
Now a
is pointing to a String in the String pool while b
is pointing to a String on the heap. Because of that we get:
if(a==b) ===> false.
String Pool
"test" <-------------------- a
Heap
"test" <-------------------- b
While .equals()
always compares a value of String so it gives true in both cases:
String a="Test";
String b="Test";
if(a.equals(b)) ===> true
String a="test";
String b=new String("test");
if(a.equals(b)) ===> true
So using .equals()
is always better.
==
compares Object references.
.equals()
compares String values.
Sometimes ==
gives illusions of comparing String values, as in following cases:
String a="Test";
String b="Test";
if(a==b) ===> true
This is because when you create any String literal, the JVM first searches for that literal in the String pool, and if it finds a match, that same reference will be given to the new String. Because of this, we get:
(a==b) ===> true
String Pool
b -----------------> "test" <-----------------a
However, ==
fails in the following case:
String a="test";
String b=new String("test");
if (a==b) ===> false
In this case for new String("test")
the statement new String will be created on the heap, and that reference will be given to b
, so b
will be given a reference on the heap, not in String pool.
Now a
is pointing to a String in the String pool while b
is pointing to a String on the heap. Because of that we get:
if(a==b) ===> false.
String Pool
"test" <-------------------- a
Heap
"test" <-------------------- b
While .equals()
always compares a value of String so it gives true in both cases:
String a="Test";
String b="Test";
if(a.equals(b)) ===> true
String a="test";
String b=new String("test");
if(a.equals(b)) ===> true
So using .equals()
is always better.
edited Feb 8 at 19:15
community wiki
6 revs, 4 users 69%
Ganesh
3
.equals() compares the two instances however equals is implemented to compare them. That might or might not be comparing the output of toString.
– Jacob
Mar 18 '16 at 2:14
3
@Jacob Object class.equals()
method compares the instances(references/Address) where as String class.equals()
methods is overridden to compare content(chars)
– Satyadev
May 7 '16 at 10:54
1
Good pointing out String pool versus Java heap differences as they are certainly not the same. In the string pool Java tries to "cache"String
objects to save memory footprint asString
is known for being immutable (I hope, I say it correctly here). Also check stackoverflow.com/questions/3052442/…
– Roland
Nov 10 '17 at 13:34
add a comment |
3
.equals() compares the two instances however equals is implemented to compare them. That might or might not be comparing the output of toString.
– Jacob
Mar 18 '16 at 2:14
3
@Jacob Object class.equals()
method compares the instances(references/Address) where as String class.equals()
methods is overridden to compare content(chars)
– Satyadev
May 7 '16 at 10:54
1
Good pointing out String pool versus Java heap differences as they are certainly not the same. In the string pool Java tries to "cache"String
objects to save memory footprint asString
is known for being immutable (I hope, I say it correctly here). Also check stackoverflow.com/questions/3052442/…
– Roland
Nov 10 '17 at 13:34
3
3
.equals() compares the two instances however equals is implemented to compare them. That might or might not be comparing the output of toString.
– Jacob
Mar 18 '16 at 2:14
.equals() compares the two instances however equals is implemented to compare them. That might or might not be comparing the output of toString.
– Jacob
Mar 18 '16 at 2:14
3
3
@Jacob Object class
.equals()
method compares the instances(references/Address) where as String class .equals()
methods is overridden to compare content(chars)– Satyadev
May 7 '16 at 10:54
@Jacob Object class
.equals()
method compares the instances(references/Address) where as String class .equals()
methods is overridden to compare content(chars)– Satyadev
May 7 '16 at 10:54
1
1
Good pointing out String pool versus Java heap differences as they are certainly not the same. In the string pool Java tries to "cache"
String
objects to save memory footprint as String
is known for being immutable (I hope, I say it correctly here). Also check stackoverflow.com/questions/3052442/…– Roland
Nov 10 '17 at 13:34
Good pointing out String pool versus Java heap differences as they are certainly not the same. In the string pool Java tries to "cache"
String
objects to save memory footprint as String
is known for being immutable (I hope, I say it correctly here). Also check stackoverflow.com/questions/3052442/…– Roland
Nov 10 '17 at 13:34
add a comment |
up vote
210
down vote
The ==
operator checks to see if the two strings are exactly the same object.
The .equals()
method will check if the two strings have the same value.
5
Generally I strongly recommend apache commons library: commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/…, java.lang.String)
– Marcin Erbel
Oct 16 '14 at 10:32
You can also use .compareTo() method if you want to evaluate order of strings.
– MarekM
Jun 27 '17 at 14:48
add a comment |
up vote
210
down vote
The ==
operator checks to see if the two strings are exactly the same object.
The .equals()
method will check if the two strings have the same value.
5
Generally I strongly recommend apache commons library: commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/…, java.lang.String)
– Marcin Erbel
Oct 16 '14 at 10:32
You can also use .compareTo() method if you want to evaluate order of strings.
– MarekM
Jun 27 '17 at 14:48
add a comment |
up vote
210
down vote
up vote
210
down vote
The ==
operator checks to see if the two strings are exactly the same object.
The .equals()
method will check if the two strings have the same value.
The ==
operator checks to see if the two strings are exactly the same object.
The .equals()
method will check if the two strings have the same value.
edited Dec 24 '13 at 7:49
community wiki
2 revs, 2 users 67%
Clayton
5
Generally I strongly recommend apache commons library: commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/…, java.lang.String)
– Marcin Erbel
Oct 16 '14 at 10:32
You can also use .compareTo() method if you want to evaluate order of strings.
– MarekM
Jun 27 '17 at 14:48
add a comment |
5
Generally I strongly recommend apache commons library: commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/…, java.lang.String)
– Marcin Erbel
Oct 16 '14 at 10:32
You can also use .compareTo() method if you want to evaluate order of strings.
– MarekM
Jun 27 '17 at 14:48
5
5
Generally I strongly recommend apache commons library: commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/…, java.lang.String)
– Marcin Erbel
Oct 16 '14 at 10:32
Generally I strongly recommend apache commons library: commons.apache.org/proper/commons-lang/javadocs/api-2.6/org/…, java.lang.String)
– Marcin Erbel
Oct 16 '14 at 10:32
You can also use .compareTo() method if you want to evaluate order of strings.
– MarekM
Jun 27 '17 at 14:48
You can also use .compareTo() method if you want to evaluate order of strings.
– MarekM
Jun 27 '17 at 14:48
add a comment |
up vote
159
down vote
Strings in Java are immutable. That means whenever you try to change/modify the string you get a new instance. You cannot change the original string. This has been done so that these string instances can be cached. A typical program contains a lot of string references and caching these instances can decrease the memory footprint and increase the performance of the program.
When using == operator for string comparison you are not comparing the contents of the string, but are actually comparing the memory address. If they are both equal it will return true and false otherwise. Whereas equals in string compares the string contents.
So the question is if all the strings are cached in the system, how come ==
returns false whereas equals return true? Well, this is possible. If you make a new string like String str = new String("Testing")
you end up creating a new string in the cache even if the cache already contains a string having the same content. In short "MyString" == new String("MyString")
will always return false.
Java also talks about the function intern() that can be used on a string to make it part of the cache so "MyString" == new String("MyString").intern()
will return true.
Note: == operator is much faster than equals just because you are comparing two memory addresses, but you need to be sure that the code isn't creating new String instances in the code. Otherwise you will encounter bugs.
add a comment |
up vote
159
down vote
Strings in Java are immutable. That means whenever you try to change/modify the string you get a new instance. You cannot change the original string. This has been done so that these string instances can be cached. A typical program contains a lot of string references and caching these instances can decrease the memory footprint and increase the performance of the program.
When using == operator for string comparison you are not comparing the contents of the string, but are actually comparing the memory address. If they are both equal it will return true and false otherwise. Whereas equals in string compares the string contents.
So the question is if all the strings are cached in the system, how come ==
returns false whereas equals return true? Well, this is possible. If you make a new string like String str = new String("Testing")
you end up creating a new string in the cache even if the cache already contains a string having the same content. In short "MyString" == new String("MyString")
will always return false.
Java also talks about the function intern() that can be used on a string to make it part of the cache so "MyString" == new String("MyString").intern()
will return true.
Note: == operator is much faster than equals just because you are comparing two memory addresses, but you need to be sure that the code isn't creating new String instances in the code. Otherwise you will encounter bugs.
add a comment |
up vote
159
down vote
up vote
159
down vote
Strings in Java are immutable. That means whenever you try to change/modify the string you get a new instance. You cannot change the original string. This has been done so that these string instances can be cached. A typical program contains a lot of string references and caching these instances can decrease the memory footprint and increase the performance of the program.
When using == operator for string comparison you are not comparing the contents of the string, but are actually comparing the memory address. If they are both equal it will return true and false otherwise. Whereas equals in string compares the string contents.
So the question is if all the strings are cached in the system, how come ==
returns false whereas equals return true? Well, this is possible. If you make a new string like String str = new String("Testing")
you end up creating a new string in the cache even if the cache already contains a string having the same content. In short "MyString" == new String("MyString")
will always return false.
Java also talks about the function intern() that can be used on a string to make it part of the cache so "MyString" == new String("MyString").intern()
will return true.
Note: == operator is much faster than equals just because you are comparing two memory addresses, but you need to be sure that the code isn't creating new String instances in the code. Otherwise you will encounter bugs.
Strings in Java are immutable. That means whenever you try to change/modify the string you get a new instance. You cannot change the original string. This has been done so that these string instances can be cached. A typical program contains a lot of string references and caching these instances can decrease the memory footprint and increase the performance of the program.
When using == operator for string comparison you are not comparing the contents of the string, but are actually comparing the memory address. If they are both equal it will return true and false otherwise. Whereas equals in string compares the string contents.
So the question is if all the strings are cached in the system, how come ==
returns false whereas equals return true? Well, this is possible. If you make a new string like String str = new String("Testing")
you end up creating a new string in the cache even if the cache already contains a string having the same content. In short "MyString" == new String("MyString")
will always return false.
Java also talks about the function intern() that can be used on a string to make it part of the cache so "MyString" == new String("MyString").intern()
will return true.
Note: == operator is much faster than equals just because you are comparing two memory addresses, but you need to be sure that the code isn't creating new String instances in the code. Otherwise you will encounter bugs.
edited Feb 8 at 19:10
community wiki
2 revs, 2 users 76%
Faisal Feroz
add a comment |
add a comment |
up vote
137
down vote
String a = new String("foo");
String b = new String("foo");
System.out.println(a == b); // prints false
System.out.println(a.equals(b)); // prints true
Make sure you understand why. It's because the ==
comparison only compares references; the equals()
method does a character-by-character comparison of the contents.
When you call new for a
and b
, each one gets a new reference that points to the "foo"
in the string table. The references are different, but the content is the same.
add a comment |
up vote
137
down vote
String a = new String("foo");
String b = new String("foo");
System.out.println(a == b); // prints false
System.out.println(a.equals(b)); // prints true
Make sure you understand why. It's because the ==
comparison only compares references; the equals()
method does a character-by-character comparison of the contents.
When you call new for a
and b
, each one gets a new reference that points to the "foo"
in the string table. The references are different, but the content is the same.
add a comment |
up vote
137
down vote
up vote
137
down vote
String a = new String("foo");
String b = new String("foo");
System.out.println(a == b); // prints false
System.out.println(a.equals(b)); // prints true
Make sure you understand why. It's because the ==
comparison only compares references; the equals()
method does a character-by-character comparison of the contents.
When you call new for a
and b
, each one gets a new reference that points to the "foo"
in the string table. The references are different, but the content is the same.
String a = new String("foo");
String b = new String("foo");
System.out.println(a == b); // prints false
System.out.println(a.equals(b)); // prints true
Make sure you understand why. It's because the ==
comparison only compares references; the equals()
method does a character-by-character comparison of the contents.
When you call new for a
and b
, each one gets a new reference that points to the "foo"
in the string table. The references are different, but the content is the same.
edited Nov 9 '13 at 12:50
community wiki
duffymo
add a comment |
add a comment |
up vote
120
down vote
Yea, it's bad...
==
means that your two string references are exactly the same object. You may have heard that this is the case because Java keeps sort of a literal table (which it does), but that is not always the case. Some strings are loaded in different ways, constructed from other strings, etc., so you must never assume that two identical strings are stored in the same location.
Equals does the real comparison for you.
add a comment |
up vote
120
down vote
Yea, it's bad...
==
means that your two string references are exactly the same object. You may have heard that this is the case because Java keeps sort of a literal table (which it does), but that is not always the case. Some strings are loaded in different ways, constructed from other strings, etc., so you must never assume that two identical strings are stored in the same location.
Equals does the real comparison for you.
add a comment |
up vote
120
down vote
up vote
120
down vote
Yea, it's bad...
==
means that your two string references are exactly the same object. You may have heard that this is the case because Java keeps sort of a literal table (which it does), but that is not always the case. Some strings are loaded in different ways, constructed from other strings, etc., so you must never assume that two identical strings are stored in the same location.
Equals does the real comparison for you.
Yea, it's bad...
==
means that your two string references are exactly the same object. You may have heard that this is the case because Java keeps sort of a literal table (which it does), but that is not always the case. Some strings are loaded in different ways, constructed from other strings, etc., so you must never assume that two identical strings are stored in the same location.
Equals does the real comparison for you.
edited Nov 19 '17 at 8:16
community wiki
2 revs, 2 users 89%
Uri
add a comment |
add a comment |
up vote
115
down vote
Yes, ==
is bad for comparing Strings (any objects really, unless you know they're canonical). ==
just compares object references. .equals()
tests for equality. For Strings, often they'll be the same but as you've discovered, that's not guaranteed always.
add a comment |
up vote
115
down vote
Yes, ==
is bad for comparing Strings (any objects really, unless you know they're canonical). ==
just compares object references. .equals()
tests for equality. For Strings, often they'll be the same but as you've discovered, that's not guaranteed always.
add a comment |
up vote
115
down vote
up vote
115
down vote
Yes, ==
is bad for comparing Strings (any objects really, unless you know they're canonical). ==
just compares object references. .equals()
tests for equality. For Strings, often they'll be the same but as you've discovered, that's not guaranteed always.
Yes, ==
is bad for comparing Strings (any objects really, unless you know they're canonical). ==
just compares object references. .equals()
tests for equality. For Strings, often they'll be the same but as you've discovered, that's not guaranteed always.
edited Dec 17 '14 at 8:28
community wiki
2 revs, 2 users 67%
cletus
add a comment |
add a comment |
up vote
108
down vote
Java have a String pool under which Java manages the memory allocation for the String objects. See String Pools in Java
When you check (compare) two objects using the ==
operator it compares the address equality into the string-pool. If the two String objects have the same address references then it returns true
, otherwise false
. But if you want to compare the contents of two String objects then you must override the equals
method.
equals
is actually the method of the Object class, but it is Overridden into the String class and a new definition is given which compares the contents of object.
Example:
stringObjectOne.equals(stringObjectTwo);
But mind it respects the case of String. If you want case insensitive compare then you must go for the equalsIgnoreCase method of the String class.
Let's See:
String one = "HELLO";
String two = "HELLO";
String three = new String("HELLO");
String four = "hello";
one == two; // TRUE
one == three; // FALSE
one == four; // FALSE
one.equals(two); // TRUE
one.equals(three); // TRUE
one.equals(four); // FALSE
one.equalsIgnoreCase(four); // TRUE
6
I see that this is a late answer to big question. May I ask what it provides that isn't already mentioned in the existing answers?
– Mysticial
Apr 2 '13 at 9:19
6
@Mysticial he has addedequalsIgnoreCase
which might be informative for the fresher.
– AmitG
Apr 4 '13 at 8:48
add a comment |
up vote
108
down vote
Java have a String pool under which Java manages the memory allocation for the String objects. See String Pools in Java
When you check (compare) two objects using the ==
operator it compares the address equality into the string-pool. If the two String objects have the same address references then it returns true
, otherwise false
. But if you want to compare the contents of two String objects then you must override the equals
method.
equals
is actually the method of the Object class, but it is Overridden into the String class and a new definition is given which compares the contents of object.
Example:
stringObjectOne.equals(stringObjectTwo);
But mind it respects the case of String. If you want case insensitive compare then you must go for the equalsIgnoreCase method of the String class.
Let's See:
String one = "HELLO";
String two = "HELLO";
String three = new String("HELLO");
String four = "hello";
one == two; // TRUE
one == three; // FALSE
one == four; // FALSE
one.equals(two); // TRUE
one.equals(three); // TRUE
one.equals(four); // FALSE
one.equalsIgnoreCase(four); // TRUE
6
I see that this is a late answer to big question. May I ask what it provides that isn't already mentioned in the existing answers?
– Mysticial
Apr 2 '13 at 9:19
6
@Mysticial he has addedequalsIgnoreCase
which might be informative for the fresher.
– AmitG
Apr 4 '13 at 8:48
add a comment |
up vote
108
down vote
up vote
108
down vote
Java have a String pool under which Java manages the memory allocation for the String objects. See String Pools in Java
When you check (compare) two objects using the ==
operator it compares the address equality into the string-pool. If the two String objects have the same address references then it returns true
, otherwise false
. But if you want to compare the contents of two String objects then you must override the equals
method.
equals
is actually the method of the Object class, but it is Overridden into the String class and a new definition is given which compares the contents of object.
Example:
stringObjectOne.equals(stringObjectTwo);
But mind it respects the case of String. If you want case insensitive compare then you must go for the equalsIgnoreCase method of the String class.
Let's See:
String one = "HELLO";
String two = "HELLO";
String three = new String("HELLO");
String four = "hello";
one == two; // TRUE
one == three; // FALSE
one == four; // FALSE
one.equals(two); // TRUE
one.equals(three); // TRUE
one.equals(four); // FALSE
one.equalsIgnoreCase(four); // TRUE
Java have a String pool under which Java manages the memory allocation for the String objects. See String Pools in Java
When you check (compare) two objects using the ==
operator it compares the address equality into the string-pool. If the two String objects have the same address references then it returns true
, otherwise false
. But if you want to compare the contents of two String objects then you must override the equals
method.
equals
is actually the method of the Object class, but it is Overridden into the String class and a new definition is given which compares the contents of object.
Example:
stringObjectOne.equals(stringObjectTwo);
But mind it respects the case of String. If you want case insensitive compare then you must go for the equalsIgnoreCase method of the String class.
Let's See:
String one = "HELLO";
String two = "HELLO";
String three = new String("HELLO");
String four = "hello";
one == two; // TRUE
one == three; // FALSE
one == four; // FALSE
one.equals(two); // TRUE
one.equals(three); // TRUE
one.equals(four); // FALSE
one.equalsIgnoreCase(four); // TRUE
edited Feb 8 at 19:19
community wiki
4 revs, 3 users 80%
Saurabh Agarwal
6
I see that this is a late answer to big question. May I ask what it provides that isn't already mentioned in the existing answers?
– Mysticial
Apr 2 '13 at 9:19
6
@Mysticial he has addedequalsIgnoreCase
which might be informative for the fresher.
– AmitG
Apr 4 '13 at 8:48
add a comment |
6
I see that this is a late answer to big question. May I ask what it provides that isn't already mentioned in the existing answers?
– Mysticial
Apr 2 '13 at 9:19
6
@Mysticial he has addedequalsIgnoreCase
which might be informative for the fresher.
– AmitG
Apr 4 '13 at 8:48
6
6
I see that this is a late answer to big question. May I ask what it provides that isn't already mentioned in the existing answers?
– Mysticial
Apr 2 '13 at 9:19
I see that this is a late answer to big question. May I ask what it provides that isn't already mentioned in the existing answers?
– Mysticial
Apr 2 '13 at 9:19
6
6
@Mysticial he has added
equalsIgnoreCase
which might be informative for the fresher.– AmitG
Apr 4 '13 at 8:48
@Mysticial he has added
equalsIgnoreCase
which might be informative for the fresher.– AmitG
Apr 4 '13 at 8:48
add a comment |
up vote
96
down vote
==
compares object references in Java, and that is no exception for String
objects.
For comparing the actual contents of objects (including String
), one must use the equals
method.
If a comparison of two String
objects using ==
turns out to be true
, that is because the String
objects were interned, and the Java Virtual Machine is having multiple references point to the same instance of String
. One should not expect that comparing one String
object containing the same contents as another String
object using ==
to evaluate as true
.
add a comment |
up vote
96
down vote
==
compares object references in Java, and that is no exception for String
objects.
For comparing the actual contents of objects (including String
), one must use the equals
method.
If a comparison of two String
objects using ==
turns out to be true
, that is because the String
objects were interned, and the Java Virtual Machine is having multiple references point to the same instance of String
. One should not expect that comparing one String
object containing the same contents as another String
object using ==
to evaluate as true
.
add a comment |
up vote
96
down vote
up vote
96
down vote
==
compares object references in Java, and that is no exception for String
objects.
For comparing the actual contents of objects (including String
), one must use the equals
method.
If a comparison of two String
objects using ==
turns out to be true
, that is because the String
objects were interned, and the Java Virtual Machine is having multiple references point to the same instance of String
. One should not expect that comparing one String
object containing the same contents as another String
object using ==
to evaluate as true
.
==
compares object references in Java, and that is no exception for String
objects.
For comparing the actual contents of objects (including String
), one must use the equals
method.
If a comparison of two String
objects using ==
turns out to be true
, that is because the String
objects were interned, and the Java Virtual Machine is having multiple references point to the same instance of String
. One should not expect that comparing one String
object containing the same contents as another String
object using ==
to evaluate as true
.
answered Feb 4 '09 at 23:20
community wiki
coobird
add a comment |
add a comment |
up vote
95
down vote
.equals()
compares the data in a class (assuming the function is implemented).
==
compares pointer locations (location of the object in memory).
==
returns true if both objects (NOT TALKING ABOUT PRIMITIVES) point to the SAME object instance.
.equals()
returns true if the two objects contain the same data equals()
Versus ==
in Java
That may help you.
add a comment |
up vote
95
down vote
.equals()
compares the data in a class (assuming the function is implemented).
==
compares pointer locations (location of the object in memory).
==
returns true if both objects (NOT TALKING ABOUT PRIMITIVES) point to the SAME object instance.
.equals()
returns true if the two objects contain the same data equals()
Versus ==
in Java
That may help you.
add a comment |
up vote
95
down vote
up vote
95
down vote
.equals()
compares the data in a class (assuming the function is implemented).
==
compares pointer locations (location of the object in memory).
==
returns true if both objects (NOT TALKING ABOUT PRIMITIVES) point to the SAME object instance.
.equals()
returns true if the two objects contain the same data equals()
Versus ==
in Java
That may help you.
.equals()
compares the data in a class (assuming the function is implemented).
==
compares pointer locations (location of the object in memory).
==
returns true if both objects (NOT TALKING ABOUT PRIMITIVES) point to the SAME object instance.
.equals()
returns true if the two objects contain the same data equals()
Versus ==
in Java
That may help you.
edited Dec 17 '14 at 8:30
community wiki
3 revs, 3 users 64%
Matt Razza
add a comment |
add a comment |
up vote
92
down vote
I agree with the answer from zacherates.
But what you can do is to call intern()
on your non-literal strings.
From zacherates example:
// ... but they are not the same object
new String("test") == "test" ==> false
If you intern the non-literal String equality is true
new String("test").intern() == "test" ==> true
5
This is generally not a good idea. Interning is relatively expensive and can (paradoxically) >>increase<< your JVMs memory footprint and increase GC costs. In most cases, these outweigh the performance benefits from using==
for string comparison.
– Stephen C
Sep 25 '15 at 23:16
add a comment |
up vote
92
down vote
I agree with the answer from zacherates.
But what you can do is to call intern()
on your non-literal strings.
From zacherates example:
// ... but they are not the same object
new String("test") == "test" ==> false
If you intern the non-literal String equality is true
new String("test").intern() == "test" ==> true
5
This is generally not a good idea. Interning is relatively expensive and can (paradoxically) >>increase<< your JVMs memory footprint and increase GC costs. In most cases, these outweigh the performance benefits from using==
for string comparison.
– Stephen C
Sep 25 '15 at 23:16
add a comment |
up vote
92
down vote
up vote
92
down vote
I agree with the answer from zacherates.
But what you can do is to call intern()
on your non-literal strings.
From zacherates example:
// ... but they are not the same object
new String("test") == "test" ==> false
If you intern the non-literal String equality is true
new String("test").intern() == "test" ==> true
I agree with the answer from zacherates.
But what you can do is to call intern()
on your non-literal strings.
From zacherates example:
// ... but they are not the same object
new String("test") == "test" ==> false
If you intern the non-literal String equality is true
new String("test").intern() == "test" ==> true
edited Sep 21 at 1:53
community wiki
3 revs, 3 users 83%
pgras
5
This is generally not a good idea. Interning is relatively expensive and can (paradoxically) >>increase<< your JVMs memory footprint and increase GC costs. In most cases, these outweigh the performance benefits from using==
for string comparison.
– Stephen C
Sep 25 '15 at 23:16
add a comment |
5
This is generally not a good idea. Interning is relatively expensive and can (paradoxically) >>increase<< your JVMs memory footprint and increase GC costs. In most cases, these outweigh the performance benefits from using==
for string comparison.
– Stephen C
Sep 25 '15 at 23:16
5
5
This is generally not a good idea. Interning is relatively expensive and can (paradoxically) >>increase<< your JVMs memory footprint and increase GC costs. In most cases, these outweigh the performance benefits from using
==
for string comparison.– Stephen C
Sep 25 '15 at 23:16
This is generally not a good idea. Interning is relatively expensive and can (paradoxically) >>increase<< your JVMs memory footprint and increase GC costs. In most cases, these outweigh the performance benefits from using
==
for string comparison.– Stephen C
Sep 25 '15 at 23:16
add a comment |
up vote
90
down vote
==
performs a reference equality check, whether the 2 objects (strings in this case) refer to the same object in the memory.
The equals()
method will check whether the contents or the states of 2 objects are the same.
Obviously ==
is faster, but will (might) give false results in many cases if you just want to tell if 2 String
s hold the same text.
Definitely the use of equals()
method is recommended.
Don't worry about the performance. Some things to encourage using String.equals()
:
- Implementation of
String.equals()
first checks for reference equality (using==
), and if the 2 strings are the same by reference, no further calculation is performed! - If the 2 string references are not the same,
String.equals()
will next check the lengths of the strings. This is also a fast operation because theString
class stores the length of the string, no need to count the characters or code points. If the lengths differ, no further check is performed, we know they cannot be equal. - Only if we got this far will the contents of the 2 strings be actually compared, and this will be a short-hand comparison: not all the characters will be compared, if we find a mismatching character (at the same position in the 2 strings), no further characters will be checked.
When all is said and done, even if we have guarantee that the strings are interns, using the equals()
method is still not that overhead that one might think, definitely the recommended way. If you want efficient reference check, then use enums where it is guaranteed by the language specification and implementation that the same enum value will be the same object (by reference).
1
Obviously == is faster
-- actually the implementation of.equals(String)
first checks==
before anything else so I would say the speed is about identical.
– Razzle Shazl
Jun 22 at 6:38
1
public boolean equals(Object anObject) { if (this == anObject) { return true; } ...
– Razzle Shazl
Jun 22 at 6:51
add a comment |
up vote
90
down vote
==
performs a reference equality check, whether the 2 objects (strings in this case) refer to the same object in the memory.
The equals()
method will check whether the contents or the states of 2 objects are the same.
Obviously ==
is faster, but will (might) give false results in many cases if you just want to tell if 2 String
s hold the same text.
Definitely the use of equals()
method is recommended.
Don't worry about the performance. Some things to encourage using String.equals()
:
- Implementation of
String.equals()
first checks for reference equality (using==
), and if the 2 strings are the same by reference, no further calculation is performed! - If the 2 string references are not the same,
String.equals()
will next check the lengths of the strings. This is also a fast operation because theString
class stores the length of the string, no need to count the characters or code points. If the lengths differ, no further check is performed, we know they cannot be equal. - Only if we got this far will the contents of the 2 strings be actually compared, and this will be a short-hand comparison: not all the characters will be compared, if we find a mismatching character (at the same position in the 2 strings), no further characters will be checked.
When all is said and done, even if we have guarantee that the strings are interns, using the equals()
method is still not that overhead that one might think, definitely the recommended way. If you want efficient reference check, then use enums where it is guaranteed by the language specification and implementation that the same enum value will be the same object (by reference).
1
Obviously == is faster
-- actually the implementation of.equals(String)
first checks==
before anything else so I would say the speed is about identical.
– Razzle Shazl
Jun 22 at 6:38
1
public boolean equals(Object anObject) { if (this == anObject) { return true; } ...
– Razzle Shazl
Jun 22 at 6:51
add a comment |
up vote
90
down vote
up vote
90
down vote
==
performs a reference equality check, whether the 2 objects (strings in this case) refer to the same object in the memory.
The equals()
method will check whether the contents or the states of 2 objects are the same.
Obviously ==
is faster, but will (might) give false results in many cases if you just want to tell if 2 String
s hold the same text.
Definitely the use of equals()
method is recommended.
Don't worry about the performance. Some things to encourage using String.equals()
:
- Implementation of
String.equals()
first checks for reference equality (using==
), and if the 2 strings are the same by reference, no further calculation is performed! - If the 2 string references are not the same,
String.equals()
will next check the lengths of the strings. This is also a fast operation because theString
class stores the length of the string, no need to count the characters or code points. If the lengths differ, no further check is performed, we know they cannot be equal. - Only if we got this far will the contents of the 2 strings be actually compared, and this will be a short-hand comparison: not all the characters will be compared, if we find a mismatching character (at the same position in the 2 strings), no further characters will be checked.
When all is said and done, even if we have guarantee that the strings are interns, using the equals()
method is still not that overhead that one might think, definitely the recommended way. If you want efficient reference check, then use enums where it is guaranteed by the language specification and implementation that the same enum value will be the same object (by reference).
==
performs a reference equality check, whether the 2 objects (strings in this case) refer to the same object in the memory.
The equals()
method will check whether the contents or the states of 2 objects are the same.
Obviously ==
is faster, but will (might) give false results in many cases if you just want to tell if 2 String
s hold the same text.
Definitely the use of equals()
method is recommended.
Don't worry about the performance. Some things to encourage using String.equals()
:
- Implementation of
String.equals()
first checks for reference equality (using==
), and if the 2 strings are the same by reference, no further calculation is performed! - If the 2 string references are not the same,
String.equals()
will next check the lengths of the strings. This is also a fast operation because theString
class stores the length of the string, no need to count the characters or code points. If the lengths differ, no further check is performed, we know they cannot be equal. - Only if we got this far will the contents of the 2 strings be actually compared, and this will be a short-hand comparison: not all the characters will be compared, if we find a mismatching character (at the same position in the 2 strings), no further characters will be checked.
When all is said and done, even if we have guarantee that the strings are interns, using the equals()
method is still not that overhead that one might think, definitely the recommended way. If you want efficient reference check, then use enums where it is guaranteed by the language specification and implementation that the same enum value will be the same object (by reference).
edited Aug 11 '15 at 6:40
community wiki
5 revs
icza
1
Obviously == is faster
-- actually the implementation of.equals(String)
first checks==
before anything else so I would say the speed is about identical.
– Razzle Shazl
Jun 22 at 6:38
1
public boolean equals(Object anObject) { if (this == anObject) { return true; } ...
– Razzle Shazl
Jun 22 at 6:51
add a comment |
1
Obviously == is faster
-- actually the implementation of.equals(String)
first checks==
before anything else so I would say the speed is about identical.
– Razzle Shazl
Jun 22 at 6:38
1
public boolean equals(Object anObject) { if (this == anObject) { return true; } ...
– Razzle Shazl
Jun 22 at 6:51
1
1
Obviously == is faster
-- actually the implementation of .equals(String)
first checks ==
before anything else so I would say the speed is about identical.– Razzle Shazl
Jun 22 at 6:38
Obviously == is faster
-- actually the implementation of .equals(String)
first checks ==
before anything else so I would say the speed is about identical.– Razzle Shazl
Jun 22 at 6:38
1
1
public boolean equals(Object anObject) { if (this == anObject) { return true; } ...
– Razzle Shazl
Jun 22 at 6:51
public boolean equals(Object anObject) { if (this == anObject) { return true; } ...
– Razzle Shazl
Jun 22 at 6:51
add a comment |
up vote
79
down vote
If you're like me, when I first started using Java, I wanted to use the "==" operator to test whether two String instances were equal, but for better or worse, that's not the correct way to do it in Java.
In this tutorial I'll demonstrate several different ways to correctly compare Java strings, starting with the approach I use most of the time. At the end of this Java String comparison tutorial I'll also discuss why the "==" operator doesn't work when comparing Java strings.
Option 1: Java String comparison with the equals method
Most of the time (maybe 95% of the time) I compare strings with the equals method of the Java String class, like this:
if (string1.equals(string2))
This String equals method looks at the two Java strings, and if they contain the exact same string of characters, they are considered equal.
Taking a look at a quick String comparison example with the equals method, if the following test were run, the two strings would not be considered equal because the characters are not the exactly the same (the case of the characters is different):
String string1 = "foo";
String string2 = "FOO";
if (string1.equals(string2))
{
// this line will not print because the
// java string equals method returns false:
System.out.println("The two strings are the same.")
}
But, when the two strings contain the exact same string of characters, the equals method will return true, as in this example:
String string1 = "foo";
String string2 = "foo";
// test for equality with the java string equals method
if (string1.equals(string2))
{
// this line WILL print
System.out.println("The two strings are the same.")
}
Option 2: String comparison with the equalsIgnoreCase method
In some string comparison tests you'll want to ignore whether the strings are uppercase or lowercase. When you want to test your strings for equality in this case-insensitive manner, use the equalsIgnoreCase method of the String class, like this:
String string1 = "foo";
String string2 = "FOO";
// java string compare while ignoring case
if (string1.equalsIgnoreCase(string2))
{
// this line WILL print
System.out.println("Ignoring case, the two strings are the same.")
}
Option 3: Java String comparison with the compareTo method
There is also a third, less common way to compare Java strings, and that's with the String class compareTo method. If the two strings are exactly the same, the compareTo method will return a value of 0 (zero). Here's a quick example of what this String comparison approach looks like:
String string1 = "foo bar";
String string2 = "foo bar";
// java string compare example
if (string1.compareTo(string2) == 0)
{
// this line WILL print
System.out.println("The two strings are the same.")
}
While I'm writing about this concept of equality in Java, it's important to note that the Java language includes an equals method in the base Java Object class. Whenever you're creating your own objects and you want to provide a means to see if two instances of your object are "equal", you should override (and implement) this equals method in your class (in the same way the Java language provides this equality/comparison behavior in the String equals method).
You may want to have a look at this ==, .equals(), compareTo(), and compare()
5
for string literals Like String string1 = "foo bar"; String string2 = "foo bar"; you can directly use == operator to test content equality
– JAVA
Sep 7 '13 at 18:11
1
In google apps script "compareTo" is not possibe. I tried instaed "equals" This was the only solution that works....
– user3887038
Apr 24 at 12:40
add a comment |
up vote
79
down vote
If you're like me, when I first started using Java, I wanted to use the "==" operator to test whether two String instances were equal, but for better or worse, that's not the correct way to do it in Java.
In this tutorial I'll demonstrate several different ways to correctly compare Java strings, starting with the approach I use most of the time. At the end of this Java String comparison tutorial I'll also discuss why the "==" operator doesn't work when comparing Java strings.
Option 1: Java String comparison with the equals method
Most of the time (maybe 95% of the time) I compare strings with the equals method of the Java String class, like this:
if (string1.equals(string2))
This String equals method looks at the two Java strings, and if they contain the exact same string of characters, they are considered equal.
Taking a look at a quick String comparison example with the equals method, if the following test were run, the two strings would not be considered equal because the characters are not the exactly the same (the case of the characters is different):
String string1 = "foo";
String string2 = "FOO";
if (string1.equals(string2))
{
// this line will not print because the
// java string equals method returns false:
System.out.println("The two strings are the same.")
}
But, when the two strings contain the exact same string of characters, the equals method will return true, as in this example:
String string1 = "foo";
String string2 = "foo";
// test for equality with the java string equals method
if (string1.equals(string2))
{
// this line WILL print
System.out.println("The two strings are the same.")
}
Option 2: String comparison with the equalsIgnoreCase method
In some string comparison tests you'll want to ignore whether the strings are uppercase or lowercase. When you want to test your strings for equality in this case-insensitive manner, use the equalsIgnoreCase method of the String class, like this:
String string1 = "foo";
String string2 = "FOO";
// java string compare while ignoring case
if (string1.equalsIgnoreCase(string2))
{
// this line WILL print
System.out.println("Ignoring case, the two strings are the same.")
}
Option 3: Java String comparison with the compareTo method
There is also a third, less common way to compare Java strings, and that's with the String class compareTo method. If the two strings are exactly the same, the compareTo method will return a value of 0 (zero). Here's a quick example of what this String comparison approach looks like:
String string1 = "foo bar";
String string2 = "foo bar";
// java string compare example
if (string1.compareTo(string2) == 0)
{
// this line WILL print
System.out.println("The two strings are the same.")
}
While I'm writing about this concept of equality in Java, it's important to note that the Java language includes an equals method in the base Java Object class. Whenever you're creating your own objects and you want to provide a means to see if two instances of your object are "equal", you should override (and implement) this equals method in your class (in the same way the Java language provides this equality/comparison behavior in the String equals method).
You may want to have a look at this ==, .equals(), compareTo(), and compare()
5
for string literals Like String string1 = "foo bar"; String string2 = "foo bar"; you can directly use == operator to test content equality
– JAVA
Sep 7 '13 at 18:11
1
In google apps script "compareTo" is not possibe. I tried instaed "equals" This was the only solution that works....
– user3887038
Apr 24 at 12:40
add a comment |
up vote
79
down vote
up vote
79
down vote
If you're like me, when I first started using Java, I wanted to use the "==" operator to test whether two String instances were equal, but for better or worse, that's not the correct way to do it in Java.
In this tutorial I'll demonstrate several different ways to correctly compare Java strings, starting with the approach I use most of the time. At the end of this Java String comparison tutorial I'll also discuss why the "==" operator doesn't work when comparing Java strings.
Option 1: Java String comparison with the equals method
Most of the time (maybe 95% of the time) I compare strings with the equals method of the Java String class, like this:
if (string1.equals(string2))
This String equals method looks at the two Java strings, and if they contain the exact same string of characters, they are considered equal.
Taking a look at a quick String comparison example with the equals method, if the following test were run, the two strings would not be considered equal because the characters are not the exactly the same (the case of the characters is different):
String string1 = "foo";
String string2 = "FOO";
if (string1.equals(string2))
{
// this line will not print because the
// java string equals method returns false:
System.out.println("The two strings are the same.")
}
But, when the two strings contain the exact same string of characters, the equals method will return true, as in this example:
String string1 = "foo";
String string2 = "foo";
// test for equality with the java string equals method
if (string1.equals(string2))
{
// this line WILL print
System.out.println("The two strings are the same.")
}
Option 2: String comparison with the equalsIgnoreCase method
In some string comparison tests you'll want to ignore whether the strings are uppercase or lowercase. When you want to test your strings for equality in this case-insensitive manner, use the equalsIgnoreCase method of the String class, like this:
String string1 = "foo";
String string2 = "FOO";
// java string compare while ignoring case
if (string1.equalsIgnoreCase(string2))
{
// this line WILL print
System.out.println("Ignoring case, the two strings are the same.")
}
Option 3: Java String comparison with the compareTo method
There is also a third, less common way to compare Java strings, and that's with the String class compareTo method. If the two strings are exactly the same, the compareTo method will return a value of 0 (zero). Here's a quick example of what this String comparison approach looks like:
String string1 = "foo bar";
String string2 = "foo bar";
// java string compare example
if (string1.compareTo(string2) == 0)
{
// this line WILL print
System.out.println("The two strings are the same.")
}
While I'm writing about this concept of equality in Java, it's important to note that the Java language includes an equals method in the base Java Object class. Whenever you're creating your own objects and you want to provide a means to see if two instances of your object are "equal", you should override (and implement) this equals method in your class (in the same way the Java language provides this equality/comparison behavior in the String equals method).
You may want to have a look at this ==, .equals(), compareTo(), and compare()
If you're like me, when I first started using Java, I wanted to use the "==" operator to test whether two String instances were equal, but for better or worse, that's not the correct way to do it in Java.
In this tutorial I'll demonstrate several different ways to correctly compare Java strings, starting with the approach I use most of the time. At the end of this Java String comparison tutorial I'll also discuss why the "==" operator doesn't work when comparing Java strings.
Option 1: Java String comparison with the equals method
Most of the time (maybe 95% of the time) I compare strings with the equals method of the Java String class, like this:
if (string1.equals(string2))
This String equals method looks at the two Java strings, and if they contain the exact same string of characters, they are considered equal.
Taking a look at a quick String comparison example with the equals method, if the following test were run, the two strings would not be considered equal because the characters are not the exactly the same (the case of the characters is different):
String string1 = "foo";
String string2 = "FOO";
if (string1.equals(string2))
{
// this line will not print because the
// java string equals method returns false:
System.out.println("The two strings are the same.")
}
But, when the two strings contain the exact same string of characters, the equals method will return true, as in this example:
String string1 = "foo";
String string2 = "foo";
// test for equality with the java string equals method
if (string1.equals(string2))
{
// this line WILL print
System.out.println("The two strings are the same.")
}
Option 2: String comparison with the equalsIgnoreCase method
In some string comparison tests you'll want to ignore whether the strings are uppercase or lowercase. When you want to test your strings for equality in this case-insensitive manner, use the equalsIgnoreCase method of the String class, like this:
String string1 = "foo";
String string2 = "FOO";
// java string compare while ignoring case
if (string1.equalsIgnoreCase(string2))
{
// this line WILL print
System.out.println("Ignoring case, the two strings are the same.")
}
Option 3: Java String comparison with the compareTo method
There is also a third, less common way to compare Java strings, and that's with the String class compareTo method. If the two strings are exactly the same, the compareTo method will return a value of 0 (zero). Here's a quick example of what this String comparison approach looks like:
String string1 = "foo bar";
String string2 = "foo bar";
// java string compare example
if (string1.compareTo(string2) == 0)
{
// this line WILL print
System.out.println("The two strings are the same.")
}
While I'm writing about this concept of equality in Java, it's important to note that the Java language includes an equals method in the base Java Object class. Whenever you're creating your own objects and you want to provide a means to see if two instances of your object are "equal", you should override (and implement) this equals method in your class (in the same way the Java language provides this equality/comparison behavior in the String equals method).
You may want to have a look at this ==, .equals(), compareTo(), and compare()
edited Mar 16 '16 at 15:00
community wiki
2 revs, 2 users 92%
Mohamed E. ManSour
5
for string literals Like String string1 = "foo bar"; String string2 = "foo bar"; you can directly use == operator to test content equality
– JAVA
Sep 7 '13 at 18:11
1
In google apps script "compareTo" is not possibe. I tried instaed "equals" This was the only solution that works....
– user3887038
Apr 24 at 12:40
add a comment |
5
for string literals Like String string1 = "foo bar"; String string2 = "foo bar"; you can directly use == operator to test content equality
– JAVA
Sep 7 '13 at 18:11
1
In google apps script "compareTo" is not possibe. I tried instaed "equals" This was the only solution that works....
– user3887038
Apr 24 at 12:40
5
5
for string literals Like String string1 = "foo bar"; String string2 = "foo bar"; you can directly use == operator to test content equality
– JAVA
Sep 7 '13 at 18:11
for string literals Like String string1 = "foo bar"; String string2 = "foo bar"; you can directly use == operator to test content equality
– JAVA
Sep 7 '13 at 18:11
1
1
In google apps script "compareTo" is not possibe. I tried instaed "equals" This was the only solution that works....
– user3887038
Apr 24 at 12:40
In google apps script "compareTo" is not possibe. I tried instaed "equals" This was the only solution that works....
– user3887038
Apr 24 at 12:40
add a comment |
up vote
74
down vote
Function:
public float simpleSimilarity(String u, String v) {
String a = u.split(" ");
String b = v.split(" ");
long correct = 0;
int minLen = Math.min(a.length, b.length);
for (int i = 0; i < minLen; i++) {
String aa = a[i];
String bb = b[i];
int minWordLength = Math.min(aa.length(), bb.length());
for (int j = 0; j < minWordLength; j++) {
if (aa.charAt(j) == bb.charAt(j)) {
correct++;
}
}
}
return (float) (((double) correct) / Math.max(u.length(), v.length()));
}
Test:
String a = "This is the first string.";
String b = "this is not 1st string!";
// for exact string comparison, use .equals
boolean exact = a.equals(b);
// For similarity check, there are libraries for this
// Here I'll try a simple example I wrote
float similarity = simple_similarity(a,b);
5
How does this differ from other answers? and why do it the way you suggest
– Mark
Sep 10 '15 at 12:27
2
@Mark The question on difference between==
andequals
was already answered by other solutions, I just offered a different way to compare strings in a loose way
– Khaled.K
Sep 10 '15 at 20:45
add a comment |
up vote
74
down vote
Function:
public float simpleSimilarity(String u, String v) {
String a = u.split(" ");
String b = v.split(" ");
long correct = 0;
int minLen = Math.min(a.length, b.length);
for (int i = 0; i < minLen; i++) {
String aa = a[i];
String bb = b[i];
int minWordLength = Math.min(aa.length(), bb.length());
for (int j = 0; j < minWordLength; j++) {
if (aa.charAt(j) == bb.charAt(j)) {
correct++;
}
}
}
return (float) (((double) correct) / Math.max(u.length(), v.length()));
}
Test:
String a = "This is the first string.";
String b = "this is not 1st string!";
// for exact string comparison, use .equals
boolean exact = a.equals(b);
// For similarity check, there are libraries for this
// Here I'll try a simple example I wrote
float similarity = simple_similarity(a,b);
5
How does this differ from other answers? and why do it the way you suggest
– Mark
Sep 10 '15 at 12:27
2
@Mark The question on difference between==
andequals
was already answered by other solutions, I just offered a different way to compare strings in a loose way
– Khaled.K
Sep 10 '15 at 20:45
add a comment |
up vote
74
down vote
up vote
74
down vote
Function:
public float simpleSimilarity(String u, String v) {
String a = u.split(" ");
String b = v.split(" ");
long correct = 0;
int minLen = Math.min(a.length, b.length);
for (int i = 0; i < minLen; i++) {
String aa = a[i];
String bb = b[i];
int minWordLength = Math.min(aa.length(), bb.length());
for (int j = 0; j < minWordLength; j++) {
if (aa.charAt(j) == bb.charAt(j)) {
correct++;
}
}
}
return (float) (((double) correct) / Math.max(u.length(), v.length()));
}
Test:
String a = "This is the first string.";
String b = "this is not 1st string!";
// for exact string comparison, use .equals
boolean exact = a.equals(b);
// For similarity check, there are libraries for this
// Here I'll try a simple example I wrote
float similarity = simple_similarity(a,b);
Function:
public float simpleSimilarity(String u, String v) {
String a = u.split(" ");
String b = v.split(" ");
long correct = 0;
int minLen = Math.min(a.length, b.length);
for (int i = 0; i < minLen; i++) {
String aa = a[i];
String bb = b[i];
int minWordLength = Math.min(aa.length(), bb.length());
for (int j = 0; j < minWordLength; j++) {
if (aa.charAt(j) == bb.charAt(j)) {
correct++;
}
}
}
return (float) (((double) correct) / Math.max(u.length(), v.length()));
}
Test:
String a = "This is the first string.";
String b = "this is not 1st string!";
// for exact string comparison, use .equals
boolean exact = a.equals(b);
// For similarity check, there are libraries for this
// Here I'll try a simple example I wrote
float similarity = simple_similarity(a,b);
edited Sep 9 '16 at 11:55
community wiki
4 revs, 3 users 69%
Khaled.K
5
How does this differ from other answers? and why do it the way you suggest
– Mark
Sep 10 '15 at 12:27
2
@Mark The question on difference between==
andequals
was already answered by other solutions, I just offered a different way to compare strings in a loose way
– Khaled.K
Sep 10 '15 at 20:45
add a comment |
5
How does this differ from other answers? and why do it the way you suggest
– Mark
Sep 10 '15 at 12:27
2
@Mark The question on difference between==
andequals
was already answered by other solutions, I just offered a different way to compare strings in a loose way
– Khaled.K
Sep 10 '15 at 20:45
5
5
How does this differ from other answers? and why do it the way you suggest
– Mark
Sep 10 '15 at 12:27
How does this differ from other answers? and why do it the way you suggest
– Mark
Sep 10 '15 at 12:27
2
2
@Mark The question on difference between
==
and equals
was already answered by other solutions, I just offered a different way to compare strings in a loose way– Khaled.K
Sep 10 '15 at 20:45
@Mark The question on difference between
==
and equals
was already answered by other solutions, I just offered a different way to compare strings in a loose way– Khaled.K
Sep 10 '15 at 20:45
add a comment |
up vote
69
down vote
The ==
operator check if the two references point to the same object or not. .equals()
check for the actual string content (value).
Note that the .equals()
method belongs to class Object
(super class of all classes). You need to override it as per you class requirement, but for String it is already implemented, and it checks whether two strings have the same value or not.
Case 1
String s1 = "Stack Overflow";
String s2 = "Stack Overflow";
s1 == s2; //true
s1.equals(s2); //true
Reason: String literals created without null are stored in the String pool in the permgen area of heap. So both s1 and s2 point to same object in the pool.
Case 2
String s1 = new String("Stack Overflow");
String s2 = new String("Stack Overflow");
s1 == s2; //false
s1.equals(s2); //true
Reason: If you create a String object using the
new
keyword a separate space is allocated to it on the heap.
add a comment |
up vote
69
down vote
The ==
operator check if the two references point to the same object or not. .equals()
check for the actual string content (value).
Note that the .equals()
method belongs to class Object
(super class of all classes). You need to override it as per you class requirement, but for String it is already implemented, and it checks whether two strings have the same value or not.
Case 1
String s1 = "Stack Overflow";
String s2 = "Stack Overflow";
s1 == s2; //true
s1.equals(s2); //true
Reason: String literals created without null are stored in the String pool in the permgen area of heap. So both s1 and s2 point to same object in the pool.
Case 2
String s1 = new String("Stack Overflow");
String s2 = new String("Stack Overflow");
s1 == s2; //false
s1.equals(s2); //true
Reason: If you create a String object using the
new
keyword a separate space is allocated to it on the heap.
add a comment |
up vote
69
down vote
up vote
69
down vote
The ==
operator check if the two references point to the same object or not. .equals()
check for the actual string content (value).
Note that the .equals()
method belongs to class Object
(super class of all classes). You need to override it as per you class requirement, but for String it is already implemented, and it checks whether two strings have the same value or not.
Case 1
String s1 = "Stack Overflow";
String s2 = "Stack Overflow";
s1 == s2; //true
s1.equals(s2); //true
Reason: String literals created without null are stored in the String pool in the permgen area of heap. So both s1 and s2 point to same object in the pool.
Case 2
String s1 = new String("Stack Overflow");
String s2 = new String("Stack Overflow");
s1 == s2; //false
s1.equals(s2); //true
Reason: If you create a String object using the
new
keyword a separate space is allocated to it on the heap.
The ==
operator check if the two references point to the same object or not. .equals()
check for the actual string content (value).
Note that the .equals()
method belongs to class Object
(super class of all classes). You need to override it as per you class requirement, but for String it is already implemented, and it checks whether two strings have the same value or not.
Case 1
String s1 = "Stack Overflow";
String s2 = "Stack Overflow";
s1 == s2; //true
s1.equals(s2); //true
Reason: String literals created without null are stored in the String pool in the permgen area of heap. So both s1 and s2 point to same object in the pool.
Case 2
String s1 = new String("Stack Overflow");
String s2 = new String("Stack Overflow");
s1 == s2; //false
s1.equals(s2); //true
Reason: If you create a String object using the
new
keyword a separate space is allocated to it on the heap.
edited Feb 8 at 19:30
community wiki
4 revs, 3 users 50%
Aniket Thakur
add a comment |
add a comment |
up vote
50
down vote
==
compares the reference value of objects whereas the equals()
method present in the java.lang.String
class compares the contents of the String
object (to another object).
14
not to be nit picky, but theequals()
method forString
is actually in theString
class, not inObject
. The defaultequals()
inObject
would not compare that the contents are the same, and in fact just returns true when the reference is the same.
– Jacob Schoen
Nov 20 '12 at 17:04
add a comment |
up vote
50
down vote
==
compares the reference value of objects whereas the equals()
method present in the java.lang.String
class compares the contents of the String
object (to another object).
14
not to be nit picky, but theequals()
method forString
is actually in theString
class, not inObject
. The defaultequals()
inObject
would not compare that the contents are the same, and in fact just returns true when the reference is the same.
– Jacob Schoen
Nov 20 '12 at 17:04
add a comment |
up vote
50
down vote
up vote
50
down vote
==
compares the reference value of objects whereas the equals()
method present in the java.lang.String
class compares the contents of the String
object (to another object).
==
compares the reference value of objects whereas the equals()
method present in the java.lang.String
class compares the contents of the String
object (to another object).
edited Mar 4 '14 at 20:19
community wiki
3 revs, 3 users 50%
samkit shah
14
not to be nit picky, but theequals()
method forString
is actually in theString
class, not inObject
. The defaultequals()
inObject
would not compare that the contents are the same, and in fact just returns true when the reference is the same.
– Jacob Schoen
Nov 20 '12 at 17:04
add a comment |
14
not to be nit picky, but theequals()
method forString
is actually in theString
class, not inObject
. The defaultequals()
inObject
would not compare that the contents are the same, and in fact just returns true when the reference is the same.
– Jacob Schoen
Nov 20 '12 at 17:04
14
14
not to be nit picky, but the
equals()
method for String
is actually in the String
class, not in Object
. The default equals()
in Object
would not compare that the contents are the same, and in fact just returns true when the reference is the same.– Jacob Schoen
Nov 20 '12 at 17:04
not to be nit picky, but the
equals()
method for String
is actually in the String
class, not in Object
. The default equals()
in Object
would not compare that the contents are the same, and in fact just returns true when the reference is the same.– Jacob Schoen
Nov 20 '12 at 17:04
add a comment |
up vote
48
down vote
I think that when you define a String
you define an object. So you need to use .equals()
. When you use primitive data types you use ==
but with String
(and any object) you must use .equals()
.
1
Also note that == doesn't work for char
– Khaled.K
Mar 24 '13 at 14:03
7
"char" isn't a primitive data type! It's an array of "char". And arrays aren't primitive data types theirselves.
– Christian
Mar 18 '15 at 21:02
add a comment |
up vote
48
down vote
I think that when you define a String
you define an object. So you need to use .equals()
. When you use primitive data types you use ==
but with String
(and any object) you must use .equals()
.
1
Also note that == doesn't work for char
– Khaled.K
Mar 24 '13 at 14:03
7
"char" isn't a primitive data type! It's an array of "char". And arrays aren't primitive data types theirselves.
– Christian
Mar 18 '15 at 21:02
add a comment |
up vote
48
down vote
up vote
48
down vote
I think that when you define a String
you define an object. So you need to use .equals()
. When you use primitive data types you use ==
but with String
(and any object) you must use .equals()
.
I think that when you define a String
you define an object. So you need to use .equals()
. When you use primitive data types you use ==
but with String
(and any object) you must use .equals()
.
edited Dec 17 '14 at 8:31
community wiki
2 revs, 2 users 67%
fabricioflores
1
Also note that == doesn't work for char
– Khaled.K
Mar 24 '13 at 14:03
7
"char" isn't a primitive data type! It's an array of "char". And arrays aren't primitive data types theirselves.
– Christian
Mar 18 '15 at 21:02
add a comment |
1
Also note that == doesn't work for char
– Khaled.K
Mar 24 '13 at 14:03
7
"char" isn't a primitive data type! It's an array of "char". And arrays aren't primitive data types theirselves.
– Christian
Mar 18 '15 at 21:02
1
1
Also note that == doesn't work for char
– Khaled.K
Mar 24 '13 at 14:03
Also note that == doesn't work for char
– Khaled.K
Mar 24 '13 at 14:03
7
7
"char" isn't a primitive data type! It's an array of "char". And arrays aren't primitive data types theirselves.
– Christian
Mar 18 '15 at 21:02
"char" isn't a primitive data type! It's an array of "char". And arrays aren't primitive data types theirselves.
– Christian
Mar 18 '15 at 21:02
add a comment |
up vote
44
down vote
If the equals()
method is present in the java.lang.Object
class, and it is expected to check for the equivalence of the state of objects! That means, the contents of the objects. Whereas the ==
operator is expected to check the actual object instances are same or not.
Example
Consider two different reference variables, str1
and str2
:
str1 = new String("abc");
str2 = new String("abc");
If you use the equals()
System.out.println((str1.equals(str2))?"TRUE":"FALSE");
You will get the output as TRUE
if you use ==
.
System.out.println((str1==str2) ? "TRUE" : "FALSE");
Now you will get the FALSE
as output, because both str1
and str2
are pointing to two different objects even though both of them share the same string content. It is because of new String()
a new object is created every time.
add a comment |
up vote
44
down vote
If the equals()
method is present in the java.lang.Object
class, and it is expected to check for the equivalence of the state of objects! That means, the contents of the objects. Whereas the ==
operator is expected to check the actual object instances are same or not.
Example
Consider two different reference variables, str1
and str2
:
str1 = new String("abc");
str2 = new String("abc");
If you use the equals()
System.out.println((str1.equals(str2))?"TRUE":"FALSE");
You will get the output as TRUE
if you use ==
.
System.out.println((str1==str2) ? "TRUE" : "FALSE");
Now you will get the FALSE
as output, because both str1
and str2
are pointing to two different objects even though both of them share the same string content. It is because of new String()
a new object is created every time.
add a comment |
up vote
44
down vote
up vote
44
down vote
If the equals()
method is present in the java.lang.Object
class, and it is expected to check for the equivalence of the state of objects! That means, the contents of the objects. Whereas the ==
operator is expected to check the actual object instances are same or not.
Example
Consider two different reference variables, str1
and str2
:
str1 = new String("abc");
str2 = new String("abc");
If you use the equals()
System.out.println((str1.equals(str2))?"TRUE":"FALSE");
You will get the output as TRUE
if you use ==
.
System.out.println((str1==str2) ? "TRUE" : "FALSE");
Now you will get the FALSE
as output, because both str1
and str2
are pointing to two different objects even though both of them share the same string content. It is because of new String()
a new object is created every time.
If the equals()
method is present in the java.lang.Object
class, and it is expected to check for the equivalence of the state of objects! That means, the contents of the objects. Whereas the ==
operator is expected to check the actual object instances are same or not.
Example
Consider two different reference variables, str1
and str2
:
str1 = new String("abc");
str2 = new String("abc");
If you use the equals()
System.out.println((str1.equals(str2))?"TRUE":"FALSE");
You will get the output as TRUE
if you use ==
.
System.out.println((str1==str2) ? "TRUE" : "FALSE");
Now you will get the FALSE
as output, because both str1
and str2
are pointing to two different objects even though both of them share the same string content. It is because of new String()
a new object is created every time.
edited Feb 8 at 19:33
community wiki
3 revs, 3 users 79%
Rakesh KR
add a comment |
add a comment |
up vote
41
down vote
Operator == is always meant for object reference comparison, whereas the String class .equals() method is overridden for content comparison:
String s1 = new String("abc");
String s2 = new String("abc");
System.out.println(s1 == s2); // It prints false (reference comparison)
System.out.println(s1.equals(s2)); // It prints true (content comparison)
add a comment |
up vote
41
down vote
Operator == is always meant for object reference comparison, whereas the String class .equals() method is overridden for content comparison:
String s1 = new String("abc");
String s2 = new String("abc");
System.out.println(s1 == s2); // It prints false (reference comparison)
System.out.println(s1.equals(s2)); // It prints true (content comparison)
add a comment |
up vote
41
down vote
up vote
41
down vote
Operator == is always meant for object reference comparison, whereas the String class .equals() method is overridden for content comparison:
String s1 = new String("abc");
String s2 = new String("abc");
System.out.println(s1 == s2); // It prints false (reference comparison)
System.out.println(s1.equals(s2)); // It prints true (content comparison)
Operator == is always meant for object reference comparison, whereas the String class .equals() method is overridden for content comparison:
String s1 = new String("abc");
String s2 = new String("abc");
System.out.println(s1 == s2); // It prints false (reference comparison)
System.out.println(s1.equals(s2)); // It prints true (content comparison)
edited Feb 8 at 19:20
community wiki
3 revs, 2 users 54%
sham.y
add a comment |
add a comment |
up vote
37
down vote
All objects are guaranteed to have a .equals()
method since Object contains a method, .equals()
, that returns a boolean. It is the subclass' job to override this method if a further defining definition is required. Without it (i.e. using ==
) only memory addresses are checked between two objects for equality. String overrides this .equals()
method and instead of using the memory address it returns the comparison of strings at the character level for equality.
A key note is that strings are stored in one lump pool so once a string is created it is forever stored in a program at the same address. Strings do not change, they are immutable. This is why it is a bad idea to use regular string concatenation if you have a serious of amount of string processing to do. Instead you would use the StringBuilder
classes provided. Remember the pointers to this string can change and if you were interested to see if two pointers were the same ==
would be a fine way to go. Strings themselves do not.
2
"once a string is created it is forever stored in a program at the same address" - This is flat-out wrong. Only compile-time constant string expressions (possibly involvingfinal String
variables) and strings that your program explicitly interns are stored in what you call a "lump pool". All otherString
objects are subject to garbage collection once there are no more live references to them, just like any other type of object. Also, while immutability is required for the whole interning mechanism to work, it's otherwise irrelevant to this.
– Ted Hopp
Apr 10 '14 at 18:13
String comparison is done either through equals or equalsIgnoreCase method which actually compares the contents of the string. But == sign just check the reference values. For string literals from string pool will work fine for this case. String s1 = new String("a"); String s2 = new String("a"); in this case s1==s2 is false, but s1.equals(s2) is true.
– Shailendra Singh
Jul 8 '16 at 15:31
add a comment |
up vote
37
down vote
All objects are guaranteed to have a .equals()
method since Object contains a method, .equals()
, that returns a boolean. It is the subclass' job to override this method if a further defining definition is required. Without it (i.e. using ==
) only memory addresses are checked between two objects for equality. String overrides this .equals()
method and instead of using the memory address it returns the comparison of strings at the character level for equality.
A key note is that strings are stored in one lump pool so once a string is created it is forever stored in a program at the same address. Strings do not change, they are immutable. This is why it is a bad idea to use regular string concatenation if you have a serious of amount of string processing to do. Instead you would use the StringBuilder
classes provided. Remember the pointers to this string can change and if you were interested to see if two pointers were the same ==
would be a fine way to go. Strings themselves do not.
2
"once a string is created it is forever stored in a program at the same address" - This is flat-out wrong. Only compile-time constant string expressions (possibly involvingfinal String
variables) and strings that your program explicitly interns are stored in what you call a "lump pool". All otherString
objects are subject to garbage collection once there are no more live references to them, just like any other type of object. Also, while immutability is required for the whole interning mechanism to work, it's otherwise irrelevant to this.
– Ted Hopp
Apr 10 '14 at 18:13
String comparison is done either through equals or equalsIgnoreCase method which actually compares the contents of the string. But == sign just check the reference values. For string literals from string pool will work fine for this case. String s1 = new String("a"); String s2 = new String("a"); in this case s1==s2 is false, but s1.equals(s2) is true.
– Shailendra Singh
Jul 8 '16 at 15:31
add a comment |
up vote
37
down vote
up vote
37
down vote
All objects are guaranteed to have a .equals()
method since Object contains a method, .equals()
, that returns a boolean. It is the subclass' job to override this method if a further defining definition is required. Without it (i.e. using ==
) only memory addresses are checked between two objects for equality. String overrides this .equals()
method and instead of using the memory address it returns the comparison of strings at the character level for equality.
A key note is that strings are stored in one lump pool so once a string is created it is forever stored in a program at the same address. Strings do not change, they are immutable. This is why it is a bad idea to use regular string concatenation if you have a serious of amount of string processing to do. Instead you would use the StringBuilder
classes provided. Remember the pointers to this string can change and if you were interested to see if two pointers were the same ==
would be a fine way to go. Strings themselves do not.
All objects are guaranteed to have a .equals()
method since Object contains a method, .equals()
, that returns a boolean. It is the subclass' job to override this method if a further defining definition is required. Without it (i.e. using ==
) only memory addresses are checked between two objects for equality. String overrides this .equals()
method and instead of using the memory address it returns the comparison of strings at the character level for equality.
A key note is that strings are stored in one lump pool so once a string is created it is forever stored in a program at the same address. Strings do not change, they are immutable. This is why it is a bad idea to use regular string concatenation if you have a serious of amount of string processing to do. Instead you would use the StringBuilder
classes provided. Remember the pointers to this string can change and if you were interested to see if two pointers were the same ==
would be a fine way to go. Strings themselves do not.
edited Feb 8 at 19:12
community wiki
3 revs, 3 users 50%
James
2
"once a string is created it is forever stored in a program at the same address" - This is flat-out wrong. Only compile-time constant string expressions (possibly involvingfinal String
variables) and strings that your program explicitly interns are stored in what you call a "lump pool". All otherString
objects are subject to garbage collection once there are no more live references to them, just like any other type of object. Also, while immutability is required for the whole interning mechanism to work, it's otherwise irrelevant to this.
– Ted Hopp
Apr 10 '14 at 18:13
String comparison is done either through equals or equalsIgnoreCase method which actually compares the contents of the string. But == sign just check the reference values. For string literals from string pool will work fine for this case. String s1 = new String("a"); String s2 = new String("a"); in this case s1==s2 is false, but s1.equals(s2) is true.
– Shailendra Singh
Jul 8 '16 at 15:31
add a comment |
2
"once a string is created it is forever stored in a program at the same address" - This is flat-out wrong. Only compile-time constant string expressions (possibly involvingfinal String
variables) and strings that your program explicitly interns are stored in what you call a "lump pool". All otherString
objects are subject to garbage collection once there are no more live references to them, just like any other type of object. Also, while immutability is required for the whole interning mechanism to work, it's otherwise irrelevant to this.
– Ted Hopp
Apr 10 '14 at 18:13
String comparison is done either through equals or equalsIgnoreCase method which actually compares the contents of the string. But == sign just check the reference values. For string literals from string pool will work fine for this case. String s1 = new String("a"); String s2 = new String("a"); in this case s1==s2 is false, but s1.equals(s2) is true.
– Shailendra Singh
Jul 8 '16 at 15:31
2
2
"once a string is created it is forever stored in a program at the same address" - This is flat-out wrong. Only compile-time constant string expressions (possibly involving
final String
variables) and strings that your program explicitly interns are stored in what you call a "lump pool". All other String
objects are subject to garbage collection once there are no more live references to them, just like any other type of object. Also, while immutability is required for the whole interning mechanism to work, it's otherwise irrelevant to this.– Ted Hopp
Apr 10 '14 at 18:13
"once a string is created it is forever stored in a program at the same address" - This is flat-out wrong. Only compile-time constant string expressions (possibly involving
final String
variables) and strings that your program explicitly interns are stored in what you call a "lump pool". All other String
objects are subject to garbage collection once there are no more live references to them, just like any other type of object. Also, while immutability is required for the whole interning mechanism to work, it's otherwise irrelevant to this.– Ted Hopp
Apr 10 '14 at 18:13
String comparison is done either through equals or equalsIgnoreCase method which actually compares the contents of the string. But == sign just check the reference values. For string literals from string pool will work fine for this case. String s1 = new String("a"); String s2 = new String("a"); in this case s1==s2 is false, but s1.equals(s2) is true.
– Shailendra Singh
Jul 8 '16 at 15:31
String comparison is done either through equals or equalsIgnoreCase method which actually compares the contents of the string. But == sign just check the reference values. For string literals from string pool will work fine for this case. String s1 = new String("a"); String s2 = new String("a"); in this case s1==s2 is false, but s1.equals(s2) is true.
– Shailendra Singh
Jul 8 '16 at 15:31
add a comment |
up vote
37
down vote
You can also use the compareTo()
method to compare two Strings. If the compareTo result is 0, then the two strings are equal, otherwise the strings being compared are not equal.
The ==
compares the references and does not compare the actual strings. If you did create every string using new String(somestring).intern()
then you can use the ==
operator to compare two strings, otherwise equals() or compareTo methods can only be used.
add a comment |
up vote
37
down vote
You can also use the compareTo()
method to compare two Strings. If the compareTo result is 0, then the two strings are equal, otherwise the strings being compared are not equal.
The ==
compares the references and does not compare the actual strings. If you did create every string using new String(somestring).intern()
then you can use the ==
operator to compare two strings, otherwise equals() or compareTo methods can only be used.
add a comment |
up vote
37
down vote
up vote
37
down vote
You can also use the compareTo()
method to compare two Strings. If the compareTo result is 0, then the two strings are equal, otherwise the strings being compared are not equal.
The ==
compares the references and does not compare the actual strings. If you did create every string using new String(somestring).intern()
then you can use the ==
operator to compare two strings, otherwise equals() or compareTo methods can only be used.
You can also use the compareTo()
method to compare two Strings. If the compareTo result is 0, then the two strings are equal, otherwise the strings being compared are not equal.
The ==
compares the references and does not compare the actual strings. If you did create every string using new String(somestring).intern()
then you can use the ==
operator to compare two strings, otherwise equals() or compareTo methods can only be used.
edited Feb 8 at 19:21
community wiki
3 revs, 3 users 43%
AlvaroAV
add a comment |
add a comment |
up vote
35
down vote
In Java, when the “==” operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory. In other words, it checks to see if the 2 object names are basically references to the same memory location.
The Java String class actually overrides the default equals() implementation in the Object class – and it overrides the method so that it checks only the values of the strings, not their locations in memory.
This means that if you call the equals() method to compare 2 String objects, then as long as the actual sequence of characters is equal, both objects are considered equal.
The
==
operator checks if the two strings are exactly the same object.
The
.equals()
method check if the two strings have the same value.
1
unless one of them is null, since s.equals(s2) will crash if s is null, causing the comparison to fail. Of course, this doesn't really contradict the answer; it's just a caveat.
– Jon Coombs
Jun 20 '14 at 1:05
1
No, it won't crash, it will throw a NullPointerException, causing the comparison to not take place.
– Bludzee
Mar 4 '16 at 9:51
add a comment |
up vote
35
down vote
In Java, when the “==” operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory. In other words, it checks to see if the 2 object names are basically references to the same memory location.
The Java String class actually overrides the default equals() implementation in the Object class – and it overrides the method so that it checks only the values of the strings, not their locations in memory.
This means that if you call the equals() method to compare 2 String objects, then as long as the actual sequence of characters is equal, both objects are considered equal.
The
==
operator checks if the two strings are exactly the same object.
The
.equals()
method check if the two strings have the same value.
1
unless one of them is null, since s.equals(s2) will crash if s is null, causing the comparison to fail. Of course, this doesn't really contradict the answer; it's just a caveat.
– Jon Coombs
Jun 20 '14 at 1:05
1
No, it won't crash, it will throw a NullPointerException, causing the comparison to not take place.
– Bludzee
Mar 4 '16 at 9:51
add a comment |
up vote
35
down vote
up vote
35
down vote
In Java, when the “==” operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory. In other words, it checks to see if the 2 object names are basically references to the same memory location.
The Java String class actually overrides the default equals() implementation in the Object class – and it overrides the method so that it checks only the values of the strings, not their locations in memory.
This means that if you call the equals() method to compare 2 String objects, then as long as the actual sequence of characters is equal, both objects are considered equal.
The
==
operator checks if the two strings are exactly the same object.
The
.equals()
method check if the two strings have the same value.
In Java, when the “==” operator is used to compare 2 objects, it checks to see if the objects refer to the same place in memory. In other words, it checks to see if the 2 object names are basically references to the same memory location.
The Java String class actually overrides the default equals() implementation in the Object class – and it overrides the method so that it checks only the values of the strings, not their locations in memory.
This means that if you call the equals() method to compare 2 String objects, then as long as the actual sequence of characters is equal, both objects are considered equal.
The
==
operator checks if the two strings are exactly the same object.
The
.equals()
method check if the two strings have the same value.
edited Aug 4 '14 at 0:48
community wiki
3 revs, 2 users 88%
Lijo
1
unless one of them is null, since s.equals(s2) will crash if s is null, causing the comparison to fail. Of course, this doesn't really contradict the answer; it's just a caveat.
– Jon Coombs
Jun 20 '14 at 1:05
1
No, it won't crash, it will throw a NullPointerException, causing the comparison to not take place.
– Bludzee
Mar 4 '16 at 9:51
add a comment |
1
unless one of them is null, since s.equals(s2) will crash if s is null, causing the comparison to fail. Of course, this doesn't really contradict the answer; it's just a caveat.
– Jon Coombs
Jun 20 '14 at 1:05
1
No, it won't crash, it will throw a NullPointerException, causing the comparison to not take place.
– Bludzee
Mar 4 '16 at 9:51
1
1
unless one of them is null, since s.equals(s2) will crash if s is null, causing the comparison to fail. Of course, this doesn't really contradict the answer; it's just a caveat.
– Jon Coombs
Jun 20 '14 at 1:05
unless one of them is null, since s.equals(s2) will crash if s is null, causing the comparison to fail. Of course, this doesn't really contradict the answer; it's just a caveat.
– Jon Coombs
Jun 20 '14 at 1:05
1
1
No, it won't crash, it will throw a NullPointerException, causing the comparison to not take place.
– Bludzee
Mar 4 '16 at 9:51
No, it won't crash, it will throw a NullPointerException, causing the comparison to not take place.
– Bludzee
Mar 4 '16 at 9:51
add a comment |
protected by Community♦ Jan 31 '14 at 5:20
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?
12
Also its good to know that, if you are overridding .equals () method, make sure you are overridding .hashcode () method, otherwise you will end up with violating equivalence relation b/w equals and hashcode. For more info refer java doc.
– Nageswaran
Nov 6 '13 at 12:44
Leaving a link to my explanation on why
==
works the way it does on Objects: stackoverflow.com/a/19966154/2284641– Johannes H.
Nov 14 '13 at 2:00
==
will work some of the time, as java has a String pool, where it tries to reuse memory references of commonly used strings. But==
compares that objects are equal, not the values... so.equals()
is the proper use you want to use.– James Oravec
Nov 14 '13 at 23:17
Never use == to test whether Strings are the same, unless you enjoy tracking down subtle errors and studying the intricacies of the Java String interning process.
"12"=="1"+2
is false (probably)– Flight Odyssey
Dec 23 '13 at 6:04