Home » Data Structures using Java »
Sample Code that use Equality and Relational Operators in Java
The equality and relational operators determine if one operand is greater than, less than, equal to, or not equal to another operand. The majority of these operators will probably look familiar to you as well. Keep in mind that you must use "==", not "=", when testing if two primitive values are equal.
== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than <= less than or equal to
The following program, ComparisonDemo, tests the comparison operators:
class ComparisonDemo {
public static void main(String[] args){
int value1 = 1;
int value2 = 2;
if(value1 == value2) System.out.println("value1 == value2");
if(value1 != value2) System.out.println("value1 != value2");
if(value1 > value2) System.out.println("value1 > value2");
if(value1 < value2) System.out.println("value1 < value2");
if(value1 <= value2) System.out.println("value1 <= value2");
}
}
Output:
value1 != value2
value1 < value2
value1 <= value2
Related posts:
If you enjoyed this article, subscribe to receive more great content just like it.
Disclaimer
Labels
- Computer Tips and Tricks (8)
- Content Management System (1)
- Data Structures using Java (15)
- Database Management Systems (2)
- File Formats (7)
- Gadgets Reviews (2)
- Games Reviews (2)
- hardware (16)
- Internet Terms (7)
- Management Information System (5)
- networking (8)
- Programming (15)
- sofware (17)
- Tech News (4)
- Web Design (4)
0 comments for this post
Leave a reply