Difference between revisions of "JAVA: Operator Relational dengan Contoh"

From OnnoWiki
Jump to navigation Jump to search
(Created page with "Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various...")
 
 
(10 intermediate revisions by the same user not shown)
Line 1: Line 1:
Operators constitute the basic building block to any programming language. Java too provides many types of operators which can be used according to the need to perform various calculations and functions, be it logical, arithmetic, relational, etc. They are classified based on the functionality they provide.
+
Operator merupakan blok bangunan dasar untuk bahasa pemrograman apa pun. Java juga menyediakan banyak jenis operator yang dapat digunakan sesuai dengan kebutuhan untuk melakukan berbagai perhitungan dan fungsi, baik itu logika, aritmatika, relasional, dll. Diklasifikasikan berdasarkan fungsionalitas yang disediakan.
  
 
Types of Operators:  
 
Types of Operators:  
  
Arithmetic Operators
+
* Operator Arithmetic
Unary Operators
+
* Operator Unary
Assignment Operator
+
* Operator Assignment
Relational Operators
+
* Operator Relational
Logical Operators
+
* Operator Logical
Ternary Operator
+
* Operator Ternary
Bitwise Operators
+
* Operator Bitwise
Shift Operators
+
* Operator Shift
Java Relational Operators are a bunch of binary operators used to check for relations between two operands, including equality, greater than, less than, etc. They return a boolean result after the comparison and are extensively used in looping statements as well as conditional if-else statements and so on. The general format of representing relational operator is:
 
  
Syntax:
+
Operator Relasional Java adalah sekelompok operator biner yang digunakan untuk memeriksa hubungan antara dua operan, termasuk kesetaraan, lebih besar dari, kurang dari, dll. Mereka mengembalikan hasil boolean setelah perbandingan dan banyak digunakan dalam pernyataan perulangan serta if-else bersyarat dan seterusnya. Format umum untuk merepresentasikan operator relasional adalah:
  
variable1 relation_operator variable2
 
Let us look at each one of the relational operators in Java:
 
  
Operator 1: ‘Equal to’ operator (==)
+
Sintaks:
  
This operator is used to check whether the two given operands are equal or not. The operator returns true if the operand at the left-hand side is equal to the right-hand side, else false.
+
variable1 relation_operator variable2
  
Syntax:  
+
Mari kita lihat masing-masing operator relasional di Java:
  
var1 == var2
 
Illustration:
 
  
var1 = "GeeksforGeeks"
+
==Operator 1: ‘Equal to’ operator (==)==
var2 = 20
 
var1 == var2 results in false
 
Example:
 
  
 +
Operator ini digunakan untuk memeriksa apakah dua operan yang diberikan sama atau tidak. Operator mengembalikan nilai true jika operan di ruas kiri sama dengan ruas kanan, jika tidak false.
  
// Java Program to Illustrate equal to Operator
+
 
 +
Sintaks:
 +
 
 +
var1 == var2
 +
 
 +
Ilustrasi:
 +
 
 +
var1 = "GeeksforGeeks"
 +
var2 = 20
 +
var1 == var2 results in false
 +
 
 +
Contoh:
 +
 
 +
// Java Program to Illustrate equal to Operator
 
   
 
   
// Importing I/O classes
+
// Importing I/O classes
import java.io.*;
+
import java.io.*;
   
+
 
// Main class
+
  // Main class
class GFG {
+
class GFG {
+
 
    // Main driver method
+
    // Main driver method
    public static void main(String[] args)
+
    public static void main(String[] args)
    {
+
    {
        // Initializing variables
+
        // Initializing variables
        int var1 = 5, var2 = 10, var3 = 5;
+
        int var1 = 5, var2 = 10, var3 = 5;
+
 
        // Displaying var1, var2, var3
+
        // Displaying var1, var2, var3
        System.out.println("Var1 = " + var1);
+
        System.out.println("Var1 = " + var1);
        System.out.println("Var2 = " + var2);
+
        System.out.println("Var2 = " + var2);
        System.out.println("Var3 = " + var3);
+
        System.out.println("Var3 = " + var3);
+
 
        // Comparing var1 and var2 and
+
        // Comparing var1 and var2 and
        // printing corresponding boolean value
+
        // printing corresponding boolean value
        System.out.println("var1 == var2: "
+
        System.out.println("var1 == var2: "
                          + (var1 == var2));
+
                            + (var1 == var2));
+
 
        // Comparing var1 and var3 and
+
        // Comparing var1 and var3 and
        // printing corresponding boolean value
+
        // printing corresponding boolean value
        System.out.println("var1 == var3: "
+
        System.out.println("var1 == var3: "
                          + (var1 == var3));
+
                            + (var1 == var3));
    }
+
    }
}
+
}
 +
 
 
Output
 
Output
Var1 = 5
+
Var1 = 5
Var2 = 10
+
Var2 = 10
Var3 = 5
+
Var3 = 5
var1 == var2: false
+
var1 == var2: false
var1 == var3: true
+
var1 == var3: true
Operator 2: ‘Not equal to’ Operator(!=)
 
  
This operator is used to check whether the two given operands are equal or not. It functions opposite to that of the equal-to-operator. It returns true if the operand at the left-hand side is not equal to the right-hand side, else false.
+
==Operator 2: ‘Not equal to’ Operator(!=)==
  
Syntax:
+
Operator ini digunakan untuk memeriksa apakah dua operan yang diberikan sama atau tidak. Ini berfungsi berlawanan dengan equal-to-operator. Ini return true jika operan di sisi kiri tidak sama dengan sisi kanan, jika tidak maka false.
  
var1 != var2
 
Illustration:
 
  
var1 = "GeeksforGeeks"
+
Sintaks:
var2 = 20
 
  
var1 != var2 results in true
+
var1 != var2
Example:
 
  
 +
Ilustrasi:
  
// Java Program to Illustrate No- equal-to Operator
+
var1 = "GeeksforGeeks"
 +
var2 = 20
 +
var1 != var2 results in true
 +
 
 +
Contoh:
 +
 
 +
// Java Program to Illustrate No- equal-to Operator
 +
 
 +
// Importing I/O classes
 +
import java.io.*;
 
   
 
   
// Importing I/O classes
+
  // Main class
import java.io.*;
+
class GFG {
   
+
 
// Main class
+
    // Main driver method
class GFG {
+
    public static void main(String[] args)
+
    {
    // Main driver method
+
        // Initializing variables
    public static void main(String[] args)
+
        int var1 = 5, var2 = 10, var3 = 5;
    {
+
 
        // Initializing variables
+
        // Displaying var1, var2, var3
        int var1 = 5, var2 = 10, var3 = 5;
+
        System.out.println("Var1 = " + var1);
+
        System.out.println("Var2 = " + var2);
        // Displaying var1, var2, var3
+
        System.out.println("Var3 = " + var3);
        System.out.println("Var1 = " + var1);
+
 
        System.out.println("Var2 = " + var2);
+
        // Comparing var1 and var2 and
        System.out.println("Var3 = " + var3);
+
        // printing corresponding boolean value
+
        System.out.println("var1 == var2: "
        // Comparing var1 and var2 and
+
                            + (var1 != var2));
        // printing corresponding boolean value
+
 
        System.out.println("var1 == var2: "
+
        // Comparing var1 and var3 and
                          + (var1 != var2));
+
        // printing corresponding boolean value
+
        System.out.println("var1 == var3: "
        // Comparing var1 and var3 and
+
                            + (var1 != var3));
        // printing corresponding boolean value
+
    }
        System.out.println("var1 == var3: "
+
}
                          + (var1 != var3));
+
 
    }
 
}
 
 
Output
 
Output
Var1 = 5
+
Var1 = 5
Var2 = 10
+
Var2 = 10
Var3 = 5
+
Var3 = 5
var1 == var2: true
+
var1 == var2: true
var1 == var3: false
+
var1 == var3: false
Operator 3: ‘Greater than’ operator(>)
 
  
This checks whether the first operand is greater than the second operand or not. The operator returns true when the operand at the left-hand side is greater than the right-hand side.
+
==Operator 3: ‘Greater than’ operator(>)==
  
Syntax:
 
  
var1 > var2
+
Ini memeriksa apakah operan pertama lebih besar dari operan kedua atau tidak. Operator return nilai true ketika operan di sisi kiri lebih besar dari sisi kanan.
Illustration:
 
  
var1 = 30
 
var2 = 20
 
  
var1 > var2 results in true
+
Sintaks:  
Example:
 
  
 +
var1 > var2
 +
 +
Ilustrasi:
 +
 +
var1 = 30
 +
var2 = 20
 +
var1 > var2 results in true
 +
 +
Contoh:
 +
 +
// 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 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));
 
    }
 
}
 
 
Output
 
Output
Var1 = 30
+
Var1 = 30
Var2 = 20
+
Var2 = 20
Var3 = 5
+
Var3 = 5
var1 > var2: true
+
var1 > var2: true
var3 > var1: false
+
var3 > var1: false
Operator 4: ‘Less than’ Operator(<)
 
  
This checks whether the first operand is less than the second operand or not. The operator returns true when the operand at the left-hand side is less than the right-hand side. It functions opposite to that of the greater-than operator.
+
==Operator 4: ‘Less than’ Operator(<)==
  
Syntax:
+
Ini memeriksa apakah operan pertama kurang dari operan kedua atau tidak. Operator return nilai true ketika operan di sisi kiri lebih kecil dari sisi kanan. Ini berfungsi berlawanan dengan greater-than operator.
  
var1 < var2
+
Sintaks:  
Illustration:  
 
  
var1 = 10
+
var1 < var2
var2 = 20
 
  
var1 < var2 results in true
+
Ilustrasi:  
Example:
 
  
 +
var1 = 10
 +
var2 = 20
 +
var1 < var2 results in true
 +
 +
Contoh:
 +
 +
// 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 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));
 
    }
 
}
 
 
Output
 
Output
Var1 = 10
+
Var1 = 10
Var2 = 20
+
Var2 = 20
Var3 = 5
+
Var3 = 5
var1 < var2: true
+
var1 < var2: true
var2 < var3: false
+
var2 < var3: false
Operator 5: Greater than or equal to (>=)
 
  
This checks whether the first operand is greater than or equal to the second operand or not. The operator returns true when the operand at the left-hand side is greater than or equal to the right-hand side.
+
==Operator 5: Greater than or equal to (>=)==
  
Syntax:
+
Ini memeriksa apakah operan pertama lebih besar dari atau sama dengan operan kedua atau tidak. Operator return nilai true ketika operan di sisi kiri lebih besar dari atau sama dengan sisi kanan.
  
var1 >= var2
 
Illustration:
 
  
var1 = 20
+
Sintaks:
var2 = 20
 
var3 = 10
 
  
var1 >= var2 results in true
+
var1 >= var2
var2 >= var3 results in true
 
Example:
 
  
 +
Ilustrasi:
  
// Java Program to Illustrate Greater than or equal to
+
var1 = 20
// Operator
+
var2 = 20
 +
var3 = 10
 
   
 
   
// Importing I/O classes
+
var1 >= var2 results in true
import java.io.*;
+
var2 >= var3 results in true
   
+
 
// Main class
+
Contoh:
class GFG {
+
 
+
// Java Program to Illustrate Greater than or equal to
    // Main driver method
+
// Operator
    public static void main(String[] args)
+
 
    {
+
// Importing I/O classes
        // Initializing variables
+
import java.io.*;
        int var1 = 20, var2 = 20, var3 = 10;
+
 
+
  // Main class
        // Displaying var1, var2, var3
+
class GFG {
        System.out.println("Var1 = " + var1);
+
 
        System.out.println("Var2 = " + var2);
+
    // Main driver method
        System.out.println("Var3 = " + var3);
+
    public static void main(String[] args)
+
    {
        // Comparing var1 and var2 and
+
        // Initializing variables
        // printing corresponding boolean value
+
        int var1 = 20, var2 = 20, var3 = 10;
        System.out.println("var1 >= var2: "
+
 
                          + (var1 >= var2));
+
        // Displaying var1, var2, var3
+
        System.out.println("Var1 = " + var1);
        // Comparing var2 and var3 and
+
        System.out.println("Var2 = " + var2);
        // printing corresponding boolean value
+
        System.out.println("Var3 = " + var3);
        System.out.println("var2 >= var3: "
+
 
                          + (var3 >= var1));
+
        // 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));
 +
    }
 +
}
 +
 
 
Output
 
Output
Var1 = 20
+
Var1 = 20
Var2 = 20
+
Var2 = 20
Var3 = 10
+
Var3 = 10
var1 >= var2: true
+
var1 >= var2: true
var2 >= var3: false
+
var2 >= var3: false
Operator 6: Less than or equal to (<=)
 
  
This checks whether the first operand is less than or equal to the second operand or not. The operator returns true when the operand at the left-hand side is less than or equal to the right-hand side.
+
==Operator 6: Less than or equal to (<=)==
  
Syntax:
+
Ini memeriksa apakah operan pertama kurang dari atau sama dengan operan kedua atau tidak. Operator return nilai true ketika operan di sisi kiri kurang dari atau sama dengan sisi kanan.
  
var1 <= var2
 
Illustration:
 
  
var1 = 10
+
Sintaks:
var2 = 10
 
var3 = 9
 
  
var1 <= var2 results in true
+
var1 <= var2
var2 <= var3 results in false
 
Example:
 
  
 +
Ilustrasi:
  
// Java Program to Illustrate Less
+
var1 = 10
// than or equal to operator
+
var2 = 10
 +
var3 = 9
 
   
 
   
// Importing I/O classes
+
var1 <= var2 results in true
import java.io.*;
+
var2 <= var3 results in false
 +
 
 +
Contoh:
 +
 
 +
// 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);
 
   
 
   
// Main class
+
        // Comparing var1 and var2 and
class GFG {
+
        // printing corresponding boolean value
+
        System.out.println("var1 <= var2: "
    // Main driver method
+
                            + (var1 <= var2));
    public static void main(String[] args)
+
 
    {
+
        // Comparing var2 and var3 and
        // Initializing variables
+
        // printing corresponding boolean value
        int var1 = 10, var2 = 10, var3 = 9;
+
        System.out.println("var2 <= var3: "
+
                            + (var2 <= var3));
        // 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));
 
    }
 
}
 
 
Output
 
Output
Var1 = 10
+
Var1 = 10
Var2 = 10
+
Var2 = 10
Var3 = 9
+
Var3 = 9
var1 <= var2: true
+
var1 <= var2: true
var2 <= var3: false
+
var2 <= var3: false
 
 
  
 
==Referensi==
 
==Referensi==
  
 
* https://www.geeksforgeeks.org/java-relational-operators-with-examples/
 
* https://www.geeksforgeeks.org/java-relational-operators-with-examples/

Latest revision as of 05:06, 7 May 2022

Operator merupakan blok bangunan dasar untuk bahasa pemrograman apa pun. Java juga menyediakan banyak jenis operator yang dapat digunakan sesuai dengan kebutuhan untuk melakukan berbagai perhitungan dan fungsi, baik itu logika, aritmatika, relasional, dll. Diklasifikasikan berdasarkan fungsionalitas yang disediakan.

Types of Operators:

  • Operator Arithmetic
  • Operator Unary
  • Operator Assignment
  • Operator Relational
  • Operator Logical
  • Operator Ternary
  • Operator Bitwise
  • Operator Shift

Operator Relasional Java adalah sekelompok operator biner yang digunakan untuk memeriksa hubungan antara dua operan, termasuk kesetaraan, lebih besar dari, kurang dari, dll. Mereka mengembalikan hasil boolean setelah perbandingan dan banyak digunakan dalam pernyataan perulangan serta if-else bersyarat dan seterusnya. Format umum untuk merepresentasikan operator relasional adalah:


Sintaks:

variable1 relation_operator variable2

Mari kita lihat masing-masing operator relasional di Java:


Operator 1: ‘Equal to’ operator (==)

Operator ini digunakan untuk memeriksa apakah dua operan yang diberikan sama atau tidak. Operator mengembalikan nilai true jika operan di ruas kiri sama dengan ruas kanan, jika tidak false.


Sintaks:

var1 == var2

Ilustrasi:

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

Contoh:

// 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));
    }
}

Output

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

Operator 2: ‘Not equal to’ Operator(!=)

Operator ini digunakan untuk memeriksa apakah dua operan yang diberikan sama atau tidak. Ini berfungsi berlawanan dengan equal-to-operator. Ini return true jika operan di sisi kiri tidak sama dengan sisi kanan, jika tidak maka false.


Sintaks:

var1 != var2

Ilustrasi:

var1 = "GeeksforGeeks"
var2 = 20
var1 != var2 results in true

Contoh:

// 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));
    }
}

Output

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

Operator 3: ‘Greater than’ operator(>)

Ini memeriksa apakah operan pertama lebih besar dari operan kedua atau tidak. Operator return nilai true ketika operan di sisi kiri lebih besar dari sisi kanan.


Sintaks:

var1 > var2

Ilustrasi:

var1 = 30
var2 = 20 
var1 > var2 results in true

Contoh:

// 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));
    }
}

Output

Var1 = 30
Var2 = 20
Var3 = 5
var1 > var2: true
var3 > var1: false

Operator 4: ‘Less than’ Operator(<)

Ini memeriksa apakah operan pertama kurang dari operan kedua atau tidak. Operator return nilai true ketika operan di sisi kiri lebih kecil dari sisi kanan. Ini berfungsi berlawanan dengan greater-than operator.

Sintaks:

var1 < var2

Ilustrasi:

var1 = 10
var2 = 20
var1 < var2 results in true

Contoh:

// 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));
    }
}

Output

Var1 = 10
Var2 = 20
Var3 = 5
var1 < var2: true
var2 < var3: false

Operator 5: Greater than or equal to (>=)

Ini memeriksa apakah operan pertama lebih besar dari atau sama dengan operan kedua atau tidak. Operator return nilai true ketika operan di sisi kiri lebih besar dari atau sama dengan sisi kanan.


Sintaks:

var1 >= var2

Ilustrasi:

var1 = 20
var2 = 20
var3 = 10

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

Contoh:

// 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));
    }
}

Output

Var1 = 20
Var2 = 20
Var3 = 10
var1 >= var2: true
var2 >= var3: false

Operator 6: Less than or equal to (<=)

Ini memeriksa apakah operan pertama kurang dari atau sama dengan operan kedua atau tidak. Operator return nilai true ketika operan di sisi kiri kurang dari atau sama dengan sisi kanan.


Sintaks:

var1 <= var2

Ilustrasi:

var1 = 10
var2 = 10
var3 = 9 

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

Contoh:

// 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));
    }
}

Output

Var1 = 10
Var2 = 10
Var3 = 9
var1 <= var2: true
var2 <= var3: false

Referensi