Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Primitive Data Types | Variables and Data Types
Introduction to Java
Section 2. Chapter 3
single

single

Primitive Data Types

Swipe to show menu

Now that you know how to create variables, it's time to understand what kind of data you can put inside them. In Java, primitive data types are the most basic types of data we can store — they define what kind of information we are working with, whether that's numbers, true/false values, or single characters.

Whole Number Types

Used when working with numbers that don't have decimals, like counts, totals, or quantities.

byte — for very small numbers, when you know the value will stay tiny:

byte freeSamples = 12;

short — a bit bigger than byte, for small to medium numbers:

short dailyCustomers = 850;

int — the most commonly used whole number type, the default choice for most situations:

int monthlyOrders = 24500;

long — for very large numbers that go way beyond normal daily usage. Note the L suffix:

long totalCoffeeBeansUsed = 5000000000L;

Decimal Number Types

Used when we need precision, like pricing or measurements.

float — when we don't need very high precision. Note the f suffix:

float coffeeTemperature = 22.5f;

double — the most commonly used decimal type, the default choice when working with money or prices:

double lattePrice = 4.99;

Boolean Type

boolean stores only two values — true or false. No "maybe", just a clear yes or no:

boolean isCoffeeShopOpen = true;

Character Type

char stores a single character — one letter, symbol, or number. Note the single quotes:

char drinkSize = 'M';
Task

Swipe to start coding

  1. Add the values of the variables firstNumber and secondNumber.
  2. Divide the sum by the value of the variable thirdNumber.
  3. Store the final result in the variable result.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 3
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt