dcsimg
LinkedIn
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Jun 16, 2021

Using Java Comparison Operators

Java has six comparison operators which let developers compare one value to another. When a programmer uses a comparison operator, they can expect a true or false returned after the evaluation. From there, you can guide your program and use the resulting output to make logical decisions within your application.

The six Java comparison operators include:

  • == The equal to operator evaluates whether one value is equal to another value. If both values are equal, a True is returned. If both values are not equal, a False is returned.
  • != The Not equal to operator evaluates as True if a value is not equal to the value it is being compared to and False if it is equal to the value you are comparing it to.
  • < The Less than operator is used to check if a value is less than another. If the value is less, True gets returned. Otherwise, False gets returned. Please be aware that if the values are equal, False is returned.
  • <= The Less than or equal to operator checks if a value is either less than another value or equal to it. If it is, True is returned. Otherwise, False is returned.
  • > The Greater than operator checks to whether a value is greater than another. If it is, it returns True. If it is not, it returns False Please note, if the two values are equal, it returns False.
  • >= The Greater than or equal to operator checks to see if one value is greater than or equal to another. If it is, it returns True. If not, it returns False.

Using the == Operator in Java

Below is an example of how to use the == or equal to operator in Java to compare two values and see if they are equal or not:

public class Main {
	public static void main(String[] args) {
		int value1 = 100;
		int value2 = 100;
		System.out.println(value1 == value2);
	}
}

The outcome of running this code would result in True. We get this results because the values of our variables value1 and value2 contain the same value and, therefore, when we compare them with the equal to operator, we get a True response.

The != Operator in Java

Here is some sample Java code showing how to use the != operator in Java:

// Using the != operator to test if a value is NOT equal to another
public class Main {
	public static void main(String[] args) {
		int value1 = 100;
		int value2 = 200;
		System.out.println(value1 != value2);
	}
}

The output of this code would be True, because the variable value1 is not equal to value2.

Using the > Operator in Java

To see if one value is greater than another in Java, we use the > or greater than operator. Here is how that looks in code:

// Using the > operator to test if a value is > than another
public class Main {
	public static void main(String[] args) {
		int value1 = 100;
		int value2 = 50;
		System.out.println(value1 > value2);
	}
}

Again, the output of this code would be True, as value1 is greater than value2.

Using the < Operator in Java

The < or less than operator works the opposite of the > operator in Java. Here it is in code:

public class Main {
	public static void main(String[] args) {
		int value1 = 100;
		int value2 = 200;
		System.out.println(value1 < value2);
	}
}

Once more, this code evaluates to True, because value1 is less than value2 in the above code example.

Using the >= Operator in Java

The >= operator in Java tests whether a value is greater than or equal to another value. If it is greater than, it eveluates as True. If it evaluates as equal, it returns True. If it is neither of these, then it returns False. Here is a acode example showcasing this:

public class Main {
	public static void main(String[] args) {
		int value1 = 200;
		int value2 = 200;
		System.out.println(value1 >= value2);
	}
}

In this example, both variables have the same value. Since we are looking for either an equal value or a greater value, it evaluates as True.

Using the <= Operator in Java

Finally, the <= or less than or equal to operator does what you would expect - the opposite of >=. Here is the code sample:

public class Main {
	public static void main(String[] args) {
		int value1 = 200;
		int value2 = 200;
		System.out.println(value1 <= value2);
	}
}

Once more, the values of value1 and value2 in this instance, are equal, so our code evaluates to True if you run the above Java code snippet.

James Payne
 
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date