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

Java long Array

long Array in Java

Java long array is used to store long data type values only in Java. The default value of the elements in a Java long array is 0. Java long array variable can also be declared like other variables with [] after the data type. The size of an array must be specified by an int value and not long or short. The long array index beginning from 0 in Java. Java array can also be used as a static field, a local variable or a method parameter. In this tutorial you can learn how to use long array in Java.

With the following Java long array examples you can learn

I found lot of people search with keywords like java long array, long array in java, long array, how to create long array, long array example from google, bing and other search engines. But in their search they like to find contents for big array in the sense they store huge amount of data or objects.

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.

What does long mean in Java ?

long is a primitive data type in Java. long means numeric datatype. The minimum value of long is -9223372036854775808 and the maximum value of long is 9223372036854775807. It occupies 8 bytes memory. The default value of long is 0 (zero).

What is long array in Java ?

The array which holds primitive data type long in it.

How to declare long 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.

long[] longArray;

Initializing a long Array in Java

The long array will be initialized to 0 when you allocate it. All arrays in Java are initialized to the default value for the type. This means that arrays of ints are initialised to 0, arrays of longs are initialised to 0, arrays of booleans are initialised to false and arrays of reference types are initialised to null.

long[] longArray = new long[10];

What is the Initial or Default Value of a long Array in Java ?

The initial value of a long array is 0 (zero). Because the default value of a long data type in Java is 0 (zero).

Can you index an array with a long int ?

An attempt to access an array component with a long index value results in a compile-time error. If for some reason you have an index stored in a long, just cast it to an int and then index your array. You cannot create an array large enough so it cannot be indexed by an integer in Java. If you cast long index to int index you will get java.lang.ArrayIndexOutOfBoundsException. ArrayIndexOutOfBoundsException is thrown to indicate that we are trying to access array element with an illegal index. This exception is thrown when the index is either negative or greater than or equal to the size of the array.

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

How to assign values to Java long array ?

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

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

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

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

How to declare Java long array with other Java long 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 long array with other Java long array variables Example
    Save with file name LongArray3.java */
  
public class LongArray3
{
  public static void main(String args[])
  {
    //JAVA LONG ARRAY DECLARATION
    //a IS AN ARRAY a2 IS NOT AN ARRAY
    long a[], a2;
    //MEMORY ALLOCATION FOR JAVA LONG ARRAY
    a = new long[4];
    //ASSIGNING ELEMENTS TO JAVA LONG ARRAY
    a[0] = 100000L;
    a[1] = 300000L;
    a[2] = 400000L;
    a[3] = 786777L;
    a2 = 222222L;
    //JAVA LONG ARRAY OUTPUT
    System.out.println("Java long 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 long array example you can learn how to assign Java long array to other Java long array.

How to assign Java long array to other Java long 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
/*  How to assign Java long array to other Java long array Example
    Save with file name LongArray4.java */
  
public class LongArray4
{
  public static void main(String args[])
  {
    //JAVA LONG ARRAY DECLARATION
    long[] a, a2; //a AND a2 ARE ARRAY VARIABLES
    //MEMORY ALLOCATION FOR JAVA LONG ARRAY
    a = new long[4];
    //ASSIGNING ELEMENTS TO JAVA LONG ARRAY
    a[0] = 100000L;
    a[1] = 300000L;
    a[2] = 400000L;
    a[3] = 786777L;
    //ASSIGNING a ARRAY TO a2 ARRAY VARIABLE
    a2 = a;
    //JAVA LONG ARRAY OUTPUT
    System.out.println("Java long 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 long Array to String in Java ?

The java.util.Arrays.toString(long[]) method returns a string representation of the contents of the specified long array. The string representation consists of a list of the array's elements, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (a comma followed by a space).

The following example shows the usage of java.util.Arrays.toString() method.

How to Convert Java long array to String Example

1
2
3
4
5
6
7
8
9
10
11
12
13
/*  How to Convert Java long array to String Example
    Save with file name ConvertLongArrayToString.java */
  
public class ConvertLongArrayToString
{
  public static void main(String args[])
  {
    //L ARRAY DECLARATION
    long long_array[] = {3, 7, 2, 1};
    System.out.println("converted long array to String");
    System.out.println(Arrays.toString(long_array);
  }
}
Download Java long Array in Formats