Primitive data types

Tuesday, November 7, 2023

Primitive data types in Java are fundamental elements that represent simple values and are used to store basic data. Unlike wrapper classes, which encapsulate primitive values in objects, primitive data types are resource-efficient and commonly used to store simple data. In this section, we will explore the most common primitive data types in Java and how they are used.

Common Primitive Data Types

Java offers a variety of primitive data types, each designed to represent a specific type of data. Here are some of the most common primitive data types in Java:

  1. int: Represents integers, positive or negative, without decimal parts. Example: int age = 25;

  2. double: Represents floating-point numbers with decimals. Example: double price = 19.99;

  3. boolean: Represents logical values, true or false, used in conditional expressions. Example: boolean isActive = true;

  4. char: Represents a 16-bit Unicode character. Example: char letter = 'A';

  5. byte: Represents small integers, often used when saving memory space. Example: byte value = 120;

  6. short: Represents short integers, used in situations that require smaller integer values than int. Example: short distance = 1000;

  7. long: Represents long integers, suitable for extremely large integer values. Example: long worldPopulation = 7,900,000,000L;

  8. float: Represents floating-point numbers, similar to double but with lower precision. Example: float height = 1.75f;

Declaration and Use of Primitive Data Types

To declare and use primitive data types in Java, you simply need to specify the data type followed by the variable name and, optionally, initialize it with a value. Here is an example of how to declare and initialize variables with primitive data types:

int integerNumber = 42;
double decimalNumber = 3.14;
boolean isTrue = true;
char character = 'X';

Primitive data types are used in mathematical operations, conditional expressions, assignments, and more.

For example:

int sum = integerNumber + 10;
boolean isGreater = decimalNumber > 2.0;
char nextCharacter = (char)(character + 1);

Size and Value Range

Each primitive data type has a memory size and an associated value range. For example, the int type occupies 4 bytes of memory and can represent values ranging from -2,147,483,648 to 2,147,483,647. It's important to understand the size and value range of each data type to ensure they are used appropriately in your programs.

Share it

Share it on your social media and challenge your friends to solve programming problems. Together, we can learn and grow.


The code has been successfully copied to the clipboard.

More Exercises

Continue improving your Java programming skills with our selection of practical exercises from the lesson. Click on Practice and challenge your knowledge!



Variables are an essential component in any programming language, and Java is no exception. In this post, we will explore the concept of variables in Java and how they are used in programming.


Fundamentals of data types

Data types are a fundamental concept in programming and play a crucial role in Java, a strongly typed programming language. In this section, we will explore the basic concepts of data types in Java and their importance in application development.


Primitive data types

Primitive data types in Java are fundamental elements that represent simple values and are used to store basic data.


Wrapper classes

In Java, wrapper classes are a set of classes that encapsulate primitive data types.


Reverse chars

Write a program to ask the user for three letters and display them in reverse order.


Double value

Write a Java program that requests the width (x) and height (y) of a rectangle and calculate the perimeter, area and diagonal.


Float value

Create a program Java to ask the user for a distance in meters and the time taken (hours, minutes, seconds).


Generate a random number

Create a Java program that prompts the user for 2 integers and displays a random number on the screen between those values.

Data types

Practice with easy and intermediate Java exercises on different types of text, numeric or boolean data.

Fundamentals concepts
Practice exercises

Keep learning

C# Programming Course

Free programming course with practical exercises and solutions in C#. Start learning right now!

Exercises C# App

Take your Exercises C# lessons everywhere with our Android app. Download it now from Google Play