Home > Common Problem > What does % mean in Java

What does % mean in Java

青灯夜游
Release: 2023-03-06 16:48:48
Original
14429 people have browsed it

In Java, "%" means remainder. It is a binary arithmetic operator that can perform division operations and obtain the remainder. The syntax is "operand 1 % operand 2". The operand of the remainder operator "%" is usually a positive integer or a negative number or even a floating point number. If a negative number participates in this operation, the result depends on whether the previous number is positive or negative.

What does % mean in Java

The operating environment of this tutorial: windows7 system, java8 version, DELL G3 computer.

% in Java means remainder, which is an arithmetic operator that can implement remainder operations, perform division operations and obtain the remainder.

The remainder operator is %, which is a binary operator. Its operand is usually a positive integer or a negative number or even a floating point number. If a negative number participates in this operation, the sign of the result depends on the previous one. Whether the number is positive or negative.

For integers, Java’s remainder operation rules are as follows

a%b=a-(a/b)*b 
5%3=5-(5/3)*3=2 
5%-3=5-(5/-3)*-3=2 
-5%3=-5-(-5/3)*3=-2 
-5%-3=-5-(-5/-3)*-3=-2
Copy after login

If there are floating point numbers in the operand, the rule used is

a%b=a- (b*q), whereq=int(a/b)

5.2%3.1=5.2-1*3.1=2.1 
5.2%-3.1=5.2-(-1)*(-3.1)=2.1 
-5.2%3.1=-5.1-(-1)*3.1=-2.1 
-5.2%-3.1=-5.1-(-1)*(-3.1)=-2.1
Copy after login

Extended knowledge:

Arithmetic in Java Operators are mainly used to organize arithmetic operations on numerical data. They can be divided into unary operators and binary operators according to the different operands involved in the operation.

Unary operators

There are three arithmetic unary operations, namely -, and --. See Table 1 for specific instructions.

##Self-incrementGet the value first and then add one, or add one first and then get the valuea or a##- -In Table 1, -a is the inversion operation of a, and a or a-- is to add or subtract one to a after the expression operation is completed. And a or --a first adds or subtracts one to a, and then performs the expression operation.
Table 1 Unary arithmetic operations
OperatorNameDescriptionExample
- Negation symbol Negation operationb=-a
decrement by oneFirst take the value and then subtract one, or first subtract one and then take the valuea-- or--a
int a = 12;
System.out.println(-a);
int b = a++;
System.out.println(b);
b = ++a;
System.out.println(b);
Copy after login

The second line of the above code is -a, which inverts the a variable, and the result output is -12. The fourth line of code is to first assign a to the b variable and then add one, that is, assign the value first and then , so the output result is 12. The 6th line of code adds one to a, and then assigns a to the b variable, that is, assigning values ​​one after another, so the output result is 14.

The output result is as shown below:

What does % mean in Java

Binary operators

Arithmetic operators in Java language The function of is to perform arithmetic operations. In addition to the frequently used addition ( ), subtraction (-), multiplication (*) and division (\), there is also the modulo operation (%). Addition ( ), subtraction (-), multiplication (*), and division (\) have the same meaning as the mathematical operations we usually come into contact with. See Table 2 for specific instructions.

Table 2 Binary arithmetic operationsOperator-*/ ##%Find the remainder when a is divided by b a % b

算术运算符都是双目运算符,即连接两个操作数的运算符。优先级上,*、/、% 具有相同运算级别,并高于 +、-(+、- 具有相同级别)。例如:

int a = 4, b = 2, c = 3;
int d = a * (b + c) % c;
Copy after login

这种运算规则与数学运算中的规则是相同的。首先计算赋值符号(=)右边配对的括号内的值,其次按从左向右的结合方向计算乘法,最后做求余运算,表达式的结果为 2, 然后把 2 赋值给 d。

例如:

  • int x=2,y=1;表达式 y/x 的结果是 0。

  • float x=2.0f; int y=1; 表达式 y/x 的结果是 0.5。

在 ① 中整型变量 x 和 y 相除,其结果仍为整型数据 0;在 ② 中由于两个不同类型的数据进行运算,此时首先要进行类型转换,会把 int 型的 y 转换成与 x 一样的 float 型,然后相除,最终结果为 float 类型的数字 0.5。

【例1】编写一个程序,输出不同类型的两个数,执行相加、相减、相乘、相除和求余后输入结果。

public static void main(String[] args) {
    float f1 = 9 % 4;// 保存取余后浮点类型的结果
    double da = 9 + 4.5; // 双精度加法
    double db = 9 - 3.0; // 双精度减法
    double dc = 9 * 2.5; // 双精度乘法
    double dd = 9 / 3.0; // 双精度除法
    double de = 9 % 4; // 双精度取余
    System.out.println("整数的算术运算"); // 整数的加、减、乘、除和取余
    System.out.printf("9+4=%d \n", 9 + 4);
    System.out.printf("9-4=%d \n", 9 - 4);
    System.out.printf("9*4=%d \n", 9 * 4);
    System.out.printf("9/4=%d \n", 9 / 4);
    System.out.printf("9%%4=%d \n", 9 % 4);
    System.out.println("\n浮点数的算术运算"); // 浮点数的加、减、乘、除和取余
    System.out.printf("9+4.5f=%f \n", 9 + 4.5f);
    System.out.printf("9-3.0f=%f \n", 9 - 3.0f);
    System.out.printf("9*2.5f=%f \n", 9 * 2.5f);
    System.out.printf("9/3.0f=%f \n", 9 / 3.0f);
    System.out.printf("9%%4=%f \n", f1);
    System.out.println("\n双精度数的算术运算"); // 双精度数的加、减、乘、除和取余
    System.out.printf("9+4.5=%4.16f \n", da);
    System.out.printf("9-3.0=%4.16f \n", db);
    System.out.printf("9*2.5=%4.16f \n", dc);
    System.out.printf("9/3.0=%4.16f \n", dd);
    System.out.printf("9%%4=%4.16f \n", de);
    System.out.println("\n字符的算术运算"); // 对字符的加法和减法
    System.out.printf("'A'+32=%d \n", 'A' + 32);
    System.out.printf("'A'+32=%c \n", 'A' + 32);
    System.out.printf("'a'-'B'=%d \n", 'a' - 'B');
}
Copy after login

保存文件并运行,输出的结果如下所示。

What does % mean in Java

本示例中使用了 4 种类型来执行算术运算。其中,整数类型的结果最容易理解,浮点型和双精度型返回的结果都带有小数,字符型将会把字符转换为 ASCII 码再运算。

从输出结果中可以看到,整数之间的运算结果只保留整数部分,浮点型运算时保留 6 位小数部分,双精度运算时则保留 16 位小数部分。

注意:Java 语言算术运算符的优先级是先乘除后加减。例如在表达式“a-b*c”中,b 的左侧为减号,右侧为乘号,而乘号优先级高于减号,因此该表达式可以转换为“a-(b*c)”。

如果在一个表达式中的多个算术运算符的优先级别相同,例如“a-b+c”,此时将按照运算符的结合方向决定顺序。算术运算符的结合方向都是“从左至右”,即先左后右。因此 b 先与减号结合,执行“a-b”的运算,再执行加 c 的运算。

更多编程相关知识,请访问:编程教学!!

The above is the detailed content of What does % mean in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
NameDescriptionExample
Add Find the sum of a plus b. It can also be used for String type to perform string concatenation operationsa b
minusFind the difference of a minus ba - b
MultiplyFind the product of a times ba * b
Division
Find the quotient of a divided by ba / b
Remainder