CodeGym /Courses /New Java Syntax /Comparing and setting conditions

Comparing and setting conditions

New Java Syntax
Level 3 , Lesson 8
Available

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:"

3
Task
New Java Syntax, level 3, lesson 8
Locked
Minimum of two numbers
Use the keyboard to enter two integers, and display the minimum. If the two numbers are equal, display either of them.
3
Task
New Java Syntax, level 3, lesson 8
Locked
18+
Use the keyboard to enter the name and age. If the age is less than 18, display "Grow up a little more".
3
Task
New Java Syntax, level 3, lesson 8
Locked
Bouncer policy
Use the keyboard to enter the name and age. If the age is more than 20, display "18 is old enough".
3
Task
New Java Syntax, level 3, lesson 8
Locked
Quadrants
Use the keyboard to enter two integers representing the coordinates of a point not on the X and Y axes. Display the number of the quadrant that contains the given point. Hint: Point (a, b) belongs to a quadrant if the following conditions are true: for the first quadrant: a>0 and b>0; for the
Comments (89)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Phil Level 3, UK
26 December 2024
Task for Quadrants: "Use the keyboard to enter two integers representing the coordinates of a point not on the X and Y axes." - This must be a mistake. I think it should read '...of a point on the X and Y axes'. If it's not on the X and Y axes, then why are we programming this?
Anonymous #11549069 Level 5, Katowice, Poland Expert
26 August 2024
🧡💖
rags 3 Level 5, Wichita, United States Expert
28 April 2024
seems wrong to just use if no else
Lunar Ghost Level 4, infinity , India
30 November 2023
I have only 9 dark matter left. I am not a premium user.💀
rags 3 Level 5, Wichita, United States Expert
1 July 2023
so i was wondering why they use else if when simple if operator works i dont understand
Anonymous #11263161 Level 3, Dearborn, United States
24 June 2023
For these exercises, I just added java.util.Scanner and used a Scanner object instead lol. I don't like using BufferedReader that much lol. Espcially when you have to use the parse methods.
abhishe_kira Level 18, India Expert
26 June 2023
You know what doing so you are resisting to learn something new. To keep moving forward you have to update your knowledge.
Phil Level 3, UK
26 December 2024
My take on this is - we will eventually learn what we need to know. I am also using Scanner class for the purpose of learning. Unless there is a strong need in the future for me to use BufferedReader, I do not feel it's necessary for me to invest the time now to deep dive into it.
Anonymous #11263139 Level 7, Mexico
16 February 2023
Don't forget go to the article and read: equals https://codegym.cc/groups/posts/equals-and-string-comparsions
WarDust00 Level 22, Iraq
22 September 2022
bufferedreader is just old and unnecessary Scanner is WAY better.
Robbie_Robot Level 4, California, United States
8 October 2022
Actually BufferedReader is faster and has a larger buffer, 8kb compareTo Scanner's 1kb. Once you start reading in large files, Scanner will run out of steam, hence the reason for using BufferedReader right out of the gate. But yes, your are correct, for simple quick inputs such as these basic tasks, Scanner does work just fine.
Imre Vigh Level 6, Skipton, United Kingdom
11 June 2022
Great tasks so far. I like it.
Ali_Alshehri Level 6, Saudi Arabia
24 April 2022
i like it