This question already has an answer here:
-
Equal strings aren't equal (==) in Java? [duplicate]
3 answers
I'm a pretty new programmer and as part of a few projects I'm doing I'm using substrings. In all of these I've run across a problem with if statements. Say I have two variable, one that is a substring of some other string, and another that should be the equivalent to that first substring. If I try to check if they are equal in an if statement (which they should be) it returns false for and == check. Here's a bit of code to demonstrate.
public class Substringproblem{
static void method(){
String random="somestring";
String randomsubstring=random.substring(0,3);
String should_be_randomsubstring="som";
if(randomsubstring==should_be_randomsubstring){
System.out.println("Success");
}else{
System.out.println("Failure");
System.out.println(randomsubstring);
}
}
public static void main(String[] args) {
method();
}
}
I get the feeling I'm missing something really obvious but I've been looking for a bit and haven't spotted anything that is like this. Help would be really appreciated.
randomsubstring.equals(should_be_randomsubstring)
or
randomsubstring.equalsIgnoreCase(should_be_randomsubstring)(If you want to ignore cae use this)
instead of
randomsubstring==should_be_randomsubstring)
whenever you are comparing string use .equals()
or .equalsIgnoreClas()
randomsubstring.equals(should_be_randomsubstring)
You are comparing String object references.
Use the String.equals(String anotherString)
function to compare strings, not the == operator. The function checks the actual contents of the string, the == operator checks whether the references to the objects are equal.
public class SubstringProblem {
public static void main(String[] args) {
method();
}
static void method() {
String random = "somestring";
String randomsubstring = random.substring(0,3);
String should_be_randomsubstring = "som";
if (randomsubstring.equals(should_be_randomsubstring)) {
System.out.println("Success");
} else {
System.out.println("Failure");
System.out.println(randomsubstring);
}
}
}
in java for comparing two Strings always use equals()
method.
your code should look like :
if(randomsubstring.equals(should_be_randomsubstring)){
System.out.println("Success");
}else{
System.out.println("Failure");
System.out.println(randomsubstring);
- ‘==’ operator does not compare the content of the text present in the String objects. It only compares the references the 2 Strings are pointing to i.e. the Object references. hence in your case it will return false.
- The
equals()
method is used when you need to compare the content of the text present in the String objects. This method returns true when two String objects hold the same content.
You should use equals
to compare String object.
According to your mean code change:
if(should_be_randomsubstring.equals(randomsubstring)){
System.out.println("Failure")
}else{
System.out.println("Success");
}
Consider you have single String object in heap memory then you can have multiple reference variable pointing to same object.
Ex. String ref1 = "Test";
String ref2 = ref1;
String ref3 = ref1;
So String ref1, ref2 and ref3 are pointing to same String "Test"
. So to check whether those references equals or not, we have to use ==
operator. which will return corresponding value.
(equals will also return true
but its purpose is different)
Equals method of the String checks actual contents of the String object.
so in above case if I create
String ref4= new String("Test");
ref1 is not equal to ref, == operator will return false, because both objects having different existence in heap memory.
and if I compare both object by equals
method then it will return true
because contents are true.
Imp Note for memory leak in substring
Also dig down into substring method which cause memory leak
if we don't use it judiciously.
String's substring method uses old string's value[]
array object to store substring.
Ex. You have a String say, 1 GB, and if make a substring of it with 500 MB then it will return 500 MB character set with 1 GB value[]
array object. It means still you have 1 GB value[] array object which contains 500 MB of data. So cause memory leak.
How to avoid?
String some1 = new String(some2.substring(5)); // use such call
String someStr = some2.substring(5); //don't use this way