+ 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'.
2 Antworten
+ 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
+ 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).