CodeGym /Java Blog /Java Numbers /Numeric operators in Java
Author
Jesse Haniel
Lead Software Architect at Tribunal de Justiça da Paraíba

Numeric operators in Java

Published in the Java Numbers group
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 to 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 (45)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
David Level 6, Spain
4 May 2023
Muy buena explicación, de gran ayuda. Mil gracias!!!
Xinbiao Level 9, Australia
16 April 2023
Thank you! this is very helpful for a beginner!
williamthomas96 Level 0, India
10 February 2023
Great article on Numeric Operators in Java! I appreciated how you explained each operator in detail and provided clear examples to help readers understand. Understanding numeric operators is crucial in developing Java programs, and your article made the learning process much easier. If anyone is looking to enhance their Java skills, I highly recommend checking out the Java Coaching Center in Chennai. With experienced instructors and practical hands-on training, they provide an efficient way to learn Java.
Lou Level 6, Spain
31 August 2022
Super clear! Thanks !
Maggie Level 3, London, United Kingdom
5 August 2022
Very informative, thanks
Janneke Level 4, Velp, Netherlands
3 May 2022
Great explanation, very clear, thank you
Andy Lee Level 10
23 February 2021
nice
punkuotukas Level 7, Vilnius, Lithuania
8 September 2020
"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);

Karas Level 20, Tampa, United States
28 August 2020
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
Yet again another useful article, I love CodeGym!