+ 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 ответов
+ 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