Please explain me, output of this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Please explain me, output of this code?

public class Program { public static void main(String[] args) { String s1="java"; String s2="java"; String s3="JAVA"; int i=s1.compareTo(s2); int j=s2.compareTo(s3); int k=s3.compareTo(s2); System.out.println(i); System.out.println(j); System.out.println(k); } } //output /* o 32 -32 */

2nd May 2018, 5:33 AM
Tilanka Dilshan Senadheera
Tilanka Dilshan Senadheera - avatar
2 Answers
+ 2
ASCII value of 'j': 106 ASCII value of 'J': 74 first output is 0 because s1 == s2. second output is positive because s2 > s3. third output is negative because s3 < s2. You get 32 because the first different letters in s2 and s3 are 'j' and 'J', and the difference between them is 106-74 = 32. also read this: https://www.javatpoint.com/java-string-compareto
2nd May 2018, 6:11 AM
Hanz
Hanz - avatar
0
Thank You very much for tha answer.
2nd May 2018, 7:24 AM
Tilanka Dilshan Senadheera
Tilanka Dilshan Senadheera - avatar