0

Whata different between if(password.equals("123456") and if(password=="123456") and when should use ==

11th Jan 2017, 1:12 PM
Mahmood Ali
Mahmood Ali - avatar
5 Answers
+ 2
equals is a method in Sting class which compares the exact characters where as == compares address of the two objects
11th Jan 2017, 6:30 PM
Kallanagouda Biradar
+ 1
can anyone give me example for == compare?.
11th Jan 2017, 7:42 PM
Mahmood Ali
Mahmood Ali - avatar
+ 1
== is used to compare int, double... while equals is used to compare strings. e.g.: String a = "abc"; if(a.equals("abc")){ System.out.println("equals"); } else{ System.out.println("not equal"); } Output: equals e.g.: int a = 123; if(a==123){ System.out.println("equals"); } else{ System.out.println("not equal"); } Output: equals not sure of the reason but if == is used to compare strings, it will give inaccurate results
12th Jan 2017, 1:55 AM
lowshuen
+ 1
==compare the reference 😉
13th Jan 2017, 8:28 AM
Ali Hasan
Ali Hasan - avatar