Magic numbers

Nov 16, 2021 | - views

Java

Label Size Range
Java Byte 1 byte -128 to 127
Java Short 2 byte -3.2 * 10^4 to 3.2 * 10^4
Java Int 4 byte -2 * 10^9 to 2 * 10^9
Java Long 8 byte -9 * 10^18 to 9 * 10^18
Java Float 4 byte Sufficient for storing 6 to 7 decimal digits
Java Double 8 byte Sufficient for storing 15 decimal digits
Java Boolean 1 bit
Java Char 2 bytes
# Widening Casting (automatically) - converting a smaller type to a larger type size
byte -> short -> char -> int -> long -> float -> double
# Narrowing Casting (manually) - converting a larger type to a smaller size type
double -> float -> long -> int -> char -> short -> byte
        

Latency

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs
Read 1 MB sequentially from memory ..... 250,000 ns  = 250 µs
Round trip within same datacenter ...... 500,000 ns  = 0.5 ms
Read 1 MB sequentially from SSD* ..... 1,000,000 ns  =   1 ms
Disk seek ........................... 10,000,000 ns  =  10 ms
Read 1 MB sequentially from disk .... 20,000,000 ns  =  20 ms
Send packet CA->Netherlands->CA .... 150,000,000 ns  = 150 ms