All set, astronaut! Today, we're diving deep into Java, mastering arithmetic and logical operations on primitive types. These skills are crucial for data manipulation and decision-making during our space voyage.
Recall from our previous sessions that Java's primitive data types include int
for whole numbers, float
for decimal numbers, boolean
for true/false values, and char
for single characters. Both int
and float
have limitations on their numerical range, which will come into play when we discuss overflow later in this lesson.
We can perform arithmetic operations — addition (+
), subtraction (-
), multiplication (*
), division (/
), and modulus - the remainder of the division (%
) — on numerical types. For example,
Java1int a = 10; 2int b = 2; 3System.out.println(a + b); // Outputs: 12 4System.out.println(a - b); // Outputs: 8 5System.out.println(a * b); // Outputs: 20; 6System.out.println(a / b); // Outputs: 5 7System.out.println(a % b); // Outputs: 0
Java allows order alteration using parentheses and provides the modulus (%
) operation — useful for identifying whether numbers are even or odd!
Logical operators — &&
(AND), ||
(OR), !
(NOT) — serve as decision-makers in Java and evaluate to boolean
values — true
or false
. Here's how we can use them with two boolean
values:
Java1System.out.println(true && true); // true 2System.out.println(true && false); // false 3System.out.println(false && true); // false 4System.out.println(false && false); // false 5 6System.out.println(true || true); // true 7System.out.println(true || false); // true 8System.out.println(false || true); // true 9System.out.println(false || false); // false 10 11System.out.println(!true); // false 12System.out.println(!false); // true
Here, &&
produces true
only if both boolean values are true
. ||
delivers true
if either is true
. And !
reverses the boolean value.
But, of course, the main application of logical operations is for variables. Here is a quick, simple example of how they can help to make decisions:
Java1int speed = 60; 2int minSpeed = 30; 3int maxSpeed = 70; 4// Check if the speed is inside the expected bounds. 5System.out.println(speed > minSpeed && speed < maxSpeed); // Prints: true
The term overflow describes what happens when we exceed the range of an integer variable. This occurs when attempting to store a value larger than what the variable's type can contain:
Java1int maxInt = Integer.MAX_VALUE; // the maximal integer, equals to 2^31 - 1 2int overflow = maxInt + 1; // causes an overflow, there is no integer after the maximal one. 3System.out.println(overflow); // Prints: -2147483648, which is -2^31 - the minimal integer number
Here, maxInt
is the maximum value an int
can store. When we add one to it, it 'overflows' to the smallest possible integer value! This brings us to the fact that integer values are "looped" under the hood.
Well done! Today, we uncovered arithmetic operations, made complex decisions using logical operators, and understood what overflow is. On deck are practice exercises to master these essential concepts on our journey into Java. Ladies and gentlemen, it's time for launch!