Bright Java Tutorial

is brought to you by DoITBright LLC

Java

Equality And Relational Operators

There are a lot of times when we need to know if an operand is equal to, not equal to, greater than, greater than or equal to, less than, less than or equal to another operand. In this tutorial, we will be looking into examples on how to accomplish those things.

  package com.example.core;

  public class EqualityAndRelational {

	public static void main(String[] args) {
	  int anInt1 = 1;
	  int anInt2 = 2;
	  long aLong1 = 1;
	  //comparing numbers
	  if(anInt1 == aLong1) {
		System.out.println("anInt1 equal to aLong1");
	  } else {
		System.out.println("anInt1 not equal to aLong1");
	  }
	  /*
	   * As expected, the numbers are equal.
	   * The '==' is the symbol used to compare
	   * the equality of 2 numbers.
	   */
	  if(anInt2 != aLong1) {
		System.out.println("anInt2 not equal to aLong1");
	  } else {
		System.out.println("anInt2 equal to aLong1");
	  }
	  /*
	   * The numbers are not equal.
	   * The '!=' is the symbol used to compare
	   * the unequality of 2 numbers. In general,
	   * the '!' means NOT.
	   */
	  if(anInt1 < anInt2) {
		System.out.println("anInt1 less than anInt2");
	  } else {
		System.out.println("anInt1 greater than anInt2");
	  }
	  /*
	   * As expected, anInt1 is less than anInt2.
	   * The '<' is the symbol for LESS THAN.
	   */
	  if(anInt2 > anInt1) {
        System.out.println("anInt2 greater than anInt1");
      } else {
        System.out.println("anInt2 less than anInt1");
      }
	  /*
	   * anInt2 is greater than anInt1
	   * The '>' is the symbol for GREATER THAN.
	   */
	  if(anInt1 >= aLong1) {
		System.out.println("anInt1 greater than " 
	      + "or equal to aLong1");
	  } else {
		System.out.println("anInt1 not greater than " 
	      + "or equal to aLong1");
	  }
	  /*
	   * The '>=' is the symbol for
	   * GREATER THAN OR EQUAL TO.
	   * It is like doing an if condition like this...
	   * if(anInt1 > aLong1 || anInt1 == aLong1)
	   */
	  if(anInt1 < anInt2) {
		System.out.println("anInt1 less than " 
	      + "or equal to anInt2");
	  } else {
		System.out.println("anInt1 not less than " 
	      + "or equal to anInt2");
	  }
	  /*
	   * The '<=' is the symbol for
	   * LESS THAN OR EQUAL TO.
	   * The above example is equivalent to
	   * if(anInt1 < anInt2 || anInt2 == anInt2)
	   */
	  String aString1 = "abc";
	  String aString2 = "ABC";
	  //comparing Strings
	  if(aString1.equalsIgnoreCase(aString2)) {
		System.out.println("abc is equal to ABC " 
	      + "when case not considered.");
	  }
	  /*
	   * The String equals and equalsIgnoreCase methods
	   * are for comparing the equality between 2 String
	   * operands. The equals method is case sensitive.
	   */
	  if(!aString1.equals(aString2)) {
		System.out.println("abc is equal to ABC " 
	      + "when case not considered.");
	  }
	  /*
	   * The aString1.equals(aString2) will result
	   * into boolean false value. The reason is case
	   * sensitivity. However, we used 
	   * the '!' symbol that resulted to negation of
	   * the condition. It is something like...
	   * If aString1 and aString2 are not equal,
	   * do this.
	   */
	  if(aString1 instanceof java.lang.String) {
		System.out.println("aString is a String.");
	  }
	  /*
	   * The 'instanceof' is a Java keyword to 
	   * determine if an object is an instance of
	   * a certain class.
	   * 
	   * NOTE: The fully qualified name of a String
	   * class is java.lang.String.
	   */
	}
  }
                           



Back    Next