Relational Operators

Code Bar
> <
>= <=
== !=
str1.equals(str2) !str1.equals(str2)

So far, we have learned the basics about primitive variables and assigning values to them. However, in the case of booleans, there doesn’t seem to be much of a point in simply assigning it a true or false value. Most of the time, you would want a boolean to tell you important information on whether or not certain conditions are true. For example, you could assign a boolean to be true if a number is less than 0 and false otherwise, and you would therefore have useful information stored in that boolean.

So far, we have learned how to assign values to different primitive variables and how to manipulate those values. Besides just knowing how to set values, we need to know how to compare them. Just like how in math class, you would use symbols such as >, <, ≥, ≤, =, and ≠, in programming, the same relational operators are used. Now, in programming, there are a few differences: the ≥ and ≤ symbols are replaced by >= and <= and the = and ≠ symbols are replaced by == and !=. However, the overall concept is exactly the same.

Note: You will see the ! symbol a lot when programming and might wonder why it is used to represent “does not equal.” Essentially, ! means “not.” For example, !true would mean not true, or false, and !false would mean not false, or true. This will be covered in more detail in a later lesson.

Try running the following code:

	int num = 5; 

	boolean isPositive = (num > 0); 

	System.out.println(isPositive); // Prints true

Note that the parentheses around num > 0 are purely optional in this case and are only put there to make the statement easier to read. The computer is able to recognize that the statement is not (isPositive = num > 0) and we will explain why further on.

Since the statement (num > 0) will obviously evaluate to true, the boolean isPositive will also be set to true. However, if you changed num to be -5, isPositive would of course be set to false. You can also try changing this statement up with the other relational operators: >=, <=, <, ==, and !=.

One thing that you should keep in mind though is if you have two Strings, str1 and str2, == and != don’t work properly. What you would do instead is str1.equals(str2) and !str1.equals(str2). This will return whether or not str1 and str2 are equal. For any of the other data types we’ve learned about, == and != work fine. For example:

	String str1 = new String("hello");

	String str2 = new String("hi");

	boolean b1 = str1.equals(str2); // Correct: returns true

	boolean b2 = (str1 == str2); // Incorrect: returns false

Now, there is an important distinction between the = sign and == sign. The = sign is used to assign variables a value, as we have learned. The == sign, on the other hand, is used to compare different values. Thus, = is an assignment operator and == is a relational operator.

Take a look at the following two snippets of code.

	num = 5 

	num == 5

In the first line, num is being assigned the value of 5. In the second line, num is being compared to the value 5. Since num does in fact equal 5, the second statement would have a value of true.

These relational operators are very important and are heavily used in programming. We will revisit them in one of the next lessons where we go over the if/else statement, a selection construct lets you execute certain statements based on whether a condition is true or not.

Lesson Quiz

1. What is the output to the console?

	int num = 5;

	boolean b = num + 5 <= num * 2;

a. true
b. false

2. What is the output to the console?

	double dec = 14.4;

	boolean b = !(dec / 2 != 12 * 0.6);

a. true
b. false

Written by Alan Bi

Notice any mistakes? Please email us at [email protected] so that we can fix any inaccuracies.