DEV Community

Jayashree
Jayashree

Posted on

Understanding Data Types in Java: A Complete Guide

  • 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;
Enter fullscreen mode Exit fullscreen mode

Example:

int number = 10;
float decimal = 14.7f;
Enter fullscreen mode Exit fullscreen mode
  • 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";
Enter fullscreen mode Exit fullscreen mode

Arrays: Store multiple values of same type

int arr[] = {1, 2, 3};
Enter fullscreen mode Exit fullscreen mode

Classes and Objects: Store complex data

Student s = new Student();
Enter fullscreen mode Exit fullscreen mode

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)