Data types

Java has eight primitive data types:

byte

  • size - 1 byte (8 bits)
  • stores whole number from -128 to 127
byte smallNumber = 100;

short

  • size - 2 bytes (16 bits)
  • stores value from - 32,768 to 32,767
short mediumNumber = 30000;

int

  • size - 4 bytes (32 bits)
  • stores values from —2,147,483,648 to 2,147,483,647
int number = 100000;

long

  • size - 8 bytes (64 bits)
  • stores values from -2^63 to 2^63 - 1
long bigNumber = 9223372036854775807L; // Add 'L' at the end

float

  • size - 4 bytes (32 bits)
  • stores decimal values with ~7 decimal precision
float decimalNumber = 3.14f; // Add 'f' at the end

double

  • size - 8 bytes (64 bits)
  • stores decimal values with ~15 decimal precision
double preciseNumber = 3.14159265358979;

char

  • size - 2 bytes (16 bits)
  • stores a single unicode character.
char letter = 'A';

boolean

  • size - 1 bit
boolean yes = true;

default values

Data TypeDefault Value
byte0
short0
int0
long0L
float0.0f
double0.0d
char’\u0000’ (null character)
booleanfalse

Variables

A variable is a named storage location in memory. It has:

  1. Data type (primitive or reference type)
  2. Name (identifier)
  3. Value (stored data)
int age = 20;        // Declaration + Initialization
double price;        // Declaration only
price = 99.99;       // Assigning a value later

Variable Naming Rules

  • Can contain letters, digits, underscores (_) and dollar signs ($)
  • Cannot start with a number
  • Cannot use reserved keywords (e.g., int, class)
  • Java follows camelCase naming convention:
  int studentAge = 25;