Bird
Raised Fist0
Javaprogramming~5 mins

Primitive data types in Java

Choose your learning style10 modes available
Introduction

Primitive data types store simple values like numbers and letters. They are the basic building blocks for data in Java.

When you need to store whole numbers like age or count.
When you want to store decimal numbers like price or temperature.
When you need to store a single character like a letter or symbol.
When you want to store true or false values for conditions.
When you want to save memory by using simple data types.
Syntax
Java
public class PrimitiveDataTypes {
    byte aByte;       // 8-bit integer
    short aShort;     // 16-bit integer
    int anInt;        // 32-bit integer
    long aLong;       // 64-bit integer
    float aFloat;     // 32-bit decimal
    double aDouble;   // 64-bit decimal
    char aChar;       // 16-bit Unicode character
    boolean aBoolean; // true or false
}

Each primitive type has a fixed size and stores a simple value.

Primitive types are not objects and do not have methods.

Examples
Different integer types store numbers of different sizes. Use L suffix for long literals.
Java
byte smallNumber = 100;
short mediumNumber = 30000;
int largeNumber = 1000000;
long veryLargeNumber = 10000000000L;
Use f suffix for float literals. Double is more precise than float.
Java
float decimalNumber = 3.14f;
double preciseDecimal = 3.1415926535;
Characters are enclosed in single quotes. Boolean stores true or false.
Java
char letter = 'A';
boolean isJavaFun = true;
Zero is a valid integer. Boolean can only be true or false, never numbers.
Java
int zero = 0;
boolean isZero = false;
Sample Program

This program shows how to declare and use all primitive data types in Java. It prints their values to the screen.

Java
public class PrimitiveDataTypesDemo {
    public static void main(String[] args) {
        byte age = 25;
        short year = 2024;
        int population = 1000000;
        long distanceToMoon = 384400000L;
        float price = 19.99f;
        double pi = 3.1415926535;
        char grade = 'A';
        boolean isJavaFun = true;

        System.out.println("Age: " + age);
        System.out.println("Year: " + year);
        System.out.println("Population: " + population);
        System.out.println("Distance to Moon (meters): " + distanceToMoon);
        System.out.println("Price: $" + price);
        System.out.println("Pi: " + pi);
        System.out.println("Grade: " + grade);
        System.out.println("Is Java fun? " + isJavaFun);
    }
}
OutputSuccess
Important Notes

Time complexity is not applicable because these are simple data holders.

Primitive types use fixed memory sizes, so they are fast and efficient.

Common mistake: Using float without f suffix causes errors.

Use primitive types when you need simple values and want better performance than objects.

Summary

Primitive data types store simple values like numbers, characters, and true/false.

They have fixed sizes and are not objects.

Use the right type based on the size and kind of data you want to store.

Practice

(1/5)
1. Which of the following is NOT a primitive data type in Java?
easy
A. char
B. boolean
C. int
D. String

Solution

  1. Step 1: Recall Java primitive types

    Java primitive types include int, boolean, char, byte, short, long, float, and double.
  2. Step 2: Identify non-primitive type

    String is a class in Java, not a primitive type.
  3. Final Answer:

    String -> Option D
  4. Quick Check:

    Primitive types exclude classes like String [OK]
Hint: Remember primitives are simple types, not objects like String [OK]
Common Mistakes:
  • Confusing String as primitive
  • Thinking boolean is not primitive
  • Mixing wrapper classes with primitives
2. Which of the following is the correct way to declare a variable of type byte in Java?
easy
A. byte b = 127;
B. byte b = 128;
C. byte b = 'a';
D. byte b = 128L;

Solution

  1. Step 1: Understand byte range

    byte stores values from -128 to 127 inclusive.
  2. Step 2: Check each option

    byte b = 128; uses 128 which is out of range; byte b = 127; uses 127 which is valid; byte b = 'a'; assigns a char which is not directly compatible; byte b = 128L; uses long literal 128L which is invalid for byte.
  3. Final Answer:

    byte b = 127; -> Option A
  4. Quick Check:

    byte max value = 127 [OK]
Hint: byte max is 127, so use values within -128 to 127 [OK]
Common Mistakes:
  • Assigning values outside byte range
  • Using char or long literals directly
  • Not knowing byte limits
3. What is the output of the following Java code?
int x = 5;
float y = 2.0f;
System.out.println(x + y);
medium
A. 7.0
B. 7.0f
C. 7
D. 7.0d

Solution

  1. Step 1: Understand type promotion in expression

    int x is promoted to float when added to float y.
  2. Step 2: Calculate the sum and output type

    5 + 2.0f = 7.0 (float), but println prints float as decimal number without suffix.
  3. Final Answer:

    7.0 -> Option A
  4. Quick Check:

    int + float = float output 7.0 [OK]
Hint: int + float results in float decimal output [OK]
Common Mistakes:
  • Expecting integer output
  • Confusing float suffix in output
  • Ignoring type promotion rules
4. Identify the error in the following Java code snippet:
char c = 'AB';
medium
A. char cannot be assigned a value directly
B. Missing semicolon at end
C. char can only hold a single character
D. char must be initialized with a number

Solution

  1. Step 1: Check char assignment rules

    char stores a single 16-bit Unicode character, so it must be assigned a single character in single quotes.
  2. Step 2: Analyze the given code

    'AB' is two characters, which is invalid for char type.
  3. Final Answer:

    char can only hold a single character -> Option C
  4. Quick Check:

    char = single character only [OK]
Hint: char holds exactly one character in single quotes [OK]
Common Mistakes:
  • Assigning multiple characters to char
  • Confusing char with String
  • Forgetting single quotes for char
5. You want to store a large whole number that is bigger than int can hold. Which primitive type should you use in Java?
hard
A. short
B. long
C. float
D. double

Solution

  1. Step 1: Recall int and long ranges

    int holds up to about 2 billion (2^31-1), long holds much larger values (2^63-1).
  2. Step 2: Choose type for large whole number

    long is the correct primitive type for large integers beyond int range; float and double are for decimals, short is smaller than int.
  3. Final Answer:

    long -> Option B
  4. Quick Check:

    Use long for large integers beyond int [OK]
Hint: Use long for big whole numbers beyond int range [OK]
Common Mistakes:
  • Using float/double for whole numbers
  • Choosing short which is smaller
  • Not knowing long size
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard