Ugenz Academy/Tutorial

Datatypes

6 min read1,173 words

Reading progress

Keep learning

Data Types in Java

Introduction

A data type specifies what kind of data a variable can store. In Java, every variable must have a data type.

For example:

int age = 20; String name = "Anuradha";

Here:

  • int is the data type of age.
  • String is the data type of name.

Java data types are mainly divided into two categories:

Data Types │ ├── Primitive Data Types │ └── Non-Primitive Data Types

1. Primitive Data Types

Primitive data types are the basic data types provided by Java.

Java has 8 primitive data types:

Data TypeSizeExampleDescription
byte8-bitbyte x = 10;Small integer
short16-bitshort x = 1000;Small integer
int32-bitint x = 10000;Integer
long64-bitlong x = 100000L;Large integer
float32-bitfloat x = 10.5f;Decimal number
double64-bitdouble x = 10.5;More precise decimal
char16-bitchar x = 'A';Single character
booleanJVM-dependentboolean x = true;True or false

1.1 byte

The byte data type is used to store small integer values.

Syntax

byte age = 20;

Range

-128 to 127

Example

public class Main { public static void main(String[] args) { byte age = 20; System.out.println(age); } }

1.2 short

The short data type is used to store integer values larger than byte.

Syntax

short number = 1000;

Range

-32,768 to 32,767

Example

short population = 30000; System.out.println(population);

1.3 int

The int data type is commonly used for whole numbers.

Syntax

int age = 20;

Range

-2,147,483,648 to 2,147,483,647

Example

int marks = 85; System.out.println(marks);

1.4 long

The long data type is used for very large integer values.

A value normally uses L or l at the end.

Syntax

long population = 1000000000L;

Example

long distance = 9876543210L; System.out.println(distance);

2. Floating-Point Data Types

Java provides two data types for decimal numbers:

  • float
  • double

2.1 float

The float data type stores decimal numbers with single precision.

The value should normally have f or F at the end.

Syntax

float price = 500.50f;

Example

float temperature = 25.5f; System.out.println(temperature);

2.2 double

The double data type stores decimal numbers with greater precision than float.

Syntax

double salary = 25000.50;

Example

double percentage = 85.75; System.out.println(percentage);

double is generally preferred when more decimal precision is needed.

3. char

The char data type stores a single character.

A character is written using single quotation marks ' '.

Syntax

char grade = 'A';

Example

char gender = 'M'; System.out.println(gender);

Output:

M

A char stores one UTF-16 code unit.

4. boolean

The boolean data type stores one of two values:

true false

Example

boolean isPassed = true; System.out.println(isPassed);

Output:

true

Boolean values are commonly used in conditions:

int age = 20; if (age >= 18) { System.out.println("Adult"); }

5. Non-Primitive Data Types

Non-primitive data types are also called reference types. They can refer to objects.

Common examples include:

  • String
  • Arrays
  • Classes
  • Objects
  • Interfaces
  • Enums

5.1 String

String is used to store a sequence of characters.

Example

String name = "Anuradha"; System.out.println(name);

Output:

Anuradha

Unlike primitive types, String is a class.

5.2 Array

An array stores multiple values of the same type.

Example

int[] marks = {80, 75, 90, 85}; System.out.println(marks[0]);

Output:

80

Array indexing starts from 0.

5.3 Class

A class is a blueprint for creating objects.

class Student { String name; int age; }

5.4 Object

An object is an instance of a class.

Student s1 = new Student(); s1.name = "Anuradha"; s1.age = 20;

Here, s1 refers to a Student object.

Primitive vs Non-Primitive Data Types

PrimitiveNon-Primitive
Basic data typesReference types
8 typesMany types
Stores a value directlyStores a reference to an object
Generally has fixed sizeSize depends on the type/object
Examples: int, char, booleanExamples: String, arrays, classes

Default Values

Instance and static variables have default values if they are not explicitly initialized.

Data TypeDefault Value
byte0
short0
int0
long0L
float0.0f
double0.0d
char'\u0000'
booleanfalse
Reference typesnull

Note: Local variables do not automatically receive default values. They must be initialized before use.

Example Using Different Data Types

public class Main { public static void main(String[] args) { byte age = 20; short marks = 500; int salary = 25000; long population = 3000000000L; float price = 500.50f; double percentage = 85.75; char grade = 'A'; boolean passed = true; String name = "Anuradha"; System.out.println("Name: " + name); System.out.println("Age: " + age); System.out.println("Marks: " + marks); System.out.println("Salary: " + salary); System.out.println("Population: " + population); System.out.println("Price: " + price); System.out.println("Percentage: " + percentage); System.out.println("Grade: " + grade); System.out.println("Passed: " + passed); } }

Type Casting

Type casting means converting one data type into another.

Widening Casting

Converting a smaller type into a larger compatible type.

int number = 100; double value = number; System.out.println(value);
int → long → float → double

Narrowing Casting

Converting a larger type into a smaller type requires explicit casting.

double number = 10.5; int value = (int) number; System.out.println(value);

Output:

10

The decimal portion is removed.

Summary

Java data types are divided into:

Java Data Types │ ├── Primitive │ ├── byte │ ├── short │ ├── int │ ├── long │ ├── float │ ├── double │ ├── char │ └── boolean │ └── Non-Primitive / Reference ├── String ├── Array ├── Class ├── Object └── Interface

Conclusion

Data types are an important part of Java because they define the kind of data a variable can store. Java provides eight primitive data types and many reference types such as String, arrays, classes, and interfaces. Understanding data types helps you write correct and efficient Java programs.

Keep learning. Keep building. Keep growing.

© 2026 Ugenz Academy