Example of relational operators in java.

Example of relational operators in java.


Posted in : Core Java Posted on : October 9, 2010 at 5:43 PM Comments : [ 0 ]

In this section, you will see the use of relational operatoer in java.

Example of relational operators in java.

In this example, you will see the use of relational operator in java. We are going to use in java program equally and relational operators determine if one operand is equal to, not equal to , less then, and greater then operand.

In this java program, there is a java class name RelationalOperater. It has six method  and two integer variable for arithmetic calculation.

equalRelOperator() -- Implements  = = (Equal)operator.

notEqualRelOperatr() -- Implements  != (Not Equal) operator.

greaterThanRelOprator() --  Implements  > (Greater Than) operator.

greaterOrEqualRelOprator() -- Implements  >=(Greater Than Or Equal To) operator. 

lessThanRelOperator() --  Implements  < (Less Than) operator. 

lessOrEqualRelOperator() --  Implements  <=(Less Than Or Equal To) operator. 

Code:

RelationalOperater.java
package com.devmanuals;

public class RelationalOperater {
	float flNum = 10.45f;
	float flNum1 = 54.85f;
	int a = 10;
	int b = 10;
	public void equalRelOperator() {
		if (flNum == flNum1) {
			System.out.println("Value of float Variable is equal.");
		} else {
			System.out.println("Value of float Variable is not equal.");
		}
	}

	public void notEqualRelOperatr() {
		if (flNum != flNum1) {
			System.out.println("Value of float Variable is not equal.");
		} else {
			System.out.println("Value of float Variable is equal.");
		}
	}
	public void greaterThanRelOprator() {
		if (flNum1 > flNum) {
			System.out.println((flNum1) + " is greater than " + (flNum));
		} else {
			System.out.println((flNum1) + " is not greater than " + (flNum));
		}
	}
	public void greaterOrEqualRelOprator() {
		if (a >= b) {
			System.out.println((a) + " is either greater than or equal " + (b));
		} else {
			System.out.println((a) + " is neither greater than nor equal "
					+ (b));
		}
	}
	public void lessThanRelOperator() {
		if (a < b) {
			System.out.println(a + " is less than " + b);
		} else {
			System.out.println(a + " is not less than " + b);
		}
	}
	public void lessOrEqualRelOperator() {
		if (a <= b) {
			System.out.println(a + " is less than or equal " + b);
		} else {
			System.out.println(a + " is not less than " + b);
		}
	}
	public static void main(String[] args) {
		RelationalOperater obRelationalOperater = new RelationalOperater();
		obRelationalOperater.equalRelOperator();
		obRelationalOperater.notEqualRelOperatr();
		obRelationalOperater.greaterThanRelOprator();
		obRelationalOperater.greaterOrEqualRelOprator();
		obRelationalOperater.lessThanRelOperator();
		obRelationalOperater.lessOrEqualRelOperator();
	}
}
Output:
Value of float Variable is not equal.
Value of float Variable is not equal.
54.85 is greater than 10.45
10 is either greater than or equal 10
10 is not less than 10
10 is less than or equal 10

Download this code.

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics