📜  带有示例的Java关系运算符

📅  最后修改于: 2022-05-13 01:55:23.274000             🧑  作者: Mango

带有示例的Java关系运算符

运算符构成任何编程语言的基本构建块。 Java也提供了许多类型的运算符,可以根据执行各种计算和功能的需要使用,如逻辑、算术、关系等。它们根据它们提供的功能进行分类。

运算符类型:

  1. 算术运算符
  2. 一元运算符
  3. 赋值运算符
  4. 关系运算符
  5. 逻辑运算符
  6. 三元运算符
  7. 位运算符
  8. 移位运算符

Java关系运算符是一组用于检查两个操作数之间关系的二元运算符,包括相等、大于、小于等。它们在比较后返回布尔结果,广泛用于循环语句以及条件 if- else 语句等。表示关系运算符的一般格式为:

句法:

variable1 relation_operator variable2

让我们看一下Java中的每一个关系运算符:

运算符 1:“等于”运算符(==)

该运算符用于检查两个给定的操作数是否相等。如果左侧的操作数等于右侧,则运算符返回 true,否则返回 false。

句法:

var1 == var2

插图:

var1 = "GeeksforGeeks"
var2 = 20
var1 == var2 results in false

例子:

Java
// Java Program to Illustrate equal to Operator
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Initializing variables
        int var1 = 5, var2 = 10, var3 = 5;
 
        // Displaying var1, var2, var3
        System.out.println("Var1 = " + var1);
        System.out.println("Var2 = " + var2);
        System.out.println("Var3 = " + var3);
 
        // Comparing var1 and var2 and
        // printing corresponding boolean value
        System.out.println("var1 == var2: "
                           + (var1 == var2));
 
        // Comparing var1 and var3 and
        // printing corresponding boolean value
        System.out.println("var1 == var3: "
                           + (var1 == var3));
    }
}


Java
// Java Program to Illustrate No- equal-to Operator
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Initializing variables
        int var1 = 5, var2 = 10, var3 = 5;
 
        // Displaying var1, var2, var3
        System.out.println("Var1 = " + var1);
        System.out.println("Var2 = " + var2);
        System.out.println("Var3 = " + var3);
 
        // Comparing var1 and var2 and
        // printing corresponding boolean value
        System.out.println("var1 == var2: "
                           + (var1 != var2));
 
        // Comparing var1 and var3 and
        // printing corresponding boolean value
        System.out.println("var1 == var3: "
                           + (var1 != var3));
    }
}


Java
// Java code to Illustrate Greater than operator
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Initializing variables
        int var1 = 30, var2 = 20, var3 = 5;
 
        // Displaying var1, var2, var3
        System.out.println("Var1 = " + var1);
        System.out.println("Var2 = " + var2);
        System.out.println("Var3 = " + var3);
 
        // Comparing var1 and var2 and
        // printing corresponding boolean value
        System.out.println("var1 > var2: " + (var1 > var2));
 
        // Comparing var1 and var3 and
        // printing corresponding boolean value
        System.out.println("var3 > var1: "
                           + (var3 >= var1));
    }
}


Java
// Java code to Illustrate Less than Operator
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Initializing variables
        int var1 = 10, var2 = 20, var3 = 5;
 
        // Displaying var1, var2, var3
        System.out.println("Var1 = " + var1);
        System.out.println("Var2 = " + var2);
        System.out.println("Var3 = " + var3);
 
        // Comparing var1 and var2 and
        // printing corresponding boolean value
        System.out.println("var1 < var2: " + (var1 < var2));
 
        // Comparing var2 and var3 and
        // printing corresponding boolean value
        System.out.println("var2 < var3: " + (var2 < var3));
    }
}


Java
// Java Program to Illustrate Greater than or equal to
// Operator
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Initializing variables
        int var1 = 20, var2 = 20, var3 = 10;
 
        // Displaying var1, var2, var3
        System.out.println("Var1 = " + var1);
        System.out.println("Var2 = " + var2);
        System.out.println("Var3 = " + var3);
 
        // Comparing var1 and var2 and
        // printing corresponding boolean value
        System.out.println("var1 >= var2: "
                           + (var1 >= var2));
 
        // Comparing var2 and var3 and
        // printing corresponding boolean value
        System.out.println("var2 >= var3: "
                           + (var3 >= var1));
    }
}


Java
// Java Program to Illustrate Less
// than or equal to operator
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Initializing variables
        int var1 = 10, var2 = 10, var3 = 9;
 
        // Displaying var1, var2, var3
        System.out.println("Var1 = " + var1);
        System.out.println("Var2 = " + var2);
        System.out.println("Var3 = " + var3);
 
        // Comparing var1 and var2 and
        // printing corresponding boolean value
        System.out.println("var1 <= var2: "
                           + (var1 <= var2));
 
        // Comparing var2 and var3 and
        // printing corresponding boolean value
        System.out.println("var2 <= var3: "
                           + (var2 <= var3));
    }
}


输出
Var1 = 5
Var2 = 10
Var3 = 5
var1 == var2: false
var1 == var3: true

运算符 2:“不等于”运算符(!=)

该运算符用于检查两个给定的操作数是否相等。它的作用与等于运算符的作用相反。如果左侧的操作数不等于右侧,则返回 true,否则返回 false。

句法:

var1 != var2

插图:

var1 = "GeeksforGeeks"
var2 = 20

var1 != var2 results in true

例子:

Java

// Java Program to Illustrate No- equal-to Operator
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Initializing variables
        int var1 = 5, var2 = 10, var3 = 5;
 
        // Displaying var1, var2, var3
        System.out.println("Var1 = " + var1);
        System.out.println("Var2 = " + var2);
        System.out.println("Var3 = " + var3);
 
        // Comparing var1 and var2 and
        // printing corresponding boolean value
        System.out.println("var1 == var2: "
                           + (var1 != var2));
 
        // Comparing var1 and var3 and
        // printing corresponding boolean value
        System.out.println("var1 == var3: "
                           + (var1 != var3));
    }
}
输出
Var1 = 5
Var2 = 10
Var3 = 5
var1 == var2: true
var1 == var3: false

运算符 3:“大于”运算符(>)

这将检查第一个操作数是否大于第二个操作数。当左侧的操作数大于右侧的操作数时,运算运算符返回 true。

句法:

var1 > var2

插图:

var1 = 30
var2 = 20

var1 > var2 results in true

例子:

Java

// Java code to Illustrate Greater than operator
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Initializing variables
        int var1 = 30, var2 = 20, var3 = 5;
 
        // Displaying var1, var2, var3
        System.out.println("Var1 = " + var1);
        System.out.println("Var2 = " + var2);
        System.out.println("Var3 = " + var3);
 
        // Comparing var1 and var2 and
        // printing corresponding boolean value
        System.out.println("var1 > var2: " + (var1 > var2));
 
        // Comparing var1 and var3 and
        // printing corresponding boolean value
        System.out.println("var3 > var1: "
                           + (var3 >= var1));
    }
}
输出
Var1 = 30
Var2 = 20
Var3 = 5
var1 > var2: true
var3 > var1: false

运算符 4:“小于”运算符 (<)

这将检查第一个操作数是否小于第二个操作数。当左侧的操作数小于右侧的操作数时,运算运算符返回 true。它的作用与大于运算符相反。

句法:

var1 < var2

插图:

var1 = 10
var2 = 20

var1 < var2 results in true

例子:

Java

// Java code to Illustrate Less than Operator
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Initializing variables
        int var1 = 10, var2 = 20, var3 = 5;
 
        // Displaying var1, var2, var3
        System.out.println("Var1 = " + var1);
        System.out.println("Var2 = " + var2);
        System.out.println("Var3 = " + var3);
 
        // Comparing var1 and var2 and
        // printing corresponding boolean value
        System.out.println("var1 < var2: " + (var1 < var2));
 
        // Comparing var2 and var3 and
        // printing corresponding boolean value
        System.out.println("var2 < var3: " + (var2 < var3));
    }
}
输出
Var1 = 10
Var2 = 20
Var3 = 5
var1 < var2: true
var2 < var3: false

运算符 5:大于或等于 (>=)

这将检查第一个操作数是否大于或等于第二个操作数。当左侧的操作数大于或等于右侧时,运算运算符返回 true。

句法:

var1 >= var2

插图:

var1 = 20
var2 = 20
var3 = 10

var1 >= var2 results in true
var2 >= var3 results in true

例子:

Java

// Java Program to Illustrate Greater than or equal to
// Operator
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Initializing variables
        int var1 = 20, var2 = 20, var3 = 10;
 
        // Displaying var1, var2, var3
        System.out.println("Var1 = " + var1);
        System.out.println("Var2 = " + var2);
        System.out.println("Var3 = " + var3);
 
        // Comparing var1 and var2 and
        // printing corresponding boolean value
        System.out.println("var1 >= var2: "
                           + (var1 >= var2));
 
        // Comparing var2 and var3 and
        // printing corresponding boolean value
        System.out.println("var2 >= var3: "
                           + (var3 >= var1));
    }
}
输出
Var1 = 20
Var2 = 20
Var3 = 10
var1 >= var2: true
var2 >= var3: false

运算符 6:小于或等于 (<=)

这将检查第一个操作数是否小于或等于第二个操作数。当左侧的操作数小于或等于右侧时,运算运算符返回 true。

句法:

var1 <= var2

插图:

var1 = 10
var2 = 10
var3 = 9

var1 <= var2 results in true
var2 <= var3 results in false

例子:

Java

// Java Program to Illustrate Less
// than or equal to operator
 
// Importing I/O classes
import java.io.*;
 
// Main class
class GFG {
 
    // Main driver method
    public static void main(String[] args)
    {
        // Initializing variables
        int var1 = 10, var2 = 10, var3 = 9;
 
        // Displaying var1, var2, var3
        System.out.println("Var1 = " + var1);
        System.out.println("Var2 = " + var2);
        System.out.println("Var3 = " + var3);
 
        // Comparing var1 and var2 and
        // printing corresponding boolean value
        System.out.println("var1 <= var2: "
                           + (var1 <= var2));
 
        // Comparing var2 and var3 and
        // printing corresponding boolean value
        System.out.println("var2 <= var3: "
                           + (var2 <= var3));
    }
}
输出
Var1 = 10
Var2 = 10
Var3 = 9
var1 <= var2: true
var2 <= var3: false