Why the output is "Your grade is = 67" when i declare grade as 'int'? | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 2

Why the output is "Your grade is = 67" when i declare grade as 'int'?

public class Test { public static void main(String args[]) { int grade = 'C'; switch(grade) { case 'A' : System.out.println("Excellent!"); break; case 'B' : case 'C' : System.out.println("Well done"); break; case 'D' : System.out.println("You passed"); case 'F' : System.out.println("Better try again"); break; default : System.out.println("Invalid grade"); } System.out.println("Your grade is = " + grade); } } If grade is 'char' the output is 'your grade is C'.

15th Dec 2018, 8:08 AM
Nitin Gutte
Nitin Gutte - avatar
2 Réponses
+ 8
int grade = 'C' // it initializes the variable grade to the ascii value of the character 'C', which is 67 Each character has a corresponding ascii value. Check the link below. https://www.cs.cmu.edu/~pattis/15-1XX/common/handouts/ascii.html
15th Dec 2018, 8:23 AM
Lambda_Driver
Lambda_Driver - avatar
+ 2
First of all the compiler throws an type error in the intialization of grade. and as you said it will print ' your grade is c' if you have declared appropriate data type for grade. else there will be no compilation of the code(to bytecode).
15th Dec 2018, 8:23 AM
Adarsh.n. Bidari
Adarsh.n. Bidari - avatar