DataTypeTest
Boolean
Char
String
Numeric Type: byte, short, int, long, float, double
Type Conversion
Enum
Permgen vs Metaspace in Java
https://www.baeldung.com/java-permgen-metaspace
Permgen (JDK7 이전)
Metaspace (JDK8 이후)
클래스의 정보가 로드되는 영역, 정적 멤버 (static member) 저장, 바이트코드나 JIT 정보 저장
Stack Memory and Heap Space in Java
Int String Conversion
//http://javadevnotes.com/java-integer-to-string-examples
// int => String type conversion
int i = -10; // -10
String s1 = Integer.toString(i); // "-10"
String s2 = String.valueOf(i); // "-10"
String s3 = new Integer(i).toString(); // "-10"
String s4 = String.format("%d", i); // "-10"
System.out.println("s1 = " + s1);
System.out.println("s2 = " + s2);
System.out.println("s3 = " + s3);
System.out.println("s4 = " + s4);
//http://javadevnotes.com/java-string-to-integer-examples
// String => int type conversion
String x = "-123"; // "-123"
int y = Integer.parseInt(x); // -123 (integer로 변환이 가능한 경우만 변환 가능 그 외엔 run-time exception error)
int z = Integer.valueOf(x); // -123 (integer로 변환이 가능한 경우만 변환 가능 그 외엔 run-time exception error)
int w = new Integer(x).intValue(); // 123 (integer로 변환이 가능한 경우만 변환 가능 그 외엔 run-time exception error)
System.out.println("x = " + x);
System.out.println("y = " + y);
System.out.println("z = " + z);
System.out.println("w = " + w);
Numeric Data Type Conversion
byte bVal = 127;
int iVal = 100;
System.out.println(bVal+iVal); // 127 + 100 = 227
System.out.println(10/4); // int/ int 2.5 => 2
System.out.println(10.0/4); // double / int => double divided by double => 2.5
System.out.println((char)0x12340041);
System.out.println((byte)(bVal+iVal)); // 127 + 100 => (byte)227 => 1110 0011 => 1 + 2 + 32 + 64 – 128 => -29
System.out.println((int)2.9 + 1.8); // 2 + 1.8 => 2.0 + 1.8 => 3.8
System.out.println((int)(2.9 + 1.8)); // (int)4.1 => 4
System.out.println((int)2.9 + (int)1.8); // 2 + 1 => 3
Implicit/Explicit Type Conversion
//Implicit type conversion vs Explicit type conversion
int intVal = 32555;
byte byteVal = 25;
long longVal = intVal; // implicit type conversion
System.out.println(longVal); // 32555
intVal = (int) longVal; // explicit type conversion
System.out.println(intVal); // 32555
longVal = byteVal; // implicit type conversion
System.out.println(longVal); // 25
byteVal = (byte) longVal; // explicit type conversion
System.out.println(byteVal); // 25
Java’s byte (-128 to 127)
Java’s byte
is a signed 8-bit numeric type whose range is -128
to 127
(JLS 4.2.1). 233
is outside of this range; the same bit pattern represents -23
instead.
11101001 = 1 + 8 + 32 + 64 + 128 = 233 (int)
1 + 8 + 32 + 64 - 128 = -23 (byte)
A byte plus a byte is an int in Java.
byte a = 1;
byte b = 2;
byte c = a + b; // compile error (byte + byte = int)
byte c = (byte)(a + b); // corrected
Postfix vs Prefix Increment/Decrement Operator
int i = 10;
System.out.println(“i++=” + (i++)); // prints 10 and then increments
System.out.println(“i=” + i); // prints 11
int j = 20;
System.out.println(“++j=” + (++j)); // increments and then prints 21
System.out.println(“j=” + j); // prints 21
Variables
instance variable vs static variable vs local variable
public class Hello {
private String name; // instance variable
private static int value = 10; // static variable
public int sum(int n, int m) {
int result = n + m; // result : local variable
return result;
}
public static void main(String args[]) {
Hello h = new Hello();
h.name = “Hello”;
System.out.println(h.name);
System.out.println(Hello.value);
}
}
Operator Precedence
https://docs.oracle.com/javase/tutorial/java/nutsandbolts/operators.html
Operators | Precedence |
---|---|
postfix | expr++ expr-- |
unary | ++expr --expr +expr -expr ~ ! |
multiplicative | * / % |
additive | + - |
shift | << >> >>> |
relational | < > <= >= instanceof |
equality | == != |
bitwise AND | & |
bitwise exclusive OR | ^ |
bitwise inclusive OR | | |
logical AND | && |
logical OR | || |
ternary | ? : |
assignment | = += -= *= /= %= &= ^= |= <<= >>= >>>= |