“This is the 28th day of my participation in the Gwen Challenge in November. Check out the details: The Last Gwen Challenge in 2021.”
๐ author’s home page: Hai Yong๐ Author profile: ๐CSDN full stack quality creator, ๐ฅHDZ core group member ๐ Fan benefits: fans send six books every week and various small gifts from time to time
Operators form the basic building blocks of any programming language. Java also provides many types of operators that you can use on demand to perform various calculations and functions, including logic, arithmetic, relations, and so on. They are classified according to the functions they provide.
Operator type:
- Arithmetic operator
- Unary operator
- The assignment operator
- Relational operator
- Logical operator
- Ternary operator
- Bitwise operators
- Shift operator
Relational operators are a set of binary operators that check the relationship between two operands, including equality, greater than, less than, and so on. They return a Boolean result after comparison and are widely used in loop statements, conditional if-else statements, and so on. The general format for representing relational operators is:
Grammar:
variable1The relational _ operator variable2
Copy the code
Let’s look at each of the relational operators in Java:
Operator 1: “equal” operator (==)
This operator checks whether two given operands are equal. The operator returns true if the left-hand operand is equal to the right-hand operand, false otherwise.
Grammar:
variable1= = variable2
Copy the code
Example:
var1 = "haiyong"
var2 = 20Var1 == var2 is falseCopy the code
Example:
import java.io.*;
class GFG {
// Main driver mode
public static void main(String[] args)
{
// Initialize variables
int var1 = 5, var2 = 10, var3 = 5;
// display var1, var2, var3
System.out.println("Var1 = " + var1);
System.out.println("Var2 = " + var2);
System.out.println("Var3 = " + var3);
// Compare var1 and var2 and print the corresponding Booleans
System.out.println("var1 == var2: "
+ (var1 == var2));
// Compare var1 and var3 and print the corresponding Booleans
System.out.println("var1 == var3: "+ (var1 == var3)); }}Copy the code
The output
Var1 = 5
Var2 = 10
Var3 = 5
var1 == var2: false
var1 == var3: true
Copy the code
Operator 2: the “not equal” operator (! =)
This operator checks whether two given operands are equal. It does the opposite of what the equal operator does. Returns true if the left-hand operand does not equal the right-hand operand, false otherwise.
Grammar:
variable1! = variable2
Copy the code
Example:
var1 = "haiyong"
var2 = 20var1 ! = var2 is trueCopy the code
example
import java.io.*;
class GFG {
// The main driver method
public static void main(String[] args)
{
// Initialize variables
int var1 = 5, var2 = 10, var3 = 5;
// display var1, var2, var3
System.out.println("Var1 = " + var1);
System.out.println("Var2 = " + var2);
System.out.println("Var3 = " + var3);
// Compare var1 and var2 and print the corresponding Booleans
System.out.println("var1 == var2: "+ (var1 ! = var2));// Compare var1 and var3 and print the corresponding Booleans
System.out.println("var1 == var3: "+ (var1 ! = var3)); }} ** output ** ' 'Java Var1 =5
Var2 = 10
Var3 = 5
var1 == var2: true
var1 == var3: false
Copy the code
Operator 3: greater than operator (>)
This checks whether the first operand is greater than the second. The operator returns true when the left-hand operand is greater than the right-hand operand.
Syntax:
variable1> variable2
Copy the code
Example:
var1 = 30
var2 = 20Var1 > var2 is trueCopy the code
Example:
import java.io.*;
class GFG {
// The main driver method
public static void main(String[] args)
{
// Initialize variables
int var1 = 30, var2 = 20, var3 = 5;
// display var1, var2, var3
System.out.println("Var1 = " + var1);
System.out.println("Var2 = " + var2);
System.out.println("Var3 = " + var3);
// Compare var1 and var2 and print the corresponding Booleans
System.out.println("var1 > var2: " + (var1 > var2));
// Compare var1 and var3 and print the corresponding Booleans
System.out.println("var3 > var1: "+ (var3 >= var1)); }} ** output ** ' 'Java Var1 =30
Var2 = 20
Var3 = 5
var1 > var2: true
var3 > var1: false
Copy the code
The author is determined to build a fishing site with 100 small games/tools, update progress: 42/100
I’ve been writing a technology blog for a long time, mostly through nuggets, and this is my article on Java relational operators and examples (above). I like to share technology and happiness through articles. You can visit my blog at juejin.cn/user/204034… For more information. Hope you like it! ๐
๐ welcomes your comments and suggestions in the comments section! ๐