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';
Swipe to start coding
- Add the values of the variables
firstNumberandsecondNumber. - Divide the sum by the value of the variable
thirdNumber. - Store the final result in the variable
result.
Solution
Thanks for your feedback!
single
Ask AI
Ask AI
Ask anything or try one of the suggested questions to begin our chat