Relational operators used to compare any combination of integers, floating-point numbers, or characters. The result of relational operators is always in a boolean
value, true
or false
. It is mostly used in an if
statement test.
There are four relational operators in Java:
>
greater than>=
greater than or equal to<
less than<=
less than or equal to
package org.kodejava.basic;
public class RelationalDemo {
public static void main(String[] args) {
int value1 = 10, value2 = 25;
int age = 15;
double salary = 1000d;
char char1 = 'd', char2 = 'f';
if (value1 > value2) {
System.out.format("%d is greater than %d %n", value1, value2);
} else {
System.out.format("%d is greater than %d %n", value2, value1);
}
if (age >= 12) {
System.out.format("Hey, I am not a kid anymore %n");
}
if (char1 < char2) {
System.out.format("%c is less than %c %n", char1, char2);
} else {
System.out.format("%c is less than %c %n", char2, char1);
}
if (salary <= 3000d) {
System.out.println("Entry-level Staff");
}
}
}
An here are the result of the program:
25 is greater than 10
Hey, I am not a kid anymore
d is less than f
Entry-level Staff
Latest posts by Wayan (see all)
- How do I create a string of repeated characters? - September 1, 2023
- How do I convert datetime string with optional part to a date object? - August 28, 2023
- How do I split large excel file into multiple smaller files? - April 15, 2023