- Java is a strongly-typed programming language, which means every variable must have a data type.
What is Datatype?
- In Simple, Data types are the types of data.
- Data types tell the compiler what kind of value a variable can hold and how much memory it will consume.
Syntax:
datatype variable_name = data;
Example:
int number = 10;
float decimal = 14.7f;
- In Java, data types are mainly divided into two categories: Primitive and Non-Primitive (Reference).
1. Primitive Data Types
Primitive data types are the basic building blocks in Java. There are 8 primitive types:
Primitive data types store actual values and are fast in execution.
2. Non-Primitive (Reference) Data Types
Non-primitive data types are also called reference types because they store the memory address of an object instead of the actual value.
Common non-primitive types:
String: Text data
String name = "Hello Java";
Arrays: Store multiple values of same type
int arr[] = {1, 2, 3};
Classes and Objects: Store complex data
Student s = new Student();
etc..
Reference types can be null and size depends on the object.
Simple analogy:
Primitive → The box contains the candy directly.
Non-Primitive → The box contains a map/address; you follow it to get the candy.


Top comments (0)