Last Updated: October 04, 2020
·
455
· a_yelenevych

Numeric operators in Java

Numeric operators in Java

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.

CodeGym

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.

coder

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.

The article was published on <a href="https://codegym.cc/groups/posts/8-numeric-operators-in-java-logical-operators-in-java-bitwise-operators-in-java-operator-precedence-">CodeGym blog</a>.