Java Program to compare Two Java float Arrays


To compare Java float arrays, use the Arrays.equals() method. The return value is a boolean. Let’s say we have the following float arrays −

float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f };
float[] floatVal2 = new float[] { 8.3f, 8.8f, 9.2f };
float[] floatVal3 = new float[] { 6.2f, 6.9f, 9.9f };
float[] floatVal4 = new float[] { 3.2f, 5.5f, 5.3f };

To compare them, the Arrays.equals() method is to be used −

Arrays.equals(floatVal1, floatVal2);
Arrays.equals(floatVal2, floatVal3);
Arrays.equals(floatVal3, floatVal4);

The following is the complete example wherein we compare all the arrays −

Example

 Live Demo

import java.util.Arrays;
public class Demo {
   public static void main(String args[]) {
      float[] floatVal1 = new float[] { 3.2f, 5.5f, 5.3f };
      float[] floatVal2 = new float[] { 8.3f, 8.8f, 9.2f };
      float[] floatVal3 = new float[] { 6.2f, 6.9f, 9.9f };
      float[] floatVal4 = new float[] { 3.2f, 5.5f, 5.3f };
      System.out.println(Arrays.equals(floatVal1, floatVal2));
      System.out.println(Arrays.equals(floatVal2, floatVal3));
      System.out.println(Arrays.equals(floatVal3, floatVal4));
      System.out.println(Arrays.equals(floatVal1, floatVal3));
      System.out.println(Arrays.equals(floatVal1, floatVal4));
   }
}

Output

false
false
false
false
true

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 26-Jun-2020

104 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements