To compare two values of the Eight Primitive types, we can use the so-called relational operators.
> (greater)
< (less)
>= (greater equal to)
<= (less than or equal to)
== (equal)
!= (not equal)
These operators are called relational operators because they compare two values (one on the left side and another to the right side) to generate a boolean value. For example, let us interact with Java to test the relational operators with numeric values:
--> 10 > 5
true
--> 10 >= 5
true
--> 10 < 5
false
--> 10 <= 5
false
--> 10 == 5
false
--> 10 != 5
true
Let us now developed a code named RelationalOperators that shows whether the operations we are evaluating is true or false by declaring some variables x and y (lines 3 and 4):
1 public class RelationalOperators{
2 public static void main(String[] args) {
3 int x = 4;
4 int y = 5;
5 System.out.print("Is 4 < 5? ");
6 System.out.println(x < y);
7 System.out.print("Is 4 == 5? ");
8 System.out.println(x == y);
9 }
10 }
Computer Terminal:
Is 4 < 5: true
Is 4 == 5: false
Computer Terminal and the following lines from the example above are the output that our code produces once we have saved it, compiled it, and run it. For further practice, you can take the next cases and check their outputs.
> int x = 10
> int y = 10
> x > y
false
> x >= y
true
> x <= y
true
> x != y
false
> x == y
true
To compare Strings we will need to use the functionality of .equals() or .compareTo().
Let us interact with Java to demonstrate the .equals() and the .compareTo() methods.
Let us first declare three Strings
> String s1 = "hello"
> String s2 = "hi"
> String s3 = "hello"
When the == relational operator is used with s1 and s2, you obtain what you expect
> s1 == s2
false
However, when the operator is used with s1 and s3, a surprise appears when the following statement is executed:
> s1 == s3
False
The reason is that the == is useful only for the eight primitive types because Java also uses the == to check "location" where the String is stored in memory (but we will talk about this later on). If we are interested in comparing the content of the strings, we need to use the .equals() method as follows:
> s1.equals(s3)
true
> s1.equals(s2)
False
For equality, you can use the .equals().
> s1 > s2
Static Error: Illegal comparison: cannot compare a String to a String
Remember that the relational operators can only be applied to the eight primitive types, so here the >, <, >=, <= operators cannot be used. Instead, if you would like to check the order of the strings, we use the .compareTo() method
> s1.compareTo(s2)
-1
The .compareTo() will return a number and a sign (i.e., positive, or negative).
If the value is negative, that means that the String to the left (i.e., s1) is "smaller" than the String to the right (i.e., s2) lexicographically speaking.
If the value is positive, that means that the string to the left (i.e., s2) is “larger” than the string to the right (i.e., s1) lexicographically speaking.
For example, let us make a couple of comparisons and evaluations:
> s2.compareTo(s1)
1
> s1.compareTo(s2)
-4
> s2.compareTo(s1)
4
> String s3 = "he"
> s1.compareTo(s3)
3
> s3.compareTo(s1)
-3
> String s4 = "Hello"
> s1
"hello"
> s4
"Hello"
> s1.compareTo(s4)
32
> String s5 = "hello"
> s1
"hello"
> s5
"hello"
> s1.compareTo(s5)
0
> s1.compareTo(s2) > 0
false
>
AP CS A
[Comparing String Objects]
CON-1.H Compare object references using Boolean expressions in program code.
CS1
[Comparing String Objects]
130.421.c.6.k Compare objects using reference values and a comparison routine;
Design, write, test, and debug a program that effectively uses the different structured data types provided in the language like strings, arrays/lists, dictionaries, sets
Write a program that uses some language-provided libraries and frameworks (where applicable).
Read a given program, and explain what it does.
Trace the flow of control during the execution of a program (both correct and incorrect).
Use appropriate terminology to identity elements of a program (e.g., identifier, operator, operand)
Strings and string processing
Write programs that work with text by using string processing capabilities provided by the language.
Explain the importance of algorithms in the problem-solving process.