Introduction
Java is statically typed: every variable has a type, fixed when it is declared and checked by the compiler. A variable declared as an int can never hold text, and the compiler tells you at once if you try. Choosing the right type matters, because each one has its own range and rules. This lesson covers the types you will use in almost every program.
How it works
A declaration names the type, then the variable: int count = 0;. With var, the compiler works the type out from the value: var price = 19.99; makes a double. The type is still fixed; var only saves typing.
Java has eight primitive types. The common ones:
int: whole numbers from about -2.1 to 2.1 billion.long: much larger whole numbers, written with anL:3_000_000_000L. Underscores in numbers are only for reading.double: numbers with decimals, accurate to about 15 digits.char: one character, in single quotes:'A'. A char is also a number, its Unicode code, so'A' + 1is 66.boolean:trueorfalse.
Text is a String, in double quotes. + joins strings, and joins a string with any other value by converting it to text.
Arithmetic follows the types of its operands. Two ints give an int, so 7 / 2 is 3: integer division drops the fraction, and 7 % 2 gives the remainder, 1. To keep the fraction, make one side a double with a cast: (double) 7 / 2 is 3.5. Casting the other way, (int) 3.99, cuts the decimals off and gives 3. Java converts automatically only when nothing can be lost, such as int to long or int to double; going the other way needs a cast.
Each type has a fixed range. A result outside it overflows: Integer.MAX_VALUE + 1 wraps round to the most negative int, without any error. Use long when values can grow large, such as money in cents, times in milliseconds or the product of two big ints.
final makes a variable a constant that can be assigned only once; by convention constants are named in capitals. The class Math provides Math.sqrt, Math.pow, Math.abs, Math.max, Math.round and more.
Example
// Primitive types, conversions, constants and var.
public class Main {
public static void main(String[] args) {
int apples = 7;
int people = 2;
double share = (double) apples / people; // a cast makes the division use decimals
long big = 3_000_000_000L;
char grade = 'A';
boolean enough = apples > people;
final double TAX = 0.15; // final: it can never change
var price = 19.99; // var: the compiler works out that this is a double
System.out.println(apples / people + " " + apples % people + " " + share);
System.out.println(big * 2 + " " + (int) 3.99 + " " + (char) (grade + 1));
System.out.println(enough + " " + Math.round(price * (1 + TAX) * 100) / 100.0);
int max = Integer.MAX_VALUE;
System.out.println(max + 1); // an int overflows and wraps around
}
}Line by line
apples / peopleis integer division, 3;%gives the remainder, 1.- The cast makes the division a double division: 3.5.
- A long holds three billion; doubling it is still fine as a long.
(int) 3.99drops the decimals, and(char) (grade + 1)turns 66 back into a letter.- The price with tax is rounded to cents with
Math.round(x * 100) / 100.0. - Adding 1 to the largest int wraps round to the smallest.
Expected result
3 1 3.5
6000000000 3 B
true 22.99
-2147483648
In the real world
Type mistakes are among the most expensive bugs: rockets have been lost to a number that did not fit its type, and games have shown negative scores after an int overflowed. Java's static types catch many mistakes at compile time, and code reviews watch for integer division and overflow in calculations with money and time.
Common mistakes
- Dividing two ints and expecting decimals:
1 / 2is 0. - Overflowing an int in a product or a sum of large values.
- Comparing doubles with ==, when rounding makes equal-looking values differ slightly.
- Forgetting the L on a large long literal, which javac rejects as an int too large.
Best practices
- Use int by default, long for large counts, and double for measurements.
- Cast before dividing when you need a fraction:
(double) total / count. - Name constants in capitals and make them final.
Summary
Every Java variable has a fixed type, written or inferred with var. The primitives int, long, double, char and boolean, and the String class, cover most needs. Integer division drops fractions, casts convert between types, final makes constants, and values outside a type's range overflow silently.
Quick check
Key takeaways
- Every Java variable has a type, fixed when it is declared; var lets the compiler work it out.
- int / int is integer division; cast one side to double to keep the fraction.
- int holds about plus or minus 2.1 billion; use long for larger whole numbers.
Practice problems for this lesson
My notes
Write down what you want to remember from this lesson. Notes are saved on this device as you type, and appear on your dashboard.