Implicit | Explicit | |
---|---|---|
Primitive | lower -> higher | higher -> lower |
Reference | child -> parent (upcasting) | parent -> child (downcasting) |
When explicit type casting beyond the data range
int a = 130;
byte z = (byte) z;//value is -126
Because JVM store the value in a circle, the largest byte range is 127. It will keep going until 130 steps which is -126.
Primitives Type Conversion
- implicit casting
- explicit casting
- boolean casting
1. Implicit Casting (widening conversion)
This is done implicitly by the JVM. The lower size is widened to higher size. This is also named as automatic type conversion.
int x = 10; // occupies 4 bytes
double y = x; // occupies 8 bytes
System.out.println(y); // prints 10.0
2. Explicit Casting (narrowing conversion)
A data type of higher size (occupying more memory) cannot be assigned to a data type of lower size. This is not done implicitly by the JVM and requires explicit casting.
double x = 10.5;
int y = (int) x;
3. Boolean Casting
A boolean value cannot be assigned to any other data type. Boolean is incompatible for conversion.
chars (2 bytes) | int (4 bytes) | double (8 bytes) | String | |
---|---|---|---|---|
chars to | ////// | 65 --> A, ASCII | String.valueOf() | |
Character.getNumericValue() | ||||
int to | /////// | implicit casting | String.valueOf(int) | |
double to | not common | (int) val | ////// | |
String to | s.toCharArray(); | Integer.parseInt(s) | Double.parseDouble() | /////// |
chars to int (explicit casting)
char c = 'A';
int i = c;// 65
chars to double
char c = 'A';
double i = c;// 65.0
chars to String
String.valueOf(char);
int to chars
int i = 65;
char c = (char) i; //A
reference: https://way2java.com/casting-operations/data-type-casting-type-conversion/
The following list gives some of the most usefulCharacter
comparison methods. TheCharacter
API documentation fully specifies the methods.
isDigit
isLetter
isLetterOrDigit
isLowerCase
isUpperCase
isSpaceChar
ASCII Table
Upper Case
for (int asci = 65; asci <= 90; asci++)
Lower Case
for (int asci = 97; asci <= 122; asci++)
@page { margin: 0.79in }
pre.cjk { font-family: "Nimbus Mono L", monospace }
h2.cjk { font-family: "AR PL UKai CN" }
h2.ctl { font-family: "Lohit Devanagari" }
p { margin-bottom: 0.1in; line-height: 120% }
ASCII Table
Dec = Decimal Value
Char = Character
'5' has the int value 53
if we write '5'-'0' it evaluates to 53-48, or the int 5
if we write char c = 'B'+32; then c stores 'b'
Dec Char Dec Char Dec Char Dec Char
--------- --------- --------- ----------
0 NUL (null) 32 SPACE 64 @ 96 `
1 SOH (start of heading) 33 ! 65 A 97 a
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 I 105 i
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 SO (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 US (unit separator) 63 ? 95 _ 127 DEL