📢 Join My WhatsApp Channel JavaWithSumit

Get Daily ICSE Java Notes, Programs & Exam Tips 🚀

👉 Join Now

Java Data Types Explained – Primitive and Non-Primitive

 

Data Types in Java:

Introduction

In Java programming, data types play a very important role. A data type defines the type of value a variable can store. For example, some variables store numbers, some store characters, and some store true/false values.

Types of Data Types in Java

In Java, data types are mainly divided into two categories:

  1. Primitive Data Types

  2. Non-Primitive (Reference) Data Types

1. Primitive Data Types in Java

Primitive data types are the basic built-in data types provided by Java and used to store simple values like numbers, characters, and true/false values. They are not objects and store actual values directly in memory, which makes them fast and memory efficient.

Java has 8 primitive data types.

Data Type        Description                                                Example                            
byteStores small integersbyte a = 10;
shortStores slightly larger integersshort b = 200;
intStores whole numbersint marks = 85;
longStores very large numberslong population = 9000000;
floatStores decimal numbersfloat price = 10.5f;
doubleStores large decimal valuesdouble pi = 3.14159;
charStores a single characterchar grade = 'A';
booleanStores true or false valuesboolean pass = true;

1. byte

  • Used to store small integer numbers.

  • Memory size: 1 byte (8 bits)

  • Range: -128 to 127

Example:

byte age = 25;

2. short

  • Used for storing slightly larger integers than byte.

  • Memory size: 2 bytes (16 bits)

  • Range: -32,768 to 32,767

Example:

short temperature = 120;

3. int

  • Most commonly used integer data type.

  • Memory size: 4 bytes (32 bits)

  • Range: -2,147,483,648 to 2,147,483,647

Example:

int marks = 95;

4. long

  • Used to store very large integer values.

  • Memory size: 8 bytes (64 bits)

  • Range: -9,223,372,036,854,775,808 to 9,223,372,036,854,775,807

Example:

long population = 7800000000L;

5. float

  • Used to store decimal numbers (single precision).

  • Memory size: 4 bytes

  • Precision: about 6–7 decimal digits

Example:

float price = 45.75f;

6. double

  • Used for large decimal numbers (double precision).

  • Memory size: 8 bytes

  • Precision: about 15 decimal digits

Example:

double distance = 12345.6789;

7. char

  • Used to store a single character.

  • Memory size: 2 bytes (16 bits)

  • Range: 0 to 65,535 (Unicode characters)

Example:

char grade = 'A';

8. boolean

  • Used to store true or false values.

  • Memory size: 1 bit (logical value)

Example:

boolean isPassed = true;

Primitive Data Types Table

Data Type            Size                Range / Capacity                                    Example                
byte1 byte-128 to 127byte b = 10;
short2 bytes-32,768 to 32,767short s = 200;
int4 bytes-2,147,483,648 to 2,147,483,647int i = 500;
long8 bytesVery large integerslong l = 10000L;
float4 bytes6–7 decimal digitsfloat f = 5.5f;
double8 bytes15 decimal digitsdouble d = 10.25;
char2 bytesUnicode characterschar c = 'A';
boolean1 bittrue / falseboolean flag = true;

Non-Primitive Data Types:

Non-primitive data types are also known as Reference Data Types because they store the reference (address) of the object in memory instead of the actual valueNon-Primitive Data Types are data types that are created using classes and can store multiple values or complex data.

Unlike primitive data types, they:

  • Can store multiple values

  • Have methods and properties

  • Are created by the programmer or provided by Java

Types of Non-Primitive Data Types in Java

The most common non-primitive data types are:

  1. String

  2. Array

  3. Class

  4. Object

  5. Interface

Features of Non-Primitive Data Types

Non-primitive data types have the following features:

  • They are created using classes

  • They can store multiple values

  • They support methods and functions

  • They store references instead of actual data

  • They are used to build complex programs

Difference Between Primitive and Non-Primitive Data Types

Feature                    Primitive Data Type                    Non-Primitive Data Type
StorageStores actual valueStores reference
SizeFixedNot fixed
Examplesint, float, charString, Array, Class
ComplexitySimpleComplex


Types of Data Type


Source: ChatGPT, Morning Star

Comments

Popular posts from this blog

Introduction to Object Oriented Programming and Java- ICSE Notes

Introduction to OOP's

Object Modelling – Entities and Their Behaviour in Java