Arithmetic and logic expressions in Java

Expressions are an important tool for programmers. We can divide them into arithmetic and logic expressions. Let’s see what expressions are and how we can use them to solve problems.

As stated here:

“An arithmetic expression is an expression that results in a numeric value. There are two kinds of numeric values, integers (whole numbers), and real or floating-point numbers (numbers containing a decimal point).”

If you are going lesson-by-lesson of this short course, you already saw two examples of the use of arithmetic expressions here and here. So, you already intuitively know that they are useful to solve problems by using a programming language.

In the next session, I’ll mention the arithmetic operators and how to use them in Java.

Arithmetic expressions in Java

There are several arithmetic operators in Java as you can see in the table below.

OperatorDescriptionExamples
+Add to values5+5, number1 + number2
++Increment in 1number1++
Subtraction5-5, number1 – number2
Decrement tin 1number1–
*Multiplication5*5, number1 * number2
/Division5/5, number1 / number2
%The remainder of the division5 % 5, number1 % number2
Java arithmetic operators

Now that you know the arithmetic operators and how you can use them, you can create arithmetic expressions.

When you use an operator together with the operands (5+5), you have an arithmetic expression. These expressions can be as simple as one number or a combination of several numeric values and several operators.

Notice that numeric literals (numbers, i.e., 5) are considered the simplest form of arithmetic expressions.

See below an example of the use of several operators in the same arithmetic expression.

float result = number1 * 5 + (number2 % 5) - number3;

You can combine as many operators, variables and, numeric literals as you need to solve the problem. Always remember we use programming languages to write a code (or a program) that is going to solve a problem.

Logic expressions in Java

We use logic expressions when the result of the expression is a logical value, true or false.

Find in the table below a summary of the main logical operators.

OperatorDescriptionExample
&&Logical andval1 && val2
||Logical orval1 || val2
!Logical not! val1
Java logical operators

Observation: the type of the variables val1 and val2 must be boolean.

Does it mean that we only can write simple code when using logic operators? No. There are other operators that can help us create more complex expressions.

Notice that more complex does not mean better. We need to learn how to create more complex expressions because sometimes the solution to a problem needs it. But, remember, like almost everything in life, simpler is better (as long as it solves the problem).

Java comparison operators

There are operators that we can use to compare values. Find a summary below.

OperatorDescriptionExample
==equal tonumber1 == number2
greater thannumber1 > number2
>=greater or equal tonumber1 >= number2
less thannumber1 < number2
<=less or equal tonumber1 <= number2
Java comparison operators

As you should know intuitively, these operators return a boolean value. In other words, the result of the comparison can only have two values, true or false.

In the previous section, you learned that logic expressions must be used only with boolean variables. Now, you will see that they can also be used with any expression that returns a boolean value.

 number1 > number2 && number2 % 2 == 0

What is the meaning of the previous expression?

The left side of the logical operator && will be true if number1 is greater than number2, or false otherwise.

The right side will return whether number2 is even or not. In other words, it first calculates the remainder of the division (number2 % 2), and it will compare if that result equals 0.

As you can see, you can create complex logical expressions by combining them with comparison operators.

Summary

In this lesson, you learned how to use operators (arithmetic, logical and comparison operators) and how to use them through examples.

In the next lesson, you will learn how to use them in a Java program that solves a certain problem.

H@ppy coding!