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 | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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

28th Jul 2016, 7:43 AM
Niki Patra
Niki Patra - avatar
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.
28th Jul 2016, 7:51 AM
James
James - avatar
+ 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".
28th Jul 2016, 11:00 AM
Jv Lauriano
Jv Lauriano - avatar
+ 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
31st Jul 2016, 2:40 AM
Aravind
0
its a predefined method used for comparison purpose
20th Nov 2016, 8:41 PM
vaibhav singh
vaibhav singh - avatar