Last Updated:

Picking up the remainder of a division operator in Java

Arithmetic operations are an integral part of any programming language. After all, there is little that can be created in a language that does not support working with numbers, but only with text. Any program processes numeric data.

It is also known from arithmetic that numbers can be added, multiplied to divide and subtracted. All this is done at the expense of operators: +, — ,/ , *, and others. Also, mathematical functions are provided in the language to perform complex algebraic calculations. But now it is the operators who are more interested, so we will analyze each of them in more detail.

Arithmetic

 

Let's consider the basic arithmetic operators on examples and try them out in action

Addition

 

The "+" operator is responsible for addition:

int x = 5;
int y = 25;
int z = x+y;
System.out.println(z); 30

Subtraction

 

The operator "-" is responsible for subtraction:

int x = 5;
int y = 25;

z = y-x;
System.out.println(z); 20

Division

 

Division is performed by a slash operator"/":

int x = 5;
int y = 25;

z = y/x;
System.out.println(z); 5

Multiplication

 

For multiplication, the operator "*" is provided:

int x = 5;
int y = 25;

z = y*x;
System.out.println(z); 125

Increment and decrement

 

To increase the value by 1 or more, and then save it to the same variable from which it was obtained, perform the following operation: x = x + 1.

Here, we took the value with a variable, increased it by 1, and stored it back to the same variable. However, there is an easier way to increase or decrease the value of a variable by 1 – use an increment and a decrement.

The increment is expressed by the operator "++" or "—". There are both prefix increments that are placed before the variable, postfix ones are placed to the right of it.

Example:

int x = 50;

x++ // The postfix increment stores the value 50 until the next System.out.println(x) statement
; 1 is added and 51 is output.
// Now the prefix increment
++x; immediately 1 is added and now the value is 52

If you want to add and save more than one, then do this:

int x = 50;
x+=10; Same as x=x+10, the result is 60;
x*=2; multiply by 2, get the x value of 120;

The same can be done with the rest of the arithmetic operators. Now let's look at everything in one example:

int a = 1;

a = a+1; a = 2
a += 5; a = 7
++a; a = 8
a /=2; a = 4

Operator priorities in large expressions

Suppose we need to calculate to get the result of such an expression:

int a = 5 * 6+6 + (++5);

A result of 32 is clearly expected here. However, the reality is quite different:

  1. The first is executed (++5).
  2. Then 5 * 6.
  3. Then 30 + 6.
  4. At the end of 36 + 6.
  5. The result will be 42.

The fact is that in the arithmetic operations of programming there are priorities and they are arranged as follows:

  1. Increments or decrements are performed first.
  2. Then multiplication, division, or the remainder of division.
  3. Then only add or subtract.

It is worth considering this, because in mathematics multiplication is performed first. In programming, decrements and increments have the highest priority.

Division with residue

Division with a residue is also practiced in most programming languages. To do this, the operator "%" is provided. When dividing even numbers, zero always returns. When dividing odd numbers, tenths of the number are added.

Example:

System.out.println(3%2) // 1

This result occurred for the following reasons:

  1. 3/2 = 1.5;
  2. two and a half equals 3;
  3. discard the unit and add 0.5 + 0.5 = 1.

Another example:

printf(7%4); // 3

Let's see why this result was obtained:

  1. 7/4 = 1.75;
  2. 0.75 * 4 = 3;
  3. 3 + 4 = 7.

As you can see, the remainder of this expression is the discarding of integer fractions and summing tenths or hundredths or thousandths depending on the number.

Division with the remainder of even numbers. Example:

8%4 // 0

When dividing with a remainder of even numbers, the result is always 0.

The "+" operator for the String and Char type

Using addition for string literals, we get their concatenation.

Example:

System.out.println(«Hello » + «World!»); // Hello World

When you add a number with a string, the result is as follows:

System.out.println("HELLO" + 10); // HELLO10 - number converted to string
System.out.println("10"+10); // 1010
System.out.println("10" * 10); // There will be a Type Error

The peculiarity of the Java language is that here the addition of strings with numbers leads to the fact that the type of number is converted into a string and concatination occurs. Multiply division and subtraction in this case will lead to an error.

As for the Char type, the addition works differently. Each letter character has its own UTF-8 code, expressed as an integer. For example, the character 'b' in numerical format is expressed by the number 98, space - 32, 'a' - 97 and so on. Now let's try to perform a mathematical operation with symbols.

Example:

System.out.println (‘b’ + 10); // 98 + 10 = 100

If you add a character to a number, the symbol is automatically converted to numeric format. Adding two characters also produces a numeric format.

System.out.println(‘a’+’b’); // 98+97 = 195

The same thing happens when symbols are multiplied, divided, or subtracted. With any arithmetic operations on characters, they are always translated into numerical format, returning the corresponding result.

Summing Up

 

Java provides all the necessary set of arithmetic operators: addition, subtraction, multiplication, division, division with the remainder. You cannot perform arithmetic operations on string literals. However, this is possible when working with symbols. When you use arithmetic operators over characters, they are converted to a numeric format that expresses the character code in UTF-8.

The list of arithmetic operators is limited, but sufficient. More complex expressions can be solved by the functions of the Math class. This and finding cosines, tangents, rounding, gaining access to mathematical constants and much more. These functions solve complex expressions that require knowledge of the basics of algebra or geometry.

Arithmetic operations have priorities. If you create a complex expression with all the available operations, the calculation will not be performed from left to right, but depending on the priority of the operation: increment/decrement, multiplication/division, and then only addition or subtraction. It is worth considering this, as the result may be different from the expected.

Arithmetic operators are the primary tool for manipulating data in any programming language. After all, without numerical data, it is impossible to create a program that would solve the problems of users.