A lecture snippet with a mentor as part of the Codegym University course. Sign up for the full course.


"I'd like to tell you a bit about comparing variables in Java."

"You already know the simplest comparison operators – less than (<) and greater than (>)."

"Yep."

"There are also operators like equal to (==) and not equal to (!=). As well as, less than or equal to (<=) and greater than or equal to (>=)."

"Now this is getting interesting."

"Note that there are no =< or => operators in Java!"

"The = sign is used for assignment operations. That's why two equal signs (==) are used to test equality. To check that variables aren't equal, use the != operator."

"I see."

"When comparing two variables in Java using the == operator, we are comparing the contents of the variables."

"Thus, for primitive variables, their values are compared."

"For reference variables, the references are compared. Suppose we have identical but distinct objects. Because references to them are different, a comparison will show that they are not equal, i.e. the comparison result will be false. A comparison of references will be true only if both references point to the same object."

"To compare objects' internal contents, we use the special equals method. This method (and all methods of the Object class) are added to your class by the compiler even if you don't declare them. Let me show you some examples:"

Code Explanation
1
int a = 5;
int b = 5;
System.out.println(a == b);
Compare primitive types.
true will be displayed on the screen.
2
Cat cat1 = new Cat("Oscar");
Cat cat2 = cat1;
System.out.println(cat1 == cat2);
Compare references.
true will be displayed on the screen.
Both variables store references to the same object.
3
String s = new String("Mom");
String s2 = s;
System.out.println(s == s2);
Compare references.
true will be displayed on the screen.
Both variables store references to the same object.
4
Cat cat1 = new Cat("Oscar");
Cat cat2 = new Cat("Oscar");
System.out.println(cat1 == cat2);
Compare references.
false will be displayed on the screen.
The two variables reference identical Cat objects, but not the same one.
5
String s = new String("Mom");
String s2 = new String("Mom");
System.out.println(s == s2);
Compare references.
false will be displayed on the screen.
The two variables reference identical String objects, but not the same one.
6
String s = new String("Mom");
String s2 = new String("Mom");
System.out.println(s.equals(s2));
Compare objects.
true will be displayed on the screen.
The two variables reference identical String objects

"Oh, I almost forgot! Here are some exercises for you:"

undefined
4
Task
New Java Syntax, level 4, lesson 6
Locked
Even numbers
Use a for loop to display even numbers from 1 to 100 inclusive. Display each value on a new line.
undefined
4
Task
New Java Syntax, level 4, lesson 6
Locked
Drawing a rectangle
Use the keyboard to enter two numbers m and n. Using a for loop, display an n x m rectangle made of eights. Here's an example: m=2, n=4 8888 8888
undefined
4
Task
New Java Syntax, level 4, lesson 6
Locked
Triangle of eights
Using a for loop, display a right triangle of eights, with a base of 10 and a height of 10. Example output: 8 88 888 8888 88888 888888 8888888 88888888 888888888 8888888888
undefined
4
Task
New Java Syntax, level 4, lesson 6
Locked
Drawing lines
Using a for loop to display: - a horizontal line of 10 eights - a vertical line of 10 eights (do not count any of the eights in the horizontal line as part of this vertical line).
undefined
4
Task
New Java Syntax, level 4, lesson 6
Locked
Chain letter
Enter a name from the keyboard and use a for loop to display the following 10 times: loves me. Example output for the name "Scarlett": Scarlett loves me. Scarlett loves me. Scarlett loves me. Scarlett loves me. Scarlett loves me. Scarlett loves me. Scarlett loves me. Scarlett loves me. Scarlett lo