Computer Applications
Find the output of the following program snippet:
char c = 'B';
int i = 4;
System.out.println(c+i);
System.out.println((int)c+i);
Answer
70
70
Working
In the expression c + i
, c
is of type char
and i
is of type int
. As int
is the higher type so char
gets promoted to int
. Thus, ASCII code of 'B' which is 66 is added to 4 giving the output as 70. This is an example of implicit type conversion.
In the next expression (int)c + i
, c
which is of char
type is explicitly casted to int
. Again, ASCII code of 'B' which is 66 is added to 4 giving the output as 70. This is an example of explicit type conversion.
Related Questions
Find the output of the following program snippet:
String s= "7"; int t =Integer.parseInt(s); t=t+1000; System.out.println(t);
Find the output of the following program snippet:
char ch = 'y'; char chr = Character.toUpperCase(ch); int p = (int) chr; System.out.println(chr + "\t" + p);
Find the output of the following program snippet:
int n = 97; char ch = Character.toUpperCase((char)n); System.out.println(ch + " Great Victory");
Find the output of the following program snippet:
char c = 'A'; int n = (int) c + 32; System.out.println((char)n);