What Does % Mean In Java?


What Does % Mean In Java

Java is a high-level programming language that is based on classes and is object-oriented. This programming language is designed in such a way it uses as few dependencies as possible. We can perform numerous mathematical operations in Java. Java has a rich set of operators that can be used to manipulate data and perform mathematical operations. One of the notable yet confusing operators is the % operator.

The % symbol is used in many programming languages such as Java, C, C++, Javascript, and more. This symbol is a very essential part of every programming language it is used in. This is called the modulo operator. The % or modulo operator in Java-like most other programming languages is used to perform arithmetic operations. This operator is used to return the remainder after a division operation. It firstly divides the left operand with the right operand and returns the remainder after the operation.

Using the % or modulo operator can be quite confusing but worry not we are here. We will try to let you know what % means in Java. Additionally, we will also elaborate on the uses of the % or modulo operator in Java along with some examples of the % modulo operator.

% or the modulo operator is an arithmetic operator in Java. To know about the % operator in Java, firstly you need to know about Java arithmetic operators.

What Are Java Arithmetic Operators?

Java is a high-level programming language with many operators that can be used according to need. These operators can manipulate data held in variables and perform almost all mathematical operations. Operators are a very essential part of every programming language. In fact, When we want to learn a new programming language, we start by learning operators in that language. In this way, you can understand how essential operators are.

We can perform the following mathematical operations with the help of operators in Java,

  • Arithmetic operators
  • Assignment operators
  • Comparison operators
  • Logical operators
  • Bitwise operators

Arithmetic operators – These are the operators that can perform basic mathematical operations such as addition, subtraction, multiplication, or division. 

OperatorNameDescriptionExample
+AdditionAdds two variablesa + b
SubtractionSubtracts two variablesa – b
/DivisionDivides two variablesa/b
%ModulusShows the remainder when one variable is divided by another variablea%b
++IncrementIncreases the value of the variable by 1a++ or ++a
DecrementDecreases the value of the variable by 1a– or –a

What Does The % Or Modulo Operator Do?

The % or modulo operator is an important and primary part of arithmetic operators in every programming language. It is an arithmetic operator that returns the remainder after two variables are divided by one another. It divides the left operand with the right operand, so make sure to place your values accordingly. You must keep in mind that the % or modulo operator only returns positive integer values of remainders that we get after a division operation.

To make the concept more clear, a modulo operator is used to find the remainder when two numbers are divided. For example, we know if we divide 10 by 3 we won’t get an integer number as a quotient. Or,

10/3 = 3 (Here 3 is the quotient with 1 remainder) then

10%3 =1 (Here 1 is the remainder that we got after dividing 10 by 3)

Again,

20/2 = 10 (Here 10 is the quotient with 0 remainders) then

20%2 = 0 (Here 0 is the remainder that we got after dividing 20 by 2 as 20 is divisible by 2)

Similarly,

  • 50%3 = 2
  • 4%3 = 1
  • 6%3 = 0
  • 15%7 = 1
  • 29%5 = 4

Now, in case you want to perform an arithmetic operation with the help of the modulo operator in Java, you can do it with the following code,

public class Main {
  public static void main(String[] args) {
    int x =  10;
    int y = 3;
    System.out.println(x%y);
  }
}

In the following code, we declared two variables x and y. Here, x = 10 and y = 3. We, then print the remainder of the division operation between 10 and 3. The following code will give the output of 1. As we know, when we divide 10 by 3 we get the remainder of value 1. 

Another example of using the modulo function is given below,

public class Main {
  public static void main(String[] args) {
    int x = 20;
    x %= 3;
    System.out.println(x);
  }
}

In the following code, we declared a variable x with the value 20. In the next line, the value 20 is divided by 3 and the remainder value is stored in the variable x. This is called the manipulation of variables. Then we print the value of x and we get the output of 2.

The modulo operator returns the remainder after a remainder operation of two variables. The symbol for the modulo operator is % and it returns a positive integer value. This operator divides the left-hand operand with the right-hand operator. It is an essential arithmetic operator used in almost every programming language including Java.

Samuel Smith

Samuel Smith is a curious person with tremendous experience. He enjoys sharing his story with everyone and is always ready for new opportunities.

Recent Posts