- Declare a variable with a type and assign a value to it
- Choose a suitable primitive type for a task
- Calculate correctly with integer division, remainders and type casting
An online shop has to remember each product's name, its price, how many are left in stock and whether it is on sale. Programs keep such information in variables. In Java every variable has a type that is known in advance and never changes — languages like this are called statically typed.
Declaring variables
To create a variable, write its type first, then its name, and optionally give it a value right away with =: int age = 14;. Later you can assign a new value, but not a value of a different type: you cannot put text into an int variable.
public class Main {
public static void main(String[] args) {
String name = "Leyla";
int age = 14;
double height = 1.62;
boolean isStudent = true;
char grade = 'A';
System.out.println(name + " is " + age + " years old.");
System.out.println("Height: " + height + " m");
System.out.println("Student: " + isStudent + ", grade: " + grade);
age = age + 1;
System.out.println("Next year: " + age);
}
}Leyla is 14 years old. Height: 1.62 m Student: true, grade: A Next year: 15
Primitive types
Java has eight primitive types — the simplest building blocks of the language. They differ in how much memory they take and which values they can hold. For whole numbers you usually choose int, and for decimals double.
| Type | Size | What it holds | Example |
|---|---|---|---|
byte | 8 bits | whole numbers from −128 to 127 | byte level = 5; |
short | 16 bits | whole numbers from −32,768 to 32,767 | short year = 2026; |
int | 32 bits | about ±2.1 billion | int people = 10_000_000; |
long | 64 bits | about ±9.2 · 10¹⁸ | long stars = 3_000_000_000L; |
float | 32 bits | a decimal, about 7 digits of precision | float t = 36.6f; |
double | 64 bits | a decimal, about 15–16 digits of precision | double price = 9.99; |
char | 16 bits | a single character (Unicode) | char grade = 'A'; |
boolean | – | true or false | boolean ok = true; |
long literal ends with L and a float literal with f. Underscores _ make big numbers easier to read.String is not in this list: it is not a primitive type but a class — that is why it starts with a capital letter. You will get to know strings properly in the next lesson. Note that a char value goes in single quotes ('A'), while a String uses double quotes.
byte is made of 8 bits. Toggle the bits: 8 bits give 256 different combinations, and Java uses them for the numbers from −128 to 127.Arithmetic operators
Java has the operators +, -, *, / and % (the remainder after division). The most important detail: when you divide two integers, the result is also an integer — the fractional part is simply dropped. So 17 / 5 is 3, not 3.4.
public class Main {
public static void main(String[] args) {
int a = 17;
int b = 5;
System.out.println(a + b);
System.out.println(a - b);
System.out.println(a * b);
System.out.println(a / b);
System.out.println(a % b);
System.out.println(17.0 / 5);
int count = 10;
count += 5;
count++;
System.out.println(count);
}
}22 12 85 3 2 3.4 16
As soon as one of the numbers is a decimal (17.0), the result is a decimal too. count += 5 is a short way of writing count = count + 5; -=, *= and /= work the same way. count++ increases a variable by 1, and count-- decreases it by 1.
A film lasts 135 minutes. How can you show this in hours and minutes?
Show solutionHide solution
135 / 60 = 2.Remaining minutes — the remainder:
135 % 60 = 15.System.out.println(135 / 60 + " h " + 135 % 60 + " min"); prints 2 h 15 min.Type casting
Sometimes you need to turn a value of one type into another. From a smaller type to a bigger one (widening, e.g. int → double) Java converts automatically and safely. From a bigger type to a smaller one (narrowing, e.g. double → int) information can be lost, so you must ask for it explicitly by writing the type in parentheses: (int) price.
public class Main {
public static void main(String[] args) {
int apples = 7;
double d = apples;
System.out.println(d);
double price = 9.99;
int whole = (int) price;
System.out.println(whole);
int sum = 7, people = 2;
System.out.println(sum / people);
System.out.println((double) sum / people);
System.out.println((int) 'A');
System.out.println((char) 66);
int max = Integer.MAX_VALUE;
System.out.println(max + 1);
}
}7.0 9 3 3.5 65 B -2147483648
(int) 9.99gives9: the fractional part is dropped, not rounded.(double) sum / peoplefirst turnssuminto adouble, so the division keeps the fraction:3.5.- A
charis really a number: the code of'A'is 65, and 66 is'B'. - Adding 1 to the largest
intcauses an overflow: the value “wraps around” to the smallest negative number.
Key points
- Java is statically typed: a variable is declared as
type name = value;and its type never changes. - There are eight primitive types:
byte,short,int,long,float,double,char,boolean;Stringis a class. - Dividing two integers gives an integer:
17 / 5=3,17 % 5=2. - Widening is automatic; narrowing needs an explicit cast:
(int) 9.99=9. finalcreates a constant, andvarlets the compiler work out the type for you.
Check yourself
10 questions. Every correct answer earns XP.
System.out.println(17 / 5); print?