Relational Operators
These operators are used to show the relationship among the operands. Relational operators compare the values of the variables and result in terms of True or False” (i.e. 0 or 1).
There are six relational operators in Java:
-
Equal to (==): This operator compares two values to check if they are equal. If the two values are equal, it returns true, otherwise, it returns false.
-
Not equal to (!=): This operator compares two values to check if they are not equal. If the two values are not equal, it returns true, otherwise, it returns false.
-
Greater than (>): This operator compares two values to check if the first value is greater than the second value. If the first value is greater than the second value, it returns true, otherwise, it returns false.
-
Less than (<): This operator compares two values to check if the first value is less than the second value. If the first value is less than the second value, it returns true, otherwise, it returns false.
-
Greater than or equal to (>=): This operator compares two values to check if the first value is greater than or equal to the second value. If the first value is greater than or equal to the second value, it returns true, otherwise, it returns false.
-
Less than or equal to (<=): This operator compares two values to check if the first value is less than or equal to the second value. If the first value is less than or equal to the second value, it returns true, otherwise, it returns false.
These operators are commonly used in Java programming for making decisions based on the result of a comparison. For example, they can be used in if statements, while loops, and for loops to control the flow of execution in a program.
public class RelationalOperatorsExample
{
public static void main(String[] args)
{
int num1 = 10;
int num2 = 20;
// Example of the equal to operator
boolean isEqual = (num1 == num2);
System.out.println("num1 == num2: " + isEqual); // Output: false
// Example of the not equal to operator
boolean isNotEqual = (num1 != num2);
System.out.println("num1 != num2: " + isNotEqual); // Output: true
// Example of the greater than operator
boolean isGreaterThan = (num1 > num2);
System.out.println("num1 > num2: " + isGreaterThan); // Output: false
// Example of the less than operator
boolean isLessThan = (num1 < num2);
System.out.println("num1 < num2: " + isLessThan); // Output: true
// Example of the greater than or equal to operator
boolean isGreaterThanOrEqual = (num1 >= num2);
System.out.println("num1 >= num2: " + isGreaterThanOrEqual); // Output: false
// Example of the less than or equal to operator
boolean isLessThanOrEqual = (num1 <= num2);
System.out.println("num1 <= num2: " + isLessThanOrEqual); // Output: true
}
}
Explain
public class RelationalOperatorsExample
declares a public class namedRelationalOperatorsExample
.{
indicates the beginning of the class body.public static void main(String[] args)
is a main method that is declared as public, static, and void. This is the entry point for the program.int num1 = 10;
declares a variable namednum1
of type int and initializes it with the value of 10.int num2 = 20;
declares a variable namednum2
of type int and initializes it with the value of 20.// Example of the equal to operator
is a comment that describes the following code.boolean isEqual = (num1 == num2);
declares a variable namedisEqual
of type boolean and initializes it with the result of the comparison ofnum1
andnum2
using the equal to an operator (==
).System.out.println("num1 == num2: " + isEqual);
prints a message to the console that displays the result of the comparison.// Example of the not equal to operator
is a comment that describes the following code.boolean isNotEqual = (num1 != num2);
declares a variable namedisNotEqual
of type boolean and initializes it with the result of the comparison ofnum1
andnum2
using the not equal to operator (!=
).System.out.println("num1 != num2: " + isNotEqual);
prints a message to the console that displays the result of the comparison.// Example of the greater than operator
is a comment that describes the following code.boolean isGreaterThan = (num1 > num2);
declares a variable namedisGreaterThan
of type boolean and initializes it with the result of the comparison ofnum1
andnum2
using the greater than operator (>
).
Logical Operators
Java uses logical operators AND(&&), OR(||) or NOT(!). These operators yield 1 or 0 depending upon the outcome of different expressions.
There are three logical operators in Java:
- AND operator (&&)
- OR operator (||)
- NOT operator (!)
Here’s a brief explanation of each operator:
-
AND operator (&&): This operator returns true only if both the boolean expressions on either side of it are true. If either one or both of the expressions are false, it returns false.
-
OR operator (||): This operator returns true if at least one of the boolean expressions on either side of it is true. It returns false only if both of the expressions are false.
-
NOT operator (!): This operator returns the opposite boolean value of the expression that it operates on. If the expression is true, it returns false. If the expression is false, it returns true.
Here’s an example code snippet that demonstrates the use of logical operators:
public class LogicalOperatorsExample
{
public static void main(String[] args)
{
int num1 = 10;
int num2 = 20;
int num3 = 30;
// Example of the AND operator
boolean andResult = (num1 < num2) && (num2 < num3);
System.out.println("num1 < num2 AND num2 < num3: " + andResult); // Output: true
// Example of the OR operator
boolean orResult = (num1 > num2) || (num2 < num3);
System.out.println("num1 > num2 OR num2 < num3: " + orResult); // Output: true
// Example of the NOT operator
boolean notResult = !(num1 < num2);
System.out.println("!(num1 < num2): " + notResult); // Output: false
}
}
Explain
public
– a Java keyword used to define the accessibility of a class or method. In this case, the class is declared as public which means it can be accessed from anywhere within the program.class
– a Java keyword used to declare a class.LogicalOperatorsExample
– the name of the class being declared.{
– an opening brace to start the body of the class.public static void main(String[] args)
– a Java method that is called when the program is executed. It is declared as public, static, and void, which means it can be accessed from anywhere, it does not require an object to be instantiated, and it does not return any value.String[] args
is an array of command-line arguments that may be passed to the program.{
– an opening brace to start the body of the method.int num1 = 10;
– a declaration and initialization of an integer variable namednum1
with a value of 10.int num2 = 20;
– a declaration and initialization of an integer variable namednum2
with a value of 20.int num3 = 30;
– a declaration and initialization of an integer variable namednum3
with a value of 30.boolean andResult = (num1 < num2) && (num2 < num3);
– an example of the logical AND operator (&&
). It compares two conditions,num1 < num2
andnum2 < num3
, and returns true only if both conditions are true. The result is assigned to a boolean variable namedandResult
.System.out.println("num1 < num2 AND num2 < num3: " + andResult);
– a statement that prints the result of the logical AND operation to the console.boolean orResult = (num1 > num2) || (num2 < num3);
– an example of the logical OR operator (||
). It compares two conditions,num1 > num2
andnum2 < num3
, and returns true if at least one of the conditions is true. The result is assigned to a boolean variable namedorResult
.System.out.println("num1 > num2 OR num2 < num3: " + orResult);
– a statement that prints the result of the logical OR operation to the console.boolean notResult = !(num1 < num2);
– an example of the logical NOT operator (!
). It negates the result of the conditionnum1 < num2
and returns false if the condition is true and true if the condition is false. The result is assigned to a boolean variable namednotResult
.System.out.println("!(num1 < num2): " + notResult);
– a statement that prints the result of the logical NOT operation to the console.}
– a closing brace to end the body of the method.}
– a closing brace to end the body of the class.