Close

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

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


Examples


package com.logicbig.example.array;

import java.lang.reflect.Array;

public class GetBooleanExample {

public static void main(String... args) {
boolean[] ar = {true, false, true, true};
boolean b = Array.getBoolean(ar, 2);
System.out.println(b);
}
}

Output

true




This method does not work for Array of Boolean objects:

package com.logicbig.example.array;

import java.lang.reflect.Array;

public class GetBooleanExample2 {

public static void main(String... args) {
Boolean[] ar = {true, false, true, true};
boolean b = Array.getBoolean(ar, 2);
System.out.println(b);
}
}

Output

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




See Also