Java Double Equals Method

JavaJavaBeginner
Practice Now

Introduction

In Java, the equals() method is used to compare the value of a Double object currently in use with the value of a parameter. This method is built in the Double class, and allows for easy comparison of Double values.


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL java(("`Java`")) -.-> java/ProgrammingTechniquesGroup(["`Programming Techniques`"]) java(("`Java`")) -.-> java/ObjectOrientedandAdvancedConceptsGroup(["`Object-Oriented and Advanced Concepts`"]) java(("`Java`")) -.-> java/BasicSyntaxGroup(["`Basic Syntax`"]) java(("`Java`")) -.-> java/DataStructuresGroup(["`Data Structures`"]) java(("`Java`")) -.-> java/StringManipulationGroup(["`String Manipulation`"]) java(("`Java`")) -.-> java/SystemandDataProcessingGroup(["`System and Data Processing`"]) java/ProgrammingTechniquesGroup -.-> java/scope("`Scope`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/classes_objects("`Classes/Objects`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/class_methods("`Class Methods`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/exceptions("`Exceptions`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/modifiers("`Modifiers`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/oop("`OOP`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/packages_api("`Packages / API`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/user_input("`User Input`") java/ObjectOrientedandAdvancedConceptsGroup -.-> java/wrapper_classes("`Wrapper Classes`") java/BasicSyntaxGroup -.-> java/identifier("`Identifier`") java/DataStructuresGroup -.-> java/arrays("`Arrays`") java/BasicSyntaxGroup -.-> java/data_types("`Data Types`") java/BasicSyntaxGroup -.-> java/if_else("`If...Else`") java/BasicSyntaxGroup -.-> java/operators("`Operators`") java/BasicSyntaxGroup -.-> java/output("`Output`") java/StringManipulationGroup -.-> java/strings("`Strings`") java/BasicSyntaxGroup -.-> java/variables("`Variables`") java/SystemandDataProcessingGroup -.-> java/object_methods("`Object Methods`") java/SystemandDataProcessingGroup -.-> java/system_methods("`System Methods`") subgraph Lab Skills java/scope -.-> lab-117605{{"`Java Double Equals Method`"}} java/classes_objects -.-> lab-117605{{"`Java Double Equals Method`"}} java/class_methods -.-> lab-117605{{"`Java Double Equals Method`"}} java/exceptions -.-> lab-117605{{"`Java Double Equals Method`"}} java/modifiers -.-> lab-117605{{"`Java Double Equals Method`"}} java/oop -.-> lab-117605{{"`Java Double Equals Method`"}} java/packages_api -.-> lab-117605{{"`Java Double Equals Method`"}} java/user_input -.-> lab-117605{{"`Java Double Equals Method`"}} java/wrapper_classes -.-> lab-117605{{"`Java Double Equals Method`"}} java/identifier -.-> lab-117605{{"`Java Double Equals Method`"}} java/arrays -.-> lab-117605{{"`Java Double Equals Method`"}} java/data_types -.-> lab-117605{{"`Java Double Equals Method`"}} java/if_else -.-> lab-117605{{"`Java Double Equals Method`"}} java/operators -.-> lab-117605{{"`Java Double Equals Method`"}} java/output -.-> lab-117605{{"`Java Double Equals Method`"}} java/strings -.-> lab-117605{{"`Java Double Equals Method`"}} java/variables -.-> lab-117605{{"`Java Double Equals Method`"}} java/object_methods -.-> lab-117605{{"`Java Double Equals Method`"}} java/system_methods -.-> lab-117605{{"`Java Double Equals Method`"}} end

Add the code snippet

Add the following code to the DoubleEquals.java file:

import java.util.Scanner;

public class DoubleEquals {
    public static void main(String[] args) {
        try {
          	Scanner sc = new Scanner(System.in);
            System.out.print("Enter first double value: ");
            Double n1 = sc.nextDouble();
            System.out.print("Enter second double value: ");
            Double n2 = sc.nextDouble();
            boolean equal = n1.equals(n2);
            if (equal) {
                System.out.println(n1 + " equals " + n2);
            } else {
                System.out.println(n1 + " does not equals " + n2);
            }
        } catch (Exception e) {
            System.out.println("Invalid input!!Please check");
        }
    }
}

This code snippet will ask the user to input two double values and then compare them using the Double Equals method. If the values are equal, it will print a message saying so; otherwise it will print a message saying that they are not equal.

Compile and run the code

In your terminal, navigate to the ~/project directory, and compile the code by running the following command:

javac DoubleEquals.java

If there are no errors, you can run your program using the following command:

java DoubleEquals

Your program will now run, and it will ask you to input two double values. After you have done so, it will print a message saying whether they are equal or not.

Summary

In this lab, we learned how to use the Java Double Equals method to compare double values in Java. We created a Java program that asks the user to input two double values and then uses the Double Equals method to compare them. If they are equal, the program prints a message saying so; otherwise, it prints a message saying that they are not equal.

Other Java Tutorials you may like