DEV Community

Karthick Narayanan
Karthick Narayanan

Posted on

Day 3: Understanding Data Types in Java

What is a Data Type?

A data type tells Java what kind of data a variable can store.
For example:

  • Numbers
  • Characters
  • True or false values

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


1. Primitive Data Types

Primitive data types are the basic building blocks in Java.
Java has 8 primitive data types.

Integer types

These are used to store whole numbers:

  • int
  • byte
  • short
  • long

Example:

int age = 25;
Enter fullscreen mode Exit fullscreen mode

Boolean type

Used to store true or false values:

  • boolean

Example:

boolean isJavaEasy = true;
Enter fullscreen mode Exit fullscreen mode

Decimal (Floating-point) types

Used to store numbers with decimal points:

  • float
  • double

Example:

double price = 260.30;
Enter fullscreen mode Exit fullscreen mode

Character type

Used to store a single character:

  • char

Example:

char grade = 'A';
Enter fullscreen mode Exit fullscreen mode

2. Non-Primitive Data Type

Non-primitive data types are more complex and can store multiple values.

String

The main non-primitive data type is:

  • String

A String is used to store text or words.

Example:

String name = "Karthick";
Enter fullscreen mode Exit fullscreen mode

Top comments (0)