Compare two float arrays in a single line in Java

Java 8Object Oriented ProgrammingProgramming

Two float arrays can be compared in Java using the java.util.Arrays.equals() method. This method returns true if the arrays are equal and false otherwise. The two arrays are equal if they contain the same number of elements in the same order.

A program that compares two float arrays using the Arrays.equals() method is given as follows −

Example

 Live Demo

import java.util.Arrays;
public class Demo {
   public static void main(String[] argv) throws Exception {
      boolean flag = Arrays.equals(new float[] { 1.5F, 8.3F, 7.6F }, new float[] { 1.5F, 8.3F, 7.6F });
      System.out.println("The two float arrays are equal? " + flag);
   }
}

Output

The two float arrays are equal? true

Now let us understand the above program.

The Arrays.equals() method is used to compare two float arrays. If they are equal then true is stored in flag and if they are not equal then false is stored in flag. The value of flag is displayed. A code snippet which demonstrates this is as follows −

boolean flag = Arrays.equals(new float[] { 1.5F, 8.3F, 7.6F }, new float[] { 1.5F, 8.3F, 7.6F });
System.out.println("The two float arrays are equal? " + flag);
raja
Published on 09-Jan-2019 10:28:59
Advertisements