← Back to Chapters

Java Primitive Data Types

? Java Primitive Data Types

? Quick Overview

Primitive data types in Java are the most basic data types used to store simple values. They are predefined by Java and are stored directly in memory.

? Key Concepts

  • Java has exactly 8 primitive data types
  • They store simple values, not objects
  • They are faster and memory-efficient
  • Each type has a fixed size

? Syntax / Theory

  • byte – 1 byte
  • short – 2 bytes
  • int – 4 bytes
  • long – 8 bytes
  • float – 4 bytes
  • double – 8 bytes
  • char – 2 bytes
  • boolean – true/false

? Code Example

? View Code Example
// Demonstrating all Java primitive data types
byte age = 25;
short year = 2024;
int salary = 50000;
long population = 8000000000L;
float price = 99.99f;
double pi = 3.14159265359;
char grade = 'A';
boolean isJavaFun = true;

? Live Output / Explanation

Each variable stores a specific type of value. For example, int stores whole numbers, double stores decimal values, and boolean stores true or false.

? Tips & Best Practices

  • Use int for most whole numbers
  • Use double for decimal calculations
  • Use boolean for conditions
  • Choose the smallest data type that fits your data

? Try It Yourself

  • Create variables using all 8 primitive data types
  • Print their values using System.out.println()
  • Change values and observe the output