Download Java float Array in Formats
QR Code for this Page
Java float Array
Complete Tutorial on Java primitive float data type array.

Java float Array - float Array in Java

Java float array is used to store float data type values only. The default value of the elements in a Java float array is 0.

With the following Java float array examples you can learn

  • how to declare Java float array
  • how to assign values to Java float array
  • how to get values from Java float array
  • What is a float Array in Java ?
  • How do you declare a float in Java ?
  • How to Fill all the elements of Java float Array ?

What is a float Array in Java ?

A float is a Java data type which can store floating point numbers (i.e., numbers that can have a fractional part). Only 4 bytes are used to store float numbers giving a value range of -3.4028235E+38 to 3.4028235E+38. The double data type is a double-precision 64-bit IEEE 754 floating point. Float and Double are both floating point data types. The only difference is that double is more precise than float and therefore takes more memory as well. In java double is the Default floating point data type.

How do you declare a float in Java ?

float Type

  • In Java any value declared with decimal point is by default of type double.
  • Suppose we have to assign float value then we must use f or F literal to specify that current value is float.
  • Specify E or e for values which contain exponent.

Initializing a float Array in Java

Arrays are declared with [] (square brackets). If you put [] (square brackets) after any variable of any type only that variable is of type array remaining variables in that declaration are not array variables those are normal variables of that type.

If you put [] (square brackets) after any data type all the variables in that declaration are array variables. All the elements in the array are accessed with index. The array element index is starting from 0 to n-1 number i.e. if the array has 5 elements then starting index is 0 and ending index is 4.

// declares an array of floats
float[] floatArray;

What is the Length of an Array in Java ?

In Java all the arrays are indexed and declared by int only. That is the size of an array must be specified by an int value and not long or short. All the arrays index beginning from 0 to ends at 2147483646. You can store elements upto 2147483647. If you try to store long (big) elements in array, you will get performance problems. If you overcome performance problems you should go to java collections framework or simply use Vector.

Syntax :
1
2
< data type > < variable >[];
< data type >[] < variable >;

Java float Array Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
/*  Java float Array Example
    Save with file name FloatArray.java */
  
public class FloatArray
{
  public static void main(String args[])
  {
    //JAVA FLOAT ARRAY DECLARATION
    float f[];
    //MEMORY ALLOCATION FOR JAVA FLOAT ARRAY
    f = new float[4];
    //ASSIGNING ELEMENTS TO JAVA FLOAT ARRAY
    f[0] = 10.10f;
    f[1] = 30.3f;
    f[2] = 40.60f;
    f[3] = 77.50f;
    //JAVA FLOAT ARRAY OUTPUT
    System.out.println("Java float Array Example");
    for(int i=0;i<f.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + f[i]);
    }
  }
}

Following Java float array example you can learn how to assign values to Java float array at the time of declaration.

How to assign values to Java float array at the time of declaration

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/*  How to assign values to Java float array at the time of declaration Example
    Save with file name FloatArray2.java    */
  
public class FloatArray2
{
  public static void main(String args[])
  {
    //JAVA FLOAT ARRAY DECLARATION
    float f[] = {10.10f,30.3f,40.60f,77.50f};
    //JAVA FLOAT ARRAY OUTPUT
    System.out.println("Java float Array Example");
    for(int i=0;i<f.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + f[i]);
    }
  }
}

Following Java float array example you can learn how to declare Java float array with other Java float array variables.

How to declare Java float array with other Java float array variables

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/*  How to declare Java float array with other Java float array variables Example
    Save with file name FloatArray3.java    */
  
public class FloatArray3
{
  public static void main(String args[])
  {
    //JAVA FLOAT ARRAY DECLARATION
    //f IS AN ARRAY f2 IS NOT AN ARRAY
    float f[], f2;
    //MEMORY ALLOCATION FOR JAVA FLOAT ARRAY
    f = new float[4];
    //ASSIGNING ELEMENTS TO JAVA FLOAT ARRAY
    f[0] = 10.10f;
    f[1] = 30.3f;
    f[2] = 40.60f;
    f[3] = 77.50f;
    f2 = 555.55f;
    //JAVA FLOAT ARRAY OUTPUT
    System.out.println("Java float Array Example");
    System.out.println("f2 value is : "+f2);
    for(int i=0;i<f.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + f[i]);
    }
  }
}

Following Java float array example you can learn how to assign Java float array to other Java float array.

How to assign Java float array to other Java float array

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
/*  How to assign Java float array to other Java float array Example
    Save with file name FloatArray4.java    */
  
public class FloatArray4
{
  public static void main(String args[])
  {
    //JAVA FLOAT ARRAY DECLARATION
     //f AND f2 ARE ARRAY VARIABLES
    float[] f, f2;
    //MEMORY ALLOCATION FOR JAVA FLOAT ARRAY
    f = new float[4];
    //ASSIGNING ELEMENTS TO JAVA FLOAT ARRAY
    f[0] = 10.10f;
    f[1] = 30.3f;
    f[2] = 40.60f;
    f[3] = 77.50f;
    //ASSIGNING f ARRAY TO f2 ARRAY VARIABLE
    f2 = f;
    //JAVA FLOAT ARRAY OUTPUT
    System.out.println("Java float Array Example");
    System.out.println("f array values");
    for(int i=0;i<f.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + f[i]);
    }
    System.out.println("f2 array values");
    for(int i=0;i<f2.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + f2[i]);
    }
  }
}

java.util.Arrays.fill(float[], float) Method

The java.util.Arrays.fill(float[] a, float val) method assigns the specified float value to each element of the specified array of floats. The default value of the elements in a Java float array is 0. The following example shows how to fill all the elements of float array in Java fill with 1.

How to Fill all the elements of Java float Array Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
/*  How to Fill all the elements of Java float Array Example
    Save with file name FillFloatArrayExample.java    */
   
import java.util.Arrays;
   
public class FillFloatArrayExample
{
  public static void main(String args[])
  {
    //float ARRAYS DECLARATION
    float[] float_array = new float[10];
    //Assign all the elements of float array to 1
    Arrays.fill(float_array, 1f);
    //float ARRAY OUTPUT
    for(int i=0;i<float_array.length;i++)
    {
      System.out.println("float array Element at : "+ i + " " + float_array[i]);
    }
  }
}
Download Java float Array in Formats