+ 2
can anyone explain?
public class Main { public static void main(String[] args) { for(int ch='1'; ch<='9'; ch++){ System.out.println(ch); } } }
2 Answers
+ 4
Sonam
see the Ascii table to understand this
Note: you have declared ch as int đ type
đ
for( int ch=1.......) // đ here ch is type int
so, when you write ch = '1' đ this '1' it will be taken as Ascii value because '1' is a character( đ in case of integer datatype ascii value of character is taken because you have initialized int ch with a character not with an integer so the compiler will look for integer data. in case of character, integer data is their ASCII value đ) so when you print ch you will get the character at Ascii value 1 and so on.
Ascii value 1 represents character 49
Ascii value 2 represents character 50 and so on
Note: đđđđđđ
But when you declared ch as char type đđđđ
'1' will treated as character constant so you will get
output đ
1
2
3
4
5
6
7
8
9
https://code.sololearn.com/c1p0RA4Csnq2/?ref=app
+ 1
this program output: 49
50
51
52
53
54
55
56
57