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 float array is false.

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

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 collection framework or simply use Vector.

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

Java float Array Example

/* 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

/* 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

/* 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

		float f[], f2; //f IS AN ARRAY f2 IS NOT AN ARRAY

		//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

/* 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

		float[] f, f2; //f AND f2 ARE ARRAY VARIABLES

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