Introduction
In Java, the Float
class has an equals()
method, which helps in comparing the equality of two float
values. In this lab, we will learn about the implementation, syntax, and various examples of the method.
In Java, the Float
class has an equals()
method, which helps in comparing the equality of two float
values. In this lab, we will learn about the implementation, syntax, and various examples of the method.
We will first create a Java file in the ~/project
directory. Let's name it FloatEquals.java
.
cd ~/project
touch FloatEquals.java
equals()
methodWe will write the equals()
method inside the main
method of our FloatEquals.java
. Then, we will create two Float
objects and compare them using the equals()
method. Finally, we will print the result to the console.
Add the following code to the FloatEquals.java
file:
public class FloatEquals {
public static void main(String[] args) {
// Implementing the equals() method
Float num1 = 10.5f;
Float num2 = 10.5f;
boolean equal = num1.equals(num2);
System.out.println("Are num1 and num2 equal? " + equal);
}
}
Explanation: Here, we have created two Float
objects, num1
and num2
, and set them both equal to 10.5f
. Then, we have compared them using the equals()
method, and stored the result in a boolean
variable called equal
. Finally, we have printed the result to the console using the println()
method, by concatenating the result with a string.
equals()
methodNow that we have implemented the method for Float
objects, let's allow the user to provide two different Float
values to compare them using the equals()
method.
Replace the code you added to the main
method of FloatEquals.java
with the following code:
import java.util.Scanner;
public class FloatEquals {
public static void main(String[] args) {
// Providing different values to the equals() method
Scanner sc = new Scanner(System.in);
System.out.print("Enter value 1: ");
Float num1 = sc.nextFloat();
System.out.print("Enter value 2: ");
Float num2 = sc.nextFloat();
boolean equal = num1.equals(num2);
System.out.println("Are the values equal? " + equal);
}
}
Explanation: Here, we are taking two Float
values as input from the user, using the Scanner
class and its nextFloat()
method. We are then comparing them using the equals()
method, and storing the result in a boolean
variable called equal
. Finally, we are printing the result to the console using the println()
method, by concatenating a string with the result.
The Scanner
class can raise a java.util.InputMismatchException
if the user enters a non-float value. Let's handle this exception using a try-catch
block.
Replace the code you added to the main
method of FloatEquals.java
with the following code:
import java.util.Scanner;
public class FloatEquals {
public static void main(String[] args) {
// Handling errors with try-catch
try {
Scanner sc = new Scanner(System.in);
System.out.print("Enter value 1: ");
Float num1 = sc.nextFloat();
System.out.print("Enter value 2: ");
Float num2 = sc.nextFloat();
boolean equal = num1.equals(num2);
System.out.println("Are the values equal? " + equal);
} catch (java.util.InputMismatchException e) {
System.out.println("Invalid input. Please enter a float value.");
}
}
}
Explanation: Here, we have added a try-catch
block that surrounds the code that takes input from the user. If the user enters a value that is not a float, it raises an InputMismatchException
which is handled by the catch
block. It prints an error message to the console.
Finally, we need to compile and run the program to see the output. In the terminal, navigate to the ~/project
directory and execute the following command:
javac FloatEquals.java && java FloatEquals
Output:
Enter value 1: 12.5
Enter value 2: 12.5
Are the values equal? true
Let's test the try-catch
block we implemented in Step 4. Enter a non-float value when prompted to enter the num1
value.
Output:
Enter value 1: abc
Invalid input. Please enter a float value.
In this lab, we have learned about the Float
class equals()
method in Java. We learned how to implement it, the syntax, and different examples of using it. We also learned how to handle errors using a try-catch
block and tested our program for incorrect input.