Skip to main content

Data-Types

Java supports two categories of data types:

  • Primitive Data Types: Store values directly, fixed size.
  • Non-Primitive (Reference/Object) Data Types: Store references to objects, flexible size and behaviors.

Primitive Data Types​

Primitive data types are predefined by Java and named by a reserved keyword and it can't devide more further.

TypeSize (bits)Default ValueRangeDescriptionExample
byte80-128 to 127Smallest integer typebyte b = 100;
short160-32,768 to 32,767Small integershort s = 2000;
int320-2,147,483,648 to 2,147,483,647Default integer typeint n = 250000;
long640L-9,223,372,036,854,775,808 to 9,223,372,036,854,775,807Large integer typelong m = 250000L;
float320.0f1.4e-45 to 3.4e+38Single precision floating-pointfloat x = 2.3f;
double640.0d4.9e-324 to 1.8e+308Double precision floating-pointdouble y = 5.7d;
char16'\u0000'Unicode: 0 to 65,535Single character (Unicode support)char c = 'A';
boolean1falsetrue/falseLogicboolean flag = true;

Primitive Groups​

  • Integer types: byte, short, int, long
  • Floating-point types: float, double
  • Character type: char
  • Boolean type: boolean

Details & Notes​

  • byte/short/int/long: Same usage but for different numeric ranges to save memory.
  • float: Ends with 'f' or 'F'. Precision up to 7 decimal digits.
  • double: Ends with 'd' or 'D'. Precision up to 15 decimal digits.
  • char: Can store any Unicode character, making it suitable for internationalization.
  • boolean: Used for flags and logical control; only two possible values.

Non-Primitive (Reference) Data Types​

Non-primitives, also called reference types, point to objects created by the programmer or provided by Java.It can divide more further.

TypeDescriptionExample
StringSequence of characters (class)String s = "Hello";
ArrayCollection of similar elementsint[] arr = {1,2,3};
ClassUser-defined or Java API classMyClass obj = new MyClass();
InterfaceTemplate for classesRunnable r = new Thread();
EnumSet of constantsenum Color {RED,GREEN}

Non-Primitive Details​

  • Strings are objects; many methods for text processing.
  • Arrays can be multi-dimensional; element type must be uniform.
  • Classes/Objects support attributes, methods, and inheritance.
  • Interfaces/enums structure code and provide abstraction.

Comparison Table​

FeaturePrimitive TypesNon-Primitive Types
Memory AllocationStackHeap (object), Stack (reference)
Supports methodsNoYes
ExtensibleNoYes
Can be nullNoYes
SizeFixedVariable
Used forSimple valuesCollections, objects, text, etc.

Example Code Block​

// Primitives
int age = 21;
double salary = 65000.25d;
char grade = 'A';
boolean isActive = true;

// Non-primitives
String name = "Alex";
int[] marks = {85, 93, 78};
Person person = new Person("Alex");

Key Points​

  • Use primitives for basic values and calculations.
  • Use non-primitives for complex data (objects, strings, arrays).
  • Non-primitives provide extra functionality via methods and inheritance.
  • Primitives are more memory efficient and faster for simple tasks.

This covers everything essential about Java's data types.