Jesse Haniel
Lead Software Architect at Tribunal de Justiça da Paraíba

Numeric operators in Java

Published in the Java Developer group
members
Hi! Today we'll consider a very important topic, namely, numeric operators in Java.
Numeric operators in Java - 1
In programming, numbers are everywhere. If you dig deep and remember high school, you may recall that a computer represents all information in a numerical format: combinations of zeros and ones, also known as binary code.
Numeric operators in Java - 2
There are lots of numeric operators in programming, so we'll use examples to explore the most important of them :) Let's start with the simplest: arithmetic operators. These are the well-known addition (+), subtraction (-), multiplication (*), and division (/) operators.
public class Main {

   public static void main(String[] args) {

       int x = 999;
       int y = 33;

       System.out.println(x+y);
       System.out.println(x-y);
       System.out.println(x*y);
       System.out.println(x/y);
   }
}
Console output: 1032 966 32967 30 You've already used all this. To this group, you can add to the remainder or modulo (%) operator.
public class Main {

   public static void main(String[] args) {

       int x = 33;
       int y = 33%2;
       System.out.println(y);
   }
}
Console output: 1 In this example, we divide 33 by 2. This yields 16, with an extra "tail" (one) that is not divisible by 2. This "tail" is the result of the "remainder from division" operation. Java also implements comparison/relational operators (just like in mathematics). They're probably familiar to you from school, too:
  • equal to (==)
  • greater than (>)
  • less than (<)
  • greater than or equal to (>=)
  • less than or equal to (<=)
  • not equal (!=)
Here you should pay attention to one important point that causes many beginners to make mistakes. The "equals" operator is written ==, not =. In Java, a single = is the assignment operator, which is used when a variable is assigned a number, string, or the value of another variable.
Numeric operators in Java - 3
public class Main {

   public static void main(String[] args) {

       int x = 33;
       int y = 999;
       System.out.println(x=y);// We expect false is be displayed
   }
}
Console output: 999 Oops! This is obviously not the result we expected. It's an entirely different data type: we expected to see a boolean, but we got a number. All because we used an assignment operator in the parentheses instead of a comparison. x=y The value of y (999) was assigned to the variable x, and then we displayed the value of x. Here's the right way to do it:
public class Main {

   public static void main(String[] args) {

       int x = 33;
       int y = 999;
       System.out.println(x==y);
   }
}
Console output: false Now we've compared the two numbers properly! :) Here's another feature of the assignment operator (=): it can be "chained" together:
public class Main {

   public static void main(String[] args) {

       int x = 999;
       int y = 33;
       int z = 256;

       x = y = z;
       System.out.println(x);
   }
}
Console output: 256 Remember assignment is from right to left. This expression (x = y = z) will be executed in steps:
  • y = z, that is, y = 256
  • x = y, that is, x = 256

Unary operators.

They are called "unary" from the word "uno", which means "one". They got this name because, unlike the previous operators, they act on a single number, not several. These include:
  • Unary minus. It flips the number's sign.

public class Main {

   public static void main(String[] args) {

       int x = 999;

       // Change the sign for the first time
       x = -x;
       System.out.println(x);

       // Change the sign for the second time
       x= -x;
       System.out.println(x);
   }
}
Console output: -999 999 We used the unary minus operator twice. As a result, our number was first negative, and then it became positive again!
  • Increment (++) and decrement (--)
The ++ operator increases a number by one, and the -- operator reduces a number by the same amount.
public class Main {

   public static void main(String[] args) {

       int x = 999;
       x++;
       System.out.println(x);

       x--;
       System.out.println(x);
   }
}
Console output: 1000 999 This notation may be familiar to you if you've heard of the C++ language. Its creators used this interesting name to convey the idea that "C++ is an extension of the C language" A popular improved version of Notepad is called Notepad++ Here's an important point. There are two types of increment and decrement operators: postfix and prefix. x++ - postfix ++x - prefix What's the fundamental difference between putting the pluses/minuses before or after the number? We'll see in the following example:
public class Main {

   public static void main(String[] args) {

       int x = 999;
       int y = x++;
       System.out.println(y);
   }
}
Console output: 999 Something's not right! We wanted to increase x by 1 and assign the new value to the variable y. In other words, y should be 1000. But instead we get something else: 999. It seems like x wasn't increased and that the increment operator didn't work? But it did work. To convince yourself, try displaying x at the end :)
public class Main {

   public static void main(String[] args) {

       int x = 999;
       int y = x++;
       System.out.println(y);
       System.out.println(x);
   }
}
Console output: 999 1000 In fact, this is precisely why this operation is called postfix: it is performed after the main expression. This means, in our case: int y = x++; y = x is performed first (and the variable y will be initialized to the value of x), and only then will x++ be executed What if this isn't the behavior we want? Then we need to use prefix notation:
public class Main {

   public static void main(String[] args) {

       int x = 999;
       int y = ++x;
       System.out.println(y);
   }
}
In this case, ++x is processed first and only afterward is y = x; executed. You should commit this difference to memory right away to avoid making mistakes in a real program where using postfix instead prefix could turn everything upside down :)

Compound operators

In addition, in Java there are so-called compound operators. They combine two operators:
  • Assignment
  • Arithmetic operators
These include:
  • +=
  • -=
  • *=
  • /=
  • %=
Let's consider an example:
public class Main {

   public static void main(String[] args) {

       int x = 999;
       int y = 33;

       x += y;
       System.out.println(x);
   }
}
Console output: 1032 x += y means x = x + y. The two symbols are used consecutively for brevity's sake. The combinations -=, *=, /= and %= work in a similar way.

Logical operators

In addition to numeric operators, Java also has operations that involve boolean values (true and false). These operations are performed using logical operators
  • ! - logical NOT. It flips the value of a boolean
public class Main {

   public static void main(String[] args) {

       boolean x = true;
       System.out.println(!x);
   }
}
Console output: false
  • && - logical AND. It returns true only if both operands are true.
public class Main {

   public static void main(String[] args) {

       System.out.println(100 > 10 && 100 > 200);
       System.out.println(100 > 50 && 100 >= 100);
   }
}
Console output: false true The result of the first operation is false, because one of the operands is false, namely 100 > 200. To return true, the && operator requires that both operands be true (as is the case in the second line).
  • || - logical OR. It returns true when at least one of the operands is true.
When we use this operator, our previous example produces a different result:
public class Main {

   public static void main(String[] args) {

       System.out.println(100 > 10 || 100 > 200);
   }
}
Console output: true The expression 100 > 200 is still false, but for the OR operator it is entirely sufficient that the first part (100 > 10) is true.
Comments (41)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
Maggie
Level 3 , London, United Kingdom
5 August, 14:44
Very informative, thanks
Janneke
Level 4 , Velp, Netherlands
3 May, 13:52
Great explanation, very clear, thank you
Andy Lee
Level 10
23 February 2021, 11:23
nice
punkuotukas
Level 7 , Vilnius, Lithuania
8 September 2020, 10:32
"In fact, this is precisely why this operation is called postfix: it is performed after the main expression." What exactly is the main expression in this case:
int x = 999;
int y = x++;
System.out.println(y);
System.out.println(x);
Onur Bal
Level 27 , Istanbul, Turkey
8 September 2020, 17:43
The main expression is this:
int y = x
This gets executed first, assigning y to the value of x (which is 999 at this point). After the execution of the main expression above, the following is executed:
x++;
making the value of x equal to 1000.
Joe M
Level 41 , Owings Mills, United States
30 September 2020, 00:47
great illustration of this difficult point. Prefix and postfix can really mess you up.
Joe M
Level 41 , Owings Mills, United States
30 September 2020, 00:54
do it in the other way using prefix and check values of x and y int x = 999; int y = ++x; System.out.println(y); System.out.println(x);
Karas Java Developer
28 August 2020, 17:51
Concise and to the point Jesse. I really wonder if my books would be more useful as door stops.
irades
Level 8 , Moscow, Russia
29 July 2020, 19:46
Yet again another useful article, I love CodeGym!
Jaki Taki
Level 16 , Poznan, Poland
20 July 2020, 08:08
Yes, good reminder
Yer Sultan
Level 7 , Karaganda, Kazakhstan
7 July 2020, 07:52
Good article, good reminder.
Attila
Level 7 , Hainault, United Kingdom
5 July 2020, 09:20
don't forget difference between ++i and i++ :)))
Andrei
Level 41
25 September 2020, 08:42
What is it? I read they are basically the same. And I did the half pyramid exercise and the results were the same when trying both ++i and i++.
Nathan Guidry
Level 6 , Lake Charles, United States
14 May 2021, 15:56
I'm sure you know by now, but the prefix executes the "++" first in a statement. Postfix would first complete the statement (Professor's case x + y assignment), then change x's value by one since the "++" is after.
Andrei
Level 41
14 May 2021, 16:04
I do, but a refresher is always nice. Thanks! 😁💪
Nirake_
Level 8 , Mumbai, India
24 May 2020, 20:50
Only Meaning full stuff - Great.