+ 2

Why the answer is false?

int a = 1; System.out.println(++a==++a);

8th May 2018, 4:28 PM
Aleksandr Efremov
Aleksandr Efremov - avatar
6 Respuestas
+ 5
Because the output comparison will be: 2==3 And that is false :)
8th May 2018, 4:39 PM
***
*** - avatar
+ 3
In Java there are two ways of comparison for Strings. You use: ==, this means you check if (a+b) is the same object in the memory of your program as c. This is not the case so it will return false. .equals, this is what you should have used to compare to Strings if they contain the same text. (a+b).equals (c) would have returned true. :)
8th May 2018, 4:50 PM
***
*** - avatar
+ 1
But, why in this case false again? String a = "a"; String b = "b"; String c = "ab"; System.out.println((a+b)==c); }} //Output: false
8th May 2018, 4:43 PM
Aleksandr Efremov
Aleksandr Efremov - avatar
+ 1
thanks, got it!
8th May 2018, 4:51 PM
Aleksandr Efremov
Aleksandr Efremov - avatar
0
Exactly! Thank you!
8th May 2018, 4:41 PM
Aleksandr Efremov
Aleksandr Efremov - avatar
0
what about this case? public class Program { public static void main(String[] args) { String a = "text"; String b = "text"; System.out.println(a==b); } }// Output: true
8th May 2018, 9:32 PM
Aleksandr Efremov
Aleksandr Efremov - avatar