Introduction
In this lab, you will learn how to compare two numbers in Java to determine if one is greater than the other. We will explore the use of comparison operators, specifically the "greater than" operator (>), which is fundamental for making conditional decisions in programming.
Through hands-on exercises, you will implement code to read user input, apply the greater than operator to compare different numeric types, and handle cases where the numbers are equal. This lab will provide practical experience in using conditional statements and comparison operators in Java.
Use Comparison Operator for Greater Than
In this step, we will learn how to use comparison operators in Java to compare two numbers. Specifically, we will focus on the "greater than" operator (>). Comparison operators are fundamental in programming as they allow us to make decisions based on the relationship between values.
Let's create a new Java program to demonstrate the greater than operator.
Open the
HelloJava.javafile in the WebIDE editor if it's not already open.Replace the entire contents of the file with the following code:
import java.util.Scanner; public class HelloJava { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the first number: "); int number1 = scanner.nextInt(); System.out.print("Enter the second number: "); int number2 = scanner.nextInt(); if (number1 > number2) { System.out.println("The first number is greater than the second number."); } scanner.close(); } }Let's look at the new parts of this code:
import java.util.Scanner;: We still need theScannerto get input from the user.System.out.print("Enter the first number: ");: Prompts the user to enter the first number.int number1 = scanner.nextInt();: Reads the first integer entered by the user and stores it in the variablenumber1.System.out.print("Enter the second number: ");: Prompts the user to enter the second number.int number2 = scanner.nextInt();: Reads the second integer entered by the user and stores it in the variablenumber2.if (number1 > number2): This is where we use the greater than operator (>). This line checks if the value ofnumber1is greater than the value ofnumber2. The code inside the curly braces{}will only execute if this condition is true.System.out.println("The first number is greater than the second number.");: This line will be printed ifnumber1is indeed greater thannumber2.
Save the file (Ctrl+S or Cmd+S).
Compile the modified program in the Terminal:
javac HelloJava.javaIf there are no compilation errors, you will see no output.
Run the compiled program:
java HelloJavaThe program will prompt you to enter two numbers. Enter a number for the first prompt, press Enter, then enter a second number, and press Enter again.
For example, if you enter
10for the first number and5for the second number, the output will be:Enter the first number: 10 Enter the second number: 5 The first number is greater than the second number.If you enter
5for the first number and10for the second number, there will be no output after entering the second number, because the conditionnumber1 > number2is false.
You have successfully used the greater than comparison operator to compare two numbers and execute code based on the result.
Test with Different Numeric Types
In the previous step, we compared two integer numbers. Java has different numeric types to handle various kinds of numbers, such as whole numbers (integers) and numbers with decimal points (floating-point numbers). In this step, we will explore how comparison operators work with different numeric types.
Java has several primitive numeric types, including:
int: for whole numbers (integers)double: for floating-point numbers (numbers with decimals)float: also for floating-point numbers, but generally less precise thandoublelong: for very large whole numbers
Comparison operators like > can be used to compare values of different numeric types. Java will often perform automatic type conversion (widening) to make the comparison possible. For example, when comparing an int and a double, the int will be converted to a double before the comparison.
Let's modify our program to compare an integer and a double.
Open the
HelloJava.javafile in the WebIDE editor.Replace the existing code with the following:
import java.util.Scanner; public class HelloJava { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter an integer: "); int intNumber = scanner.nextInt(); System.out.print("Enter a decimal number: "); double doubleNumber = scanner.nextDouble(); if (intNumber > doubleNumber) { System.out.println("The integer is greater than the decimal number."); } if (doubleNumber > intNumber) { System.out.println("The decimal number is greater than the integer."); } scanner.close(); } }In this updated code:
- We now read an
intinto theintNumbervariable usingscanner.nextInt(). - We read a
doubleinto thedoubleNumbervariable usingscanner.nextDouble(). - We use the
>operator to compare theintNumberanddoubleNumber. Notice that we added a secondifstatement to check if the decimal number is greater than the integer.
- We now read an
Save the file (Ctrl+S or Cmd+S).
Compile the program in the Terminal:
javac HelloJava.javaRun the program:
java HelloJavaEnter an integer and a decimal number when prompted.
For example, if you enter
10for the integer and9.5for the decimal number, the output will be:Enter an integer: 10 Enter a decimal number: 9.5 The integer is greater than the decimal number.If you enter
5for the integer and5.1for the decimal number, the output will be:Enter an integer: 5 Enter a decimal number: 5.1 The decimal number is greater than the integer.
This demonstrates that Java can compare different numeric types using the greater than operator.
Handle Equal Numbers
In the previous steps, we used the greater than operator (>) to check if one number is larger than another. However, what happens if the two numbers are equal? Our current program doesn't explicitly handle this case.
In this step, we will learn how to check for equality using the equality operator (==) and how to use if-else if-else statements to handle multiple possibilities, including when numbers are equal.
The equality operator (==) in Java is used to check if two values are equal. It returns true if the values are the same and false otherwise.
Let's modify our program to compare two numbers and print a message indicating whether the first number is greater than, less than, or equal to the second number.
Open the
HelloJava.javafile in the WebIDE editor.Replace the existing code with the following:
import java.util.Scanner; public class HelloJava { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.print("Enter the first number: "); double number1 = scanner.nextDouble(); System.out.print("Enter the second number: "); double number2 = scanner.nextDouble(); if (number1 > number2) { System.out.println("The first number is greater than the second number."); } else if (number1 < number2) { System.out.println("The first number is less than the second number."); } else { System.out.println("The two numbers are equal."); } scanner.close(); } }Let's examine the changes:
- We are now reading two
doublevalues to allow for more flexibility in testing. if (number1 > number2): This is the same check as before. Ifnumber1is greater thannumber2, the first message is printed.else if (number1 < number2): This is a new part. Theelse ifblock is executed only if the previousifcondition (number1 > number2) is false. Here, we use the less than operator (<) to check ifnumber1is less thannumber2.else: This block is executed if none of the precedingiforelse ifconditions are true. In this case, ifnumber1is not greater thannumber2and not less thannumber2, it must be equal tonumber2.
- We are now reading two
Save the file (Ctrl+S or Cmd+S).
Compile the program in the Terminal:
javac HelloJava.javaRun the program:
java HelloJavaEnter two numbers, including cases where they are equal.
If you enter
7.5for the first number and7.5for the second number, the output will be:Enter the first number: 7.5 Enter the second number: 7.5 The two numbers are equal.If you enter
10and5, you'll see "The first number is greater than the second number." If you enter5and10, you'll see "The first number is less than the second number."
You have now successfully used if-else if-else statements and the equality operator (==) to handle different comparison outcomes, including equality.
Summary
In this lab, we learned how to check if a number is greater than another number in Java. We started by using the comparison operator > within an if statement to compare two integer values entered by the user. This demonstrated the fundamental concept of using comparison operators to make decisions in our code.
We then explored how to handle different numeric types, ensuring our comparison logic works correctly for various data types like double and float. Finally, we addressed the scenario where the two numbers are equal, expanding our conditional logic to include the "greater than or equal to" operator (>=) or separate checks for equality.



