Previous
Next

Java double Array - double Array in Java

Last updated on

double Array in Java

double Array

Java double Array

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

With the following java double array examples you can learn

What is double in Java ?

The double is a keyword in Java and also a primitive data type. The double data type is a double precision 64-bit IEEE 754 floating point in Java. Its range of values is beyond the scope of this discussion, but is specified in the Floating-Point Types, Formats, and Values section of the Java Language Specification. For decimal values, double data type is generally the default choice. As mentioned above, this data type should never be used for precise values, such as currency.

What is a double array in Java ?

A double array is an array that holds a primitive double data type values or Java Double wrapper class in Java.

How to create a double array in Java ?

Creating a double array in Java is very simple, it is in a single line. You could use a double array to store a collection of double data. You can define a double array in Java as follows :

// creating a Java double array
double double_array[] = new double[10];

Share This Page On :

Initializing a double 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 doubles
double[] doubleArray;

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

Java double Array Example

/*  Java double Array Example
    Save with file name DoubleArray.java    */
   
public class DoubleArray
{
  public static void main(String args[])
  {
    //JAVA DOUBLE ARRAY DECLARATION
    double a[];
    //MEMORY ALLOCATION FOR JAVA DOUBLE ARRAY
    a = new double[4];
    //ASSIGNING ELEMENTS TO JAVA DOUBLE ARRAY
    a[0] = 100000d;
    a[1] = 300000d;
    a[2] = 400000d;
    a[3] = 786777d;
    //JAVA DOUBLE ARRAY OUTPUT
    System.out.println("Java double Array Example");
    for(int i=0;i<a.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + a[i]);
    }
  }
}

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

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

/*  How to assign values to Java double array at the time of declaration Example
    Save with file name DoubleArray2.java   */
   
public class DoubleArray2
{
  public static void main(String args[])
  {
    //JAVA DOUBLE ARRAY DECLARATION
    double a[] = {100000d,300000d,400000d,786777d};
    //JAVA DOUBLE ARRAY OUTPUT
    System.out.println("Java double Array Example");
    for(int i=0;i<a.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + a[i]);
    }
  }
}

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

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

/*  How to declare Java double array with other Java double array variables Example
    Save with file name DoubleArray3.java   */
   
public class DoubleArray3
{
  public static void main(String args[])
  {
    //JAVA DOUBLE ARRAY DECLARATION
    double a[], a2;
    //MEMORY ALLOCATION FOR JAVA DOUBLE ARRAY
    a = new double[4];
    //ASSIGNING ELEMENTS TO JAVA DOUBLE ARRAY
    a[0] = 100000d;
    a[1] = 300000d;
    a[2] = 400000d;
    a[3] = 786777d;
    a2 = 333333d;
    //DOUBLE ARRAY OUTPUT
    System.out.println("Java double Array Example");
    System.out.println("a2 value is : "+a2);
    for(int i=0;i<a.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + a[i]);
    }
  }
}

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

How to assign Java double array to other Java double array

/*  How to assign Java double array to other Java double array Example
    Save with file name DoubleArray4.java   */
   
public class DoubleArray4
{
  public static void main(String args[])
  {
    //JAVA DOUBLE ARRAY DECLARATION
     //a AND a2 ARE ARRAY VARIABLES
    double[] a, a2;
    //MEMORY ALLOCATION FOR JAVA DOUBLE ARRAY
    a = new double[4];
    //ASSIGNING ELEMENTS TO JAVA DOUBLE ARRAY
    a[0] = 100000d;
    a[1] = 300000d;
    a[2] = 400000d;
    a[3] = 786777d;
    //ASSIGNING a ARRAY TO a2 ARRAY VARIABLE
    a2 = a;
    //JAVA DOUBLE ARRAY OUTPUT
    System.out.println("Java double Array Example");
    System.out.println("a array values");
    for(int i=0;i<a.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + a[i]);
    }
    System.out.println("a2 array values");
    for(int i=0;i<a2.length;i++)
    {
      System.out.println("Element at Index : "+ i + " " + a2[i]);
    }
  }
}

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.

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

/*  How to Convert a double array to a float array in Java Example
    Save with file name DoubleArrayToFloatArray.java   */
    
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]);
    }
  }
}

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.

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

/*  How to Convert a float array to a double array in Java Example
    Save with file name FloatArrayToDoubleArray.java   */
    
public class FloatArrayToDoubleArray
{
  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]);
    }
  }
}
    

More Related Tutorials

Previous
Next