+ 2
String a = "a"; String b = "b"; if(a.equals(b)){ System.out.println("a equals b"); } in above why we wrote " a.equals(b)" i cant understand.. describe in details plz.. i m a beginner
4 Answers
+ 10
.equals() is a string method used for comparing two strings. Lot of times with if statements, you'll see == which will work with primitive data types like int, double, etc... but doesn't work with strings since it compares object references and two strings being the same word will be different objects. So .equals is used to check if one word is exactly the same as the other.
+ 4
.equals() meaning it's a case sensitive. when you write "Patrick" is not the same as "patrick". But in the .equalsIgnoreCase() this is not a case sensitive so when you write "Patrick" is exactly the same as "patrick".
+ 1
public class Program {
public static void main(String[] args) {
String a = "a";
String b = "a";
if(a==b){
System.out.println("a equals b");
}}}you can use like this also
0
its a predefined method used for comparison purpose