·  JAVA – Syntax – Operators – Comparison

 

- Info:

  - This tutorial shows how to use Comparison Operators in JAVA.: == != < <= > >= instanceof.

  - It also shows how to compare and analyze Strings.

 

- Procedure:

  - Create JAVA Console Application Test.java

 

  Test.java

  public class Test {

 

    public static void main(String[] args) { 

      

      //FOR char, byte, short, int, long, float, double.-------------------------------------------------

      char left  = 65;

      long right = 70;   

      if (left == right) { System.out.println("Left    equals            right."); }

      if (left != right) { System.out.println("Left is different from    right."); }

      if (left <  right) { System.out.println("Left is smaller then      right."); }

      if (left <= right) { System.out.println("Left is smaller or equals right."); }

      if (left >  right) { System.out.println("Left is greater then      right."); }

      if (left >= right) { System.out.println("Left is greater or equals right."); }

      

      //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.-------------------------------------------------------------------------------------

      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\"."       ); }     

      

    }

 

  }