Close

Java Reflection - Array.getLong() Examples

Java Reflection Java Java API 


Class:

java.lang.reflect.Array

java.lang.Objectjava.lang.Objectjava.lang.reflect.Arrayjava.lang.reflect.ArrayLogicBig

Method:

public static long getLong(Object array,
                           int index)
                    throws IllegalArgumentException,
                           ArrayIndexOutOfBoundsException

Returns the value of the indexed component in the specified array object, as a long .


Examples


package com.logicbig.example.array;

import java.lang.reflect.Array;

public class GetLongExample {

public static void main(String... args) {
Object o = new long[]{1, 4, 9};
long aLong = Array.getLong(o, 2);
System.out.println(aLong);
}
}

Output

9




Does not work with java.lang.Long:

package com.logicbig.example.array;

import java.lang.reflect.Array;

public class GetLongExample2 {

public static void main(String... args) {
Object o = new Long[]{1l, 4l, 9l};
long aLong = Array.getLong(o, 2);
System.out.println(aLong);
}
}

Output

Caused by: java.lang.IllegalArgumentException: Argument is not an array of primitive type
at java.base/java.lang.reflect.Array.getLong(Native Method)
at com.logicbig.example.array.GetLongExample2.main(GetLongExample2.java:15)
... 6 more




See Also