HudaTutorials.com

Java float Array - float Array in Java

Last updated on

float Array in Java

float Array

Java float Array

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 :

< data type > < variable >[];
< data type >[] < variable >;

Following example shows Java float Array . Save the following Java float Array Example program with file name JavaFloatArrayExample.java .

Java float Array Example

public class JavaFloatArrayExample
{
	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]);
		}
	}
}

Related : Java boolean Array

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

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

Following example shows How to assign values to Java float array at the time of declaration . Save the following assign values to Java float array at the time of declaration Example program with file name AssignValuesToFloatArrayDeclaration.java .

Assign values to Java float array at the time of declaration Example

public class AssignValuesToFloatArrayDeclaration
{
	public static void main(String args[])
	{
		float f[] = {10.10f,30.3f,40.60f,77.50f};
		System.out.println("Java float Array Example");
		for(int i=0;i<f.length;i++)
		{
			System.out.println("Element at Index : "+ i + " " + f[i]);
		}
	}
}

Related : Java byte Array

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

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

Following example shows How to declare Java float array with other Java float variables . Save the following declare Java float array with other Java float variables Example program with file name DeclareFloatArrayWithFloatVariables.java .

Declare Java float array with other Java float variables Example

public class DeclareFloatArrayWithFloatVariables
{
	public static void main(String args[])
	{
		float f[], f2;
		f = new float[4];
		f[0] = 10.10f;
		f[1] = 30.3f;
		f[2] = 40.60f;
		f[3] = 77.50f;
		f2 = 555.55f;
		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]);
		}
	}
}

Related : Java char Array

How to assign Java float array to other Java float array ?

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

Following example shows How to assign Java float array to other Java float array . Save the following assign Java float array to other Java float array Example program with file name AssingFloatArrayToFloatArray.java .

Assign Java float array to other Java float array Example

public class AssingFloatArrayToFloatArray
{
	public static void main(String args[])
	{
		float[] f, f2;
		f = new float[4];
		f[0] = 10.10f;
		f[1] = 30.3f;
		f[2] = 40.60f;
		f[3] = 77.50f;
		f2 = f;
		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]);
		}
	}
}

Related : Java short Array

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.

Following example shows How to Fill all the elements of Java float Array . Save the following Fill all the elements of Java float Array Example program with file name FillFloatArrayExample.java .

Fill all the elements of Java float Array to 1 Example

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]);
		}
	}
}

Related : Java int Array

How to Convert a double array to a float array in Java ?

Convert a double array to a float array in Java is not possible by using casting the array. You need to explicitly convert each array item. Following example shows how to convert a double array to a float array in Java.

Following example shows How to Convert a double array to a float array in Java . Save the following Convert a double array to a float array in Java Example program with file name ConvertDoubleArrayToFloatArray.java .

Convert a double array to a float array in Java Example

public class DoubleArrayToFloatArray
{
	public static void main(String args[])
	{
		double[] doubleArray = {3.5,5.0,7.5,11.55};
		float[] floatArray = new float[doubleArray.length];
		for (int i = 0 ; i < doubleArray.length; i++)
		{
			floatArray[i] = (float) doubleArray[i];
		}
		for(int i=0; i < floatArray.length; i++)
		{
			System.out.println("Element at Index "+ i + " is : " + floatArray[i]);
		}
	}
}

Related : Java long Array

How to Convert a float array to a double array in Java ?

Convert a float array to a double array in Java is not possible by using casting the array. You need to explicitly convert each array item. Following example shows how to convert a float array to a double array in Java. In the below example don't forget to put f after the float values. The float values must be specify with f because Java compiler treat as double value.

Following example shows How to Convert a float array to a double array in Java . Save the following Convert a float array to a double array in Java Example program with file name ConvertFloatArrayToDoubleArray.java .

Convert a float array to a double array in Java Example

public class ConvertFloatArrayToDoubleArray
{
	public static void main(String args[])
	{
		float[] floatArray = {2.0f,1.5f,8.45f,116.77f};
		double[] doubleArray = new double[floatArray.length];
		for (int i = 0 ; i < floatArray.length; i++)
		{
			doubleArray[i] = (double) floatArray[i];
		}
		for(int i=0; i < doubleArray.length; i++)
		{
			System.out.println("Element at Index "+ i + " is : " + doubleArray[i]);
		}
	}
}

Related : Java double Array