+ 1
Is there a method in Java to test if two strings' values are equal?
9 Answers
+ 3
The String equals() method comparesthe original content of the string. Itcompares values of string for equality.String class provides two methods: public boolean equals(Object another) compares this string to the specified object.
+ 4
String s = "Straight"; Â
String t = "straight"; System.out.println(s.equals(t));
System.out.println(s.equalsIgnoreCase(t));
If you run the above program you will get the following result:
false
true
The first one will return false because of the first letterâs case. equals() doesnât takes cases seriously. To curb that issue we have equalsIgnoreCase.
= = Operator
If you have references to compare = = operator comes in handy. Remember it doesnât compare values.
String s = "Straight"; Â
String t = "straight";Â
String m = new String("Straight");Â
System.out.println(s == t);Â
System.out.println(s == m);
In the above code, line 2 will try to find the instance available in the String Pool thus referencing itself to âsâ. Both will become equal, and hence the operator result for it will turn out to be true. For the second sysout display the result will be false because m has become a different instance in the heap. Hereâs the result of the above program:
true
false
+ 3
Do we need to create an object to use the method?
+ 3
Paul Grasser (string1 == string2) returns "String cannot be converted into boolean"...
+ 2
go through this it will give you detailed information
https://stackoverflow.com/questions/13001971/using-the-equals-method-with-string-and-object-in-java
+ 1
if (string1 == string2){
//Code to be executed
}
0
See this code, it shows the diffetence between == and equals()
https://code.sololearn.com/c79tfjeZkWvs/?ref=app
0
str.equals