Java 8 Primitive Data Types & Literals

โ˜• Java 21+ LTS ๐ŸŸข Chapter 7 of 47 ๐Ÿ“‚ Phase 2: Variables & Data Types ๐Ÿ“… 2026 Edition
๐Ÿ“Œ Covered in this chapter:

byte, short, int, long ยท float, double ยท char (Unicode UTF-16) ยท boolean ยท Memory Sizes & Bit Ranges ยท Numeric Underscores & Literals

Deep dive into the 8 primitive data types of Java: exact byte sizes, minimum/maximum mathematical ranges, IEEE 754 floating-point standards, 16-bit Unicode characters, boolean logic, and modern numeric literal formatting (binary, hex, and numeric underscores).

1. The 8 Primitive Data Types Complete Table

Java has exactly 8 Primitive Data Types built directly into the language syntax for maximum hardware performance:

Data TypeCategorySize (Bytes)Size (Bits)Minimum ValueMaximum ValueDefault ValueExample Literal
**`byte`**
Integer | 1 byte | 8 bits | -128 ($-2^7$) | 127 ($2^7 - 1$) | 0 | byte b = 100; | | short | Integer | 2 bytes | 16 bits | -32,768 ($-2^{15}$) | 32,767 ($2^{15} - 1$) | 0 | short s = 25000; | | int | Integer | 4 bytes | 32 bits | -2,147,483,648 ($-2^{31}$) | 2,147,483,647 ($2^{31} - 1$) | 0 | int i = 500000; | | long | Integer | 8 bytes | 64 bits | -9,223,372,036,854,775,808 ($-2^{63}$) | 9,223,372,036,854,775,807 ($2^{63} - 1$) | 0L | long l = 9876543210L; | | float | Floating Point | 4 bytes | 32 bits | $\approx 1.4 \times 10^{-45}$ | $\approx 3.4028235 \times 10^{38}$ (6-7 decimal digits precision) | 0.0f | float f = 3.14159f; | | double | Floating Point | 8 bytes | 64 bits | $\approx 4.9 \times 10^{-324}$ | $\approx 1.7976931 \times 10^{308}$ (15-16 decimal digits precision) | 0.0d | double d = 3.1415926535; | | char | Character | 2 bytes | 16 bits | '\u0000' (0) | '\uffff' (65,535 unsigned Unicode UTF-16) | '\u0000' | char c = 'A'; | | boolean | Truth Value | 1 bit (JVM dependent) | 1 bit | false | true | false | boolean flag = true; |

2. Integer Types: Why `int` is Default & The `long` 'L' Suffix

In Java, every whole integer literal (e.g. 100, 5000) is treated by default as a 32-bit int.

If a number exceeds the maximum 32-bit limit (2,147,483,647), you must append an uppercase L or lowercase l suffix (always use uppercase L to avoid confusing l with the digit 1):

int standardNumber = 2000000;
long worldPopulation = 8000000000L; // Mandatory 'L' suffix

3. Floating-Point: `float` ('F' Suffix) vs `double` (Default)

In Java, every fractional decimal literal (e.g. 3.14, 99.99) is treated by default as a 64-bit double (IEEE 754 standard).

If you want to store a decimal in a 32-bit float to conserve memory in graphics/game engines, you must append an F or f suffix:

double exactGpa = 3.95;    // 64-bit standard default
float screenCoord = 120.5f; // Mandatory 'f' suffix

4. `char` and Unicode UTF-16 Support

Unlike C/C++ where char is only 1 byte (ASCII only, 0-127), Java's char is 2 bytes (16-bit unsigned Unicode). This allows Java to represent characters from every human language (English, Telugu, Hindi, Chinese, Arabic) and international symbols natively!

char englishChar = 'A';
char unicodeChar = '\u0C05'; // Telugu letter 'เฐ…'
char copyright   = '\u00A9'; // ยฉ Symbol
char asciiCode   = 65;       // Storing numeric ASCII 65 yields 'A'

5. Modern Java Literals: Underscores, Binary, and Hexadecimal

Java 7+ introduced expressive literal formats to improve code readability:

1. Underscores in Numbers: Group large numbers visually (the compiler strips underscores automatically):

long creditCardNumber = 4123_5678_9012_3456L;
int oneMillion = 1_000_000;
double nationalBudget = 4_500_000.75;

2. Binary Literals (0b prefix):
int binaryByte = 0b1010_1100; // Decimal: 172

3. Hexadecimal Literals (0x prefix):
int hexColor = 0xFF_57_33; // RGB Hex Color

Beginner Example & Code Anatomy

โ˜• Main.java โ€” Chapter 7 Core Example
public class Main {
    public static void main(String[] args) {
        // 1. Integer Types
        byte  serverPortSmall = 80;
        short companyOffices  = 1250;
        int   cityPopulation  = 2_500_000; // Underscore for readability
        long  globalDataBytes = 9_876_543_210_000L; // 'L' suffix

        // 2. Floating-Point Types
        float  fuelLevelPercent = 87.5f; // 'f' suffix
        double precisePI        = 3.141592653589793;

        // 3. Character & Unicode
        char letterAlpha = 'J';
        char teluguVowel = '\u0C05'; // Telugu 'เฐ…'
        char symbolRupee = 'โ‚น';

        // 4. Boolean
        boolean isServerOnline = true;

        // Output all primitives
        System.out.println("--- Java 8 Primitive Types Demonstration ---");
        System.out.println("byte    (8-bit)  : " + serverPortSmall);
        System.out.println("short   (16-bit) : " + companyOffices);
        System.out.println("int     (32-bit) : " + cityPopulation);
        System.out.println("long    (64-bit) : " + globalDataBytes);
        System.out.println("float   (32-bit) : " + fuelLevelPercent + "%");
        System.out.println("double  (64-bit) : " + precisePI);
        System.out.println("char    (Unicode): " + letterAlpha + " | " + teluguVowel + " | " + symbolRupee);
        System.out.println("boolean (Truth)  : " + isServerOnline);
    }
}
๐Ÿ’ป Program Console Output
--- Java 8 Primitive Types Demonstration --- byte (8-bit) : 80 short (16-bit) : 1250 int (32-bit) : 2500000 long (64-bit) : 9876543210000 float (32-bit) : 87.5% double (64-bit) : 3.141592653589793 char (Unicode): J | เฐ… | โ‚น boolean (Truth) : true

๐Ÿ” Line-by-Line Code Explanation

int cityPopulation = 2_500_000;

Underscores enhance readability for large numeric literals without affecting compiled value.

long globalDataBytes = 9_876_543_210_000L;

The L suffix forces the compiler to treat this 64-bit literal as long instead of 32-bit int.

float fuelLevelPercent = 87.5f;

The f suffix prevents compile error by explicitly designating a 32-bit single-precision float.

char teluguVowel = '\u0C05';

2-byte Unicode escape sequence representing the Telugu alphabet vowel character 'เฐ…'.

Practical Real-World Example

โ˜• PracticalApplication.java โ€” Industry Implementation
public class ScientificDataDemo {
    public static void main(String[] args) {
        // Querying Primitive Type Limits using Built-in Wrapper Constants
        System.out.println("=== Primitive Type Maximum & Minimum Bounds ===");
        System.out.println("Byte Range   : " + Byte.MIN_VALUE + " to " + Byte.MAX_VALUE);
        System.out.println("Short Range  : " + Short.MIN_VALUE + " to " + Short.MAX_VALUE);
        System.out.println("Integer Range: " + Integer.MIN_VALUE + " to " + Integer.MAX_VALUE);
        System.out.println("Long Range   : " + Long.MIN_VALUE + " to " + Long.MAX_VALUE);
        System.out.println("Float Min/Max: " + Float.MIN_VALUE + " to " + Float.MAX_VALUE);
        System.out.println("Double Bounds: " + Double.MIN_VALUE + " to " + Double.MAX_VALUE);
    }
}
๐Ÿ’ป Practical Console Output
=== Primitive Type Maximum & Minimum Bounds === Byte Range : -128 to 127 Short Range : -32768 to 32767 Integer Range: -2147483648 to 2147483647 Long Range : -9223372036854775808 to 9223372036854775807 Float Min/Max: 1.4E-45 to 3.4028235E38 Double Bounds: 4.9E-324 to 1.7976931348623157E308
โš ๏ธ Common Mistakes & Professional Best Practices
  • Omitting the 'L' on large numbers: "long x = 5000000000;" fails with "integer number too large" because the literal is evaluated as int before assignment. Use "5000000000L".
  • Omitting the 'f' on float decimals: "float pi = 3.14;" fails compilation with "possible loss of precision". Use "3.14f".
  • Treating boolean as 1 or 0: In C++, 1 is true and 0 is false. In Java, boolean is strictly true or false. Writing "boolean flag = 1;" fails compilation.
๐ŸŽฏ Hands-on Coding Challenge

Test your understanding by writing the code directly in your editor or running in our online Java compiler:

โ˜• Challenge.java
// Coding Challenge:
// Write a program that demonstrates all 4 integer types with their exact max limits:
// 1. byte b = Byte.MAX_VALUE;
// 2. short s = Short.MAX_VALUE;
// 3. int i = Integer.MAX_VALUE;
// 4. long l = Long.MAX_VALUE;
// Print their values and calculate the sum (l + i).

public class Main {
    public static void main(String[] args) {
        // TODO: Declare and print the primitive max values
        
    }
}

๐Ÿ’ก Frequently Asked Questions & Interview Insights

โ“ Why does Java use 16-bit char instead of 8-bit like C?

Java was designed for global internet applications. 8-bit ASCII can only represent 256 characters (English alphabet and basic punctuation), whereas 16-bit Unicode supports over 65,000 international characters natively.

โ“ Should I use float or double for monetary transactions?

Neither! Floating point types (float/double) suffer from binary rounding inaccuracies (e.g. 0.1 + 0.2 != 0.3). Always use "java.math.BigDecimal" for financial, banking, and e-commerce calculations.

โ“ Can underscores be placed at the start or end of a number?

No! Underscores can only be placed between digits (e.g. 1_000). Writing "_100" or "100_" causes a compile-time syntax error.

๐Ÿš€ Quick Chapter Recap

  • Java provides 8 primitives: byte (1B), short (2B), int (4B), long (8B), float (4B), double (8B), char (2B), and boolean.
  • Integer literals default to int (use 'L' for long); decimal literals default to double (use 'f' for float).
  • char uses 2-byte Unicode UTF-16 in single quotes ('A').
  • Use underscores (e.g. 1_000_000) for clean numeric readability.
โ† Prev: 6. Variables & Memory Next: 8. Reference Types & Scopes โ†’
OC
Curated by Our Compiler Java Technical Editorial Team
Published for 2026 Academic & Enterprise Reference ยท 100% Free & Open Access