Data Types in Java

Java is a strongly-typed language, which means every variable must have a specific data type. Java's data types can be divided into two categories:

  1. Primitive Data Types
  2. Reference Data Types
Primitive Data Types

Primitive data types are the most basic data types in Java. They store simple values like numbers, characters, and booleans.

Data TypeSizeDefault ValueDescriptionExample
byte1 byte0Stores small integers from -128 to 127byte a = 10;
short2 bytes0Stores integers from -32,768 to 32,767short b = 1000;
int4 bytes0Stores integers from -2^31 to 2^31-1int c = 50000;
long8 bytes0LStores large integers from -2^63 to 2^63-1long d = 100000L;
float4 bytes0.0fStores floating-point numbers (single precision)float e = 5.75f;
double8 bytes0.0dStores double-precision floating-point numbersdouble f = 19.99d;
char2 bytes'\u0000'Stores single 16-bit Unicode characterschar g = 'A';
boolean1 bitfalseStores true or falseboolean h = true;
Output
Byte value: 120
Short value: 1000
Int value: 123456
Long value: 1000000000
Float value: 12.34
Double value: 1234.56
Char value: J
Boolean value: true
Reference Data Types

Reference data types refer to objects and arrays, not primitive values. They store references (addresses) to the actual objects in memory.

Examples:

  • String: Represents a sequence of characters.
  • Arrays: Represents a fixed-size collection of elements of the same type.
  • Objects: Instances of classes.
Output
Name: Java Programming
First number in the array: 1