Comparison operators are used to compare values of two variables.
This way they provide a method to direct program flow depending on the outcome.
JAVA comparison operators should be used only for comparing numerical values.
Strings should be compared using predefined JAVA functions designed for that purpose.
Operators
For Numerical values
//FOR char, byte, short, int, long, float, double.-------------------------------------------------
char left = 65;
long right = 70;
if (left == right) { System.out.println("Left equals right."); } // Equal
if (left != right) { System.out.println("Left is different from right."); } // Not equal
if (left < right) { System.out.println("Left is smaller then right."); } // Less than
if (left <= right) { System.out.println("Left is smaller or equals right."); } // Less than or equal
if (left > right) { System.out.println("Left is greater then right."); } // Greater than
if (left >= right) { System.out.println("Left is greater or equals right."); } // Greater than or equal
//FOR boolean.-------------------------------------------------------------------------------------
boolean l = true;
boolean r = false;
if (l == r) { System.out.println("Left equals right."); }
if (l != r) { System.out.println("Left is different from right."); }
For Objects and Strings
//FOR Objects.-------------------------------------------------------------------------------------
String text = "Hello";
if (text instanceof String) { System.out.println("Object is of class String."); }
//FOR Strings.-------------------------------------------------------------------------------------
String text = "Hello";
if (text.equals ("Hello")) { System.out.println("text is equal to \"Hello\"." ); }
if (text.equalsIgnoreCase("hello")) { System.out.println("text is equal to \"hello\"." ); }
if (text.contains ("llo" )) { System.out.println("text contains \"llo\"." ); }
if (text.contentEquals ("Hello")) { System.out.println("text is equal to \"Hello\"." ); }
if (text.startsWith ("He" )) { System.out.println("text starts with \"He\"." ); }
if (text.startsWith ("llo",2)) { System.out.println("text at index 2 starts with \"llo\"."); }
if (text.endsWith ("llo" )) { System.out.println("text ends with \"llo\"." ); }
if (text.matches ("Hello")) { System.out.println("text matches regex \"Hello\"." ); }