undefined

Comparing and setting conditions

Java Syntax
Level 4 , Lesson 6
Available

"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
Cat cat1 = new Cat("Oscar");
Cat cat2 = new Cat("Oscar");
System.out.println(cat1.equals(cat2));
Compare objects.
true will be displayed on the screen.
The two variables reference identical Cat objects
7
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
Java Syntax, level 4, lesson 6
Locked
Minimum of two numbers
All search and sort algorithms are based on comparisons. You'll be able to handle these very soon, if you so desire. In the meantime, we suggest starting with something small: write a program to find the minimum of two numbers. Find it and then display it. And if the numbers are the same, display either of them.
undefined
4
Task
Java Syntax, level 4, lesson 6
Locked
Maximum of four numbers
Finding the maximum is an n-ary operation (an operation on n numbers) that returns the largest of several numbers. Never mind. We have no need for such definitions at the secret CodeGym center. We're here to learn how to write code. In this task, you need to use the keyboard to enter four numbers. Then determine the largest of them and display it on the screen.
undefined
8
Task
Java Syntax, level 4, lesson 6
Locked
Sorting three numbers
Planet Linear Chaos is populated by isomorphs. They are believed to have invented sorting algorithms. Everything in their heads is extremely well-ordered. They only issue planetary visas to people who know at least 7 sorting algorithms. Let's take our first step toward Linear Chaos: read three numbers from the keyboard, put them in descending order, and then display them on the screen.
undefined
4
Task
Java Syntax, level 4, lesson 6
Locked
Jen or Jen?
Jen, Company X's admin, learned how to pilot a space ship and flew away to another planet. People in Company X are good and sincere. It's just that they're scatterbrained and they mix up names. So they decided that the new administrator would also be called Jen. Let's help Company X find their Jen: write a program that checks the identity of two entered names.
undefined
2
Task
Java Syntax, level 4, lesson 6
Locked
18+
Would any child close a website after being informed that it is not intended for anybody under 18? Despite being entirely ineffective, this age "verification" process is still used. Let's write a program that asks the user's age. If the answer is <18, the program displays "Grow up a little more".
undefined
2
Task
Java Syntax, level 4, lesson 6
Locked
Bouncer policy
The Chastity nightclub has commissioned Company X to make an automatic bouncer. A junior developer (you) was instructed to write a visitor identification method that includes an age check. Would-be guests need to use the keyboard to enter their name and age. If the age is 20 or more, the auto-bouncer should assure the guest that everything is OK—18 is old enough.
undefined
4
Task
Java Syntax, level 4, lesson 6
Locked
Three numbers
Keyboard input, numeric comparisons, and screen output—4th-level CodeGym students hone these skills until they are automatic. Let's write a program where the user enters three numbers from the keyboard. Then we'll perform a comparison. If we find a number that differs from the other two, we'll display its ordinal number.
undefined
2
Task
Java Syntax, level 4, lesson 6
Locked
Target locked!
A military robot has been equipped with a sight that helps it shoot down enemy tin cans. An informant passes the robot the enemy's surface coordinates (latitude and longitude). Let's write a program that helps the robot calculate the quadrant where the enemy tin can is located.
Comments (70)
TO VIEW ALL COMMENTS OR TO MAKE A COMMENT,
GO TO FULL VERSION
Bak Level 5, Kansas City, United States
4 January 2021
I'm not familiar with this code: int min = a < b ? a : b; I see that it is supposed to find the minimum value between a and b, but how would this code change if there were more than two values? I don't think there was an explanation of it anywhere in the lessons, or did I miss it? I tried to google it, but could only find math.min function which has a different syntax. I'd appreciate an explanation of the min function (not math.min). Thanks.
Albert Level 6, Hong Kong, Hong Kong
17 November 2020
// when one is typing int a = 5; int 5 = a; // are the 2 cases the same? or they bear different meaning?
mastere Level 9, Rochester, United States
3 November 2020
I used if else statements with the greater than or equal to comparison operator >= and the and logical operator && to complete the sorting numbers task. There are six if else statements to be made if you do it this way.
Chandan Thapa Level 22, Dubai, United Arab Emirates
3 October 2020
hardest so far was sorting numbers.. tried with max and min and if/else instead of loops.. Enjoyed it thoroughly.. took 6 attempts :)
Mihai Bone Level 8, Bucharest, Romania
1 October 2020
Sorting three numbers - Don't waste time...... learn loops and then come back for it. (you will lose hours)
ViperWolfAlpha Level 5, Los Angeles, United States
19 September 2020
Proud of myself! Completed it all in one sitting. Had a tough time with sorting numbers at first but decided to erase it all and start from scratch and came up with a better solution. Sometimes you just need to start from zero to get a clear picture.
Mariam Level 9, Columbus, United States
6 September 2020
"The program should display the ORDINAL number of the number that is different from the others"
Agent Smith Level 38
7 August 2020
You might want to do "Sorting three numbers" after the lesson about loops.
MoJo Level 22, Cairns, Australia
31 July 2020
Do NOT check if all numbers are the same (a ==b && c != b) , i did it and it got only accepted after i took that out.
Brad Reed Level 19, Hartford, United States
21 June 2020
I’ve said it before, I’ll say it again(cuz I just did it too); ALWAYA proofread, especially if you’re doing this on a tablet or phone(like me) because it’s so easy to mistype things on a haptic keyboard.