2.3 Radix (Base)
Computers are electronic devices capable of processing only 0s and 1s. However, in our daily lives, we use the base-10 (Decimal) system. Therefore, to understand variables more deeply, it is important to know how computers store and read values.
1. Understanding Number Bases
Base-10 (Decimal): Represents numbers using ten digits from 0 to 9. Base-2 (Binary): Represents numbers using only two digits, 0 and 1. This is the format computers use. Base-8 (Octal): Uses eight digits, from 0 to 7. (Rarely used recently) Base-16 (Hexadecimal): Represents numbers using the ten digits 0 to 9 plus the six letters A through F (A=10, F=15).
graph TD
A[Decimal: 15] --> B[Binary: 1111]
A[Decimal: 15] --> C[Octal: 17]
A[Decimal: 15] --> D[Hexadecimal: F]
2. Using Various Base Literals in Java
Within Java source code, you can store binary, octal, and hexadecimal literals in variables. Java designates values with special prefixes to distinguish between different bases.
- Decimal: No prefix
- Binary: Prefix
0bor0B - Octal: Prefix
0 - Hexadecimal: Prefix
0xor0X
Examples of Using Base Literals
int decNum = 10; // Decimal format (Value: 10)
int binNum = 0b1010; // Binary format (Value: 10)
int octNum = 012; // Octal format (Value: 10)
int hexNum = 0xA; // Hexadecimal format (Value: 10)
System.out.println(decNum);
System.out.println(binNum); // All output internally as decimal
System.out.println(octNum);
System.out.println(hexNum);
3. Bit and Byte
- bit: The most basic unit of data for a computer. Either 0 or 1.
- byte: A unit of 8 bits combined.
1 byte = 8 bit.
The smallest unit among Java's integer types is 1 byte (the byte type), which can store values ranging from -128 to 127. Using a 2-digit hexadecimal number clearly represents a value of exactly 1 byte, making it commonly used. (0xFF == 255)