java - What char variable really is? -
both statements valid:
char c1 = 'a'; char c2 = 97;
now, if add char variable, lets say:
char c3 = 10;
and sum c2 , c3 so:
int sum = c2 + c3
i not compile error , why that? , why need ever sum 2 chars?
thanks
each character represented type 'char' in java mapped number in ascii table. when add values adding operands numeric equivalent (the char types cast int) x (120 ascii val) - p(80 ascii val) give integer value 40.
dec char dec char dec char dec char --------- --------- --------- ---------- 0 nul (null) 32 space 64 @ 96 ` 1 soh (start of heading) 33 ! 65 97 2 stx (start of text) 34 " 66 b 98 b 3 etx (end of text) 35 # 67 c 99 c 4 eot (end of transmission) 36 $ 68 d 100 d 5 enq (enquiry) 37 % 69 e 101 e 6 ack (acknowledge) 38 & 70 f 102 f 7 bel (bell) 39 ' 71 g 103 g 8 bs (backspace) 40 ( 72 h 104 h 9 tab (horizontal tab) 41 ) 73 105 10 lf (nl line feed, new line) 42 * 74 j 106 j 11 vt (vertical tab) 43 + 75 k 107 k 12 ff (np form feed, new page) 44 , 76 l 108 l 13 cr (carriage return) 45 - 77 m 109 m 14 (shift out) 46 . 78 n 110 n 15 si (shift in) 47 / 79 o 111 o 16 dle (data link escape) 48 0 80 p 112 p 17 dc1 (device control 1) 49 1 81 q 113 q 18 dc2 (device control 2) 50 2 82 r 114 r 19 dc3 (device control 3) 51 3 83 s 115 s 20 dc4 (device control 4) 52 4 84 t 116 t 21 nak (negative acknowledge) 53 5 85 u 117 u 22 syn (synchronous idle) 54 6 86 v 118 v 23 etb (end of trans. block) 55 7 87 w 119 w 24 can (cancel) 56 8 88 x 120 x 25 em (end of medium) 57 9 89 y 121 y 26 sub (substitute) 58 : 90 z 122 z 27 esc (escape) 59 ; 91 [ 123 { 28 fs (file separator) 60 < 92 \ 124 | 29 gs (group separator) 61 = 93 ] 125 } 30 rs (record separator) 62 > 94 ^ 126 ~ 31 (unit separator) 63 ? 95 _ 127 del
https://www.cs.cmu.edu/~pattis/15-1xx/common/handouts/ascii.html
this prints numbers 32-128 , associated char:
for (int c=32; c<128; c++) { system.out.println("ascii #: " + c + " char" + ": " + (char)c); }
Comments
Post a Comment