📖 Scenario: You are creating a simple program to compare ages of two friends to see who is older or if they are the same age.
🎯 Goal: Build a Java program that uses relational operators to compare two age values and prints the comparison results.
📋 What You'll Learn
Create two integer variables named ageAlice and ageBob with exact values 25 and 30 respectively.
Create a boolean variable named isAliceOlder that stores the result of comparing if Alice is older than Bob.
Create a boolean variable named isBobOlder that stores the result of comparing if Bob is older than Alice.
Create a boolean variable named areSameAge that stores the result of checking if Alice and Bob have the same age.
Print the values of isAliceOlder, isBobOlder, and areSameAge each on a new line.
💡 Why This Matters
🌍 Real World
Comparing values is common in real life, like checking if someone is old enough to vote or drive.
💼 Career
Understanding relational operators is essential for decision-making in programming, used in conditions and loops.
Progress0 / 4 steps
1
Create age variables
Create two integer variables called ageAlice and ageBob with values 25 and 30 respectively.
Java
Hint
Use int to declare whole number variables and assign the exact values.
2
Create relational boolean variables
Create boolean variables isAliceOlder, isBobOlder, and areSameAge that store the results of ageAlice > ageBob, ageBob > ageAlice, and ageAlice == ageBob respectively.
Java
Hint
Use relational operators > and == to compare the ages and store the results in boolean variables.
3
Print the comparison results
Use System.out.println to print the values of isAliceOlder, isBobOlder, and areSameAge each on a new line.
Java
Hint
Use System.out.println to show each boolean result on its own line.
4
Run and check output
Run the program and confirm the output shows false, true, and false each on a new line.
Java
Hint
The output should be three lines: false, true, and false.
Practice
(1/5)
1. Which of the following relational operators in Java checks if two values are not equal?
easy
A. ==
B. >=
C. !=
D. <
Solution
Step 1: Understand the meaning of relational operators
Relational operators compare two values and return true or false based on the comparison.
Step 2: Identify the operator for 'not equal'
The operator '!=' means 'not equal to' in Java.
Final Answer:
!= -> Option C
Quick Check:
Not equal operator = != [OK]
Hint: Remember: '!=' means not equal in Java [OK]
Common Mistakes:
Confusing '!=' with '=='
Using '=' instead of '=='
Mixing up '<' and '<='
2. Which of the following is the correct syntax to check if variable a is greater than or equal to variable b in Java?
easy
A. a => b
B. a >= b
C. a =< b
D. a =>= b
Solution
Step 1: Recall the correct relational operator syntax
The operator for 'greater than or equal to' is written as '>=' in Java.
Step 2: Check each option for correct syntax
a >= b uses '>=', which is correct. Other options have invalid syntax.
Final Answer:
a >= b -> Option B
Quick Check:
Greater or equal operator = >= [OK]
Hint: Use '>=' for greater or equal, not '=>' or '=<' [OK]
Common Mistakes:
Writing '=>' instead of '>='
Using '=' instead of '=='
Confusing '<=' and '=>'
3. What will be the output of the following Java code?
int x = 5; int y = 10; System.out.println(x < y); System.out.println(x == y); System.out.println(x != y);
medium
A. false false true
B. false true false
C. true true false
D. true false true
Solution
Step 1: Evaluate each relational expression
x is 5 and y is 10. So, x < y is true, x == y is false, and x != y is true.