Close

Java Reflection - Array.getShort() 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 short getShort(Object array,
                             int index)
                      throws IllegalArgumentException,
                             ArrayIndexOutOfBoundsException

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


Examples


package com.logicbig.example.array;

import java.lang.reflect.Array;

public class GetShortExample {

public static void main(String... args) {
Object o = new short[]{1, 4, 9};
Short s = Array.getShort(o, 2);
System.out.println(s);
}
}

Output

9




package com.logicbig.example.array;

import java.lang.reflect.Array;

public class GetShortExample2 {

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

Output

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




See Also