Java算术运算符与示例
运算符构成任何编程语言的基本构建块。 Java也提供了许多类型的运算符,可以根据执行各种计算和功能的需要使用,如逻辑、算术、关系等。它们根据它们提供的功能进行分类。这里有几种类型:
- 算术运算符
- 一元运算符
- 赋值运算符
- 关系运算符
- 逻辑运算符
- 三元运算符
- 位运算符
- 移位运算符
本文解释了有关算术运算符的所有知识。
算术运算符
这些运算符涉及数学运算符,可用于对称为操作数的原始数据类型执行各种简单或高级算术运算。这些运算符由可应用于单个或两个操作数的各种一元和二元运算符符组成。让我们看看算术运算符下Java必须提供的各种运算符运算符。
现在让我们看看Java中的每一个算术运算符:
1. Addition(+):该运算符是二元运算符,用于将两个操作数相加。
句法:
num1 + num2
例子:
num1 = 10, num2 = 20
sum = num1 + num2 = 30
Java
// Java code to illustrate Addition operator
import java.io.*;
class Addition {
public static void main(String[] args)
{
// initializing variables
int num1 = 10, num2 = 20, sum = 0;
// Displaying num1 and num2
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
// adding num1 and num2
sum = num1 + num2;
System.out.println("The sum = " + sum);
}
}
Java
// Java code to illustrate Subtraction operator
import java.io.*;
class Subtraction {
public static void main(String[] args)
{
// initializing variables
int num1 = 20, num2 = 10, sub = 0;
// Displaying num1 and num2
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
// subtracting num1 and num2
sub = num1 - num2;
System.out.println("Subtraction = " + sub);
}
}
Java
// Java code to illustrate Multiplication operator
import java.io.*;
class Multiplication {
public static void main(String[] args)
{
// initializing variables
int num1 = 20, num2 = 10, mult = 0;
// Displaying num1 and num2
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
// Multiplying num1 and num2
mult = num1 * num2;
System.out.println("Multiplication = " + mult);
}
}
Java
// Java code to illustrate Division operator
import java.io.*;
class Division {
public static void main(String[] args)
{
// initializing variables
int num1 = 20, num2 = 10, div = 0;
// Displaying num1 and num2
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
// Dividing num1 and num2
div = num1 / num2;
System.out.println("Division = " + div);
}
}
Java
// Java code to illustrate Modulus operator
import java.io.*;
class Modulus {
public static void main(String[] args)
{
// initializing variables
int num1 = 5, num2 = 2, mod = 0;
// Displaying num1 and num2
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
// Remaindering num1 and num2
mod = num1 % num2;
System.out.println("Remainder = " + mod);
}
}
输出
num1 = 10
num2 = 20
The sum = 30
2、减法(-):该运算符是二元运算符,用于将两个操作数相减。
句法:
num1 - num2
例子:
num1 = 20, num2 = 10
sub = num1 - num2 = 10
Java
// Java code to illustrate Subtraction operator
import java.io.*;
class Subtraction {
public static void main(String[] args)
{
// initializing variables
int num1 = 20, num2 = 10, sub = 0;
// Displaying num1 and num2
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
// subtracting num1 and num2
sub = num1 - num2;
System.out.println("Subtraction = " + sub);
}
}
输出
num1 = 20
num2 = 10
Subtraction = 10
3.乘法(*):该运算符是二元运算符,用于将两个操作数相乘。
句法:
num1 * num2
例子:
num1 = 20, num2 = 10
mult = num1 * num2 = 200
Java
// Java code to illustrate Multiplication operator
import java.io.*;
class Multiplication {
public static void main(String[] args)
{
// initializing variables
int num1 = 20, num2 = 10, mult = 0;
// Displaying num1 and num2
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
// Multiplying num1 and num2
mult = num1 * num2;
System.out.println("Multiplication = " + mult);
}
}
输出
num1 = 20
num2 = 10
Multiplication = 200
4. 除法(/):这是一个二元运算符,用于将第一个操作数(除数)除以第二个操作数(除数)并给出商作为结果。
句法:
num1 / num2
例子:
num1 = 20, num2 = 10
div = num1 / num2 = 2
Java
// Java code to illustrate Division operator
import java.io.*;
class Division {
public static void main(String[] args)
{
// initializing variables
int num1 = 20, num2 = 10, div = 0;
// Displaying num1 and num2
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
// Dividing num1 and num2
div = num1 / num2;
System.out.println("Division = " + div);
}
}
输出
num1 = 20
num2 = 10
Division = 2
5. 模数(%):这是一个二元运算符,用于在第一个操作数(被除数)除以第二个操作数(除数)时返回余数。
句法:
num1 % num2
例子:
num1 = 5, num2 = 2
mod = num1 % num2 = 1
Java
// Java code to illustrate Modulus operator
import java.io.*;
class Modulus {
public static void main(String[] args)
{
// initializing variables
int num1 = 5, num2 = 2, mod = 0;
// Displaying num1 and num2
System.out.println("num1 = " + num1);
System.out.println("num2 = " + num2);
// Remaindering num1 and num2
mod = num1 % num2;
System.out.println("Remainder = " + mod);
}
}
输出
num1 = 5
num2 = 2
Remainder = 1