Variables and Data Types

The int, long, double, char, boolean and String types; arithmetic and overflow.

Java is a strictly typed language: every variable is declared with a type, and that type never changes. The main types: • int — integers up to about 2 billion (2·10⁹); • long — big integers up to 9·10¹⁸; literals get an L suffix: 1000000000000L; • double — floating-point numbers; • char — a single character in single quotes; • boolean — true or false; • String — a string (it is a class, written with a capital letter).
Java

Declaring variables of different types

Arithmetic: + - * / %. As in C++, integer division discards the fractional part: 7 / 2 equals 3. Remainder: 7 % 2 equals 1. Overflow is the main trap: the product of two ints worth a billion each does not fit in an int. Use long for large values — and remember that the result type is determined by the operand types: for the product to be computed in long, at least one factor must be long.
Java

Integer division and overflow

Task: create variables with the values 15 and 4 and print the sum, difference, product, integer quotient and remainder — each on its own line. Then mark the lesson as completed.
Доска