If you do not want to make the switch for all primitive types you could do like this:

if (ofArray.isPrimitive()) {
    int length = Array.getLength(o);
    for (int i = 0; i < length; i++) {
        Object obj = Array.get(o, i);
        System.out.println(obj);
    }
}
else {
    Object[] objects = (Object[]) o;
    for (Object obj : objects) {
        System.out.println(obj);
    }
}

Edit: If you don't want to loop in two different places in your code use this method to convert the primitive arrays to an object array:

static Object[] convertToObjectArray(Object array) {
    Class ofArray = array.getClass().getComponentType();
    if (ofArray.isPrimitive()) {
        List ar = new ArrayList();
        int length = Array.getLength(array);
        for (int i = 0; i < length; i++) {
            ar.add(Array.get(array, i));
        }
        return ar.toArray();
    }
    else {
        return (Object[]) array;
    }
}
Answer from Vegard on Stack Overflow
🌐
ServiceNow Community
servicenow.com › community › developer-forum › please-suggest-how-to-convert-object-to-array-in-javascript › m-p › 2657108
Please suggest how to convert object to array in j... - ServiceNow Community
August 30, 2023 - To convert an ordinary object that's not iterable or array-like to an array (by enumerating its property keys, values, or both), use Object.keys() , Object.values() , or Object.entries() . To convert an async iterable to an array, use Array.fromAsync() .
🌐
Coderanch
coderanch.com › t › 516610 › java › Cast-Object-Object
Cast Object to Object[] (Java in General forum at Coderanch)
November 9, 2010 - You can use Generics- ArrayList<T> where T can be- Integer, Double, Boolean ... You can then store your elements in this list instead of putting them in an array then typecasting it and then again creating List<Object>. Mohamed Sanaulla | My Blog | Author of Java 9 Cookbook | Java 11 Cookbook
🌐
W3Docs
w3docs.com › java
How to convert object array to string array in Java
To convert an object array to a string array in Java, you can use the toString() method of the Object class and the map() method of the Stream class.
Top answer
1 of 4
28

If you do not want to make the switch for all primitive types you could do like this:

if (ofArray.isPrimitive()) {
    int length = Array.getLength(o);
    for (int i = 0; i < length; i++) {
        Object obj = Array.get(o, i);
        System.out.println(obj);
    }
}
else {
    Object[] objects = (Object[]) o;
    for (Object obj : objects) {
        System.out.println(obj);
    }
}

Edit: If you don't want to loop in two different places in your code use this method to convert the primitive arrays to an object array:

static Object[] convertToObjectArray(Object array) {
    Class ofArray = array.getClass().getComponentType();
    if (ofArray.isPrimitive()) {
        List ar = new ArrayList();
        int length = Array.getLength(array);
        for (int i = 0; i < length; i++) {
            ar.add(Array.get(array, i));
        }
        return ar.toArray();
    }
    else {
        return (Object[]) array;
    }
}
2 of 4
2

This will work for arrays with elements that are sublcasses of Object:

if (clazz.isArray()) {
    Object[] objects = (Object[]) o;
    for (Object obj : objects)
        System.out.println(obj);
}   

If you need to cast the array to an specific array type you could (ab)use instanceof but for, say, just printing the contents as strings, an Object[] suffices.

UPDATE

If the array is of primitive types, you have no option but to test for each one and cast to the correct type, Object[] won't work for this case - but there aren't that many primitive types in Java, so it won't be a gigantic switch :) . This is what I mean:

if (clazz.isArray()) {
    if (clazz.getComponentType().equals(double.class)) {
        double[] numbers = (double[]) o;
        for (double d : numbers)
            System.out.println(d);
    }
}  
🌐
Stack Overflow
stackoverflow.com › questions › 42933908 › convert-object-to-array-java › 42970799
Convert Object to Array [Java]? - Stack Overflow
As you can see casting the value is needed as well since there's no generic type provided for the map and thus all you know is that keys and values are objects of any class. If you know what generic type should be used you could improve that, especially if the value type is String. Unfortunateley due to type erasure you can't get that type at runtime so the only way to get to know what to expect would be some documentation.
🌐
Baeldung
baeldung.com › home › java › java array › convert an array of primitives to an array of objects
Convert an Array of Primitives to an Array of Objects | Baeldung
June 14, 2025 - Now, let’s convert from an array of objects to an array of primitives: Integer[] input = new Integer[] { 0, 1, 2, 3, 4 }; int[] expected = new int[] { 0, 1, 2, 3, 4 }; int[] output = new int[input.length]; for (int i = 0; i < input.length; i++) { output[i] = input[i]; } assertArrayEquals(expected, output); As we can see, this is not complicated at all, but a more readable solution, like the Stream API, might suit our needs better. Since Java 8, we can use the Stream API to write fluent code.
🌐
Microsoft Learn
learn.microsoft.com › en-us › answers › questions › 1021381 › convert-java-lang-object-to-array
Convert Java.Lang.Object to Array - Microsoft Q&A
I have a Java.Lang.Object which was created from a float[][][][] using Java.Lang.Object.FromArray(xxx). I now need to convert the Java.Lang.Object back to a float[][][][]. I am not very familiar with converting between Java.Lang.Object and .NET. How can…
🌐
How to do in Java
howtodoinjava.com › home › java array › convert int[] to integer[] in java and back to int[]
Convert int[] to Integer[] in Java and Back to int[]
February 10, 2022 - Java has no cast between int[] and Integer[], so to convert int[] to Integer[] we box every element into a new array, most often with Arrays.stream(ints).boxed().toArray(Integer[]::new), and to go back we unbox every element with mapToInt(Integer::intValue).toArray(). Both directions create a new array and leave the original unchanged. We need an Integer[] when an API works only with objects, for example Arrays.sort() with a Comparator or a generic method that takes T[]. We go back to int[] for arithmetic and for compact storage of large numeric data.
Find elsewhere
🌐
Quora
quora.com › How-do-I-convert-an-object-into-String-array
How to convert an object into String array - Quora
Answer (1 of 4): You can’t. Objects, strings, and arrays are three totally different concepts, and none of them can be “converted” to the others. Oh, they’re sometimes related, but they can’t be converted, no. Probably what you actually want is to write strings representing some of ...
🌐
Stack Overflow
stackoverflow.com › questions › 16811235 › convert-object-to-array
java - Convert Object[] to Array() - Stack Overflow
May 24, 2017 - What exactly do you have, and what exactly do you want to convert it to. ... Could you provide more info on what you try to achieve? Are you implementing a Collection, a List? Where is the method toArray(Object[]) implemented? ... Save this answer. ... Show activity on this post. ... Copypublic ArrayList<Object> toArray(Object[] object){ ArrayList<Object> array = new ArrayList<Object>(); for(Object obj : object){ array.add(obj); } return array; }
🌐
TutorialsPoint
tutorialspoint.com › how-to-convert-Object-array-to-String-array-in-java
how to convert Object array to String array in java
July 30, 2019 - As list.toArray() returns an Object[], it can be converted to String array by passing the String[] as parameter. See the example below. import java.util.ArrayList; import java.util.List; public class Tester { public static void main(String[] args) { List<String> data = new ArrayList<String>(); ...
🌐
Medium
medium.com › @LearnITbyPrashant › convert-java-object-into-an-array-d02ce64f6a7b
Convert Java object into an Array - Prashant Kumar LearnIT - Medium
January 4, 2023 - Convert Java object into an Array You can get the logged-in user’s group converted to an array using this syntax. var groups = new global.ArrayUtil().convertArray(gs.getUser().getMyGroups());
🌐
Software Testing Help
softwaretestinghelp.com › home › java › covert list to array and other collections in java
Covert List To Array And Other Collections In Java
April 1, 2025 - The following program shows this ... strList.add("Tutorial"); System.out.println("The given list:" + strList); //use toArray method to convert list to array String[] strArray = strList.toArray(new String[0]); //print the array ...
🌐
Oracle
forums.oracle.com › ords › apexds › post › how-to-convert-cast-an-object-to-an-array-6261
How to convert/cast an Object to an array? - Oracle Forums
January 11, 2008 - I have been trying to re-use a method that accepts an Object parameter and handling the different types within it. Everything works as expected until I get to arrays. I have had very limited success i...
🌐
Techie Delight
techiedelight.com › home › java › convert object array to integer array in java
Convert Object array to Integer array in Java | Techie Delight
July 7, 2026 - Use Stream.map() to convert every object in the stream to their integer representation. Use the toArray() method to accumulate the stream elements into a new integer array.
🌐
Codemia
codemia.io › home › knowledge hub › how to convert object array to string array in java
How to convert object array to string array in Java | Codemia
January 27, 2025 - Converting an Object[] to a String[] is only simple if you first decide what "convert" means. Sometimes the array already contains only strings and you want a cast-like copy. Other times the array contains mixed object types and you want each element turned into text with String.valueOf. If the goal is textual conversion, use String.valueOf rather than calling toString() directly: ... 1import java.util.Arrays; 2 3public class Main { 4 public static void main(String[] args) { 5 Object[] objects = {42, true, null, "java"}; 6 7 String[] strings = Arrays.stream(objects) 8 .map(String::valueOf) 9 .toArray(String[]::new); 10 11 System.out.println(Arrays.toString(strings)); 12 } 13}
🌐
Edureka Community
edureka.co › home › community › categories › java › how an object array can be converted to string...
How an object array can be converted to string array in java | Edureka Community
June 8, 2018 - I use the following code to convert an Object array to a String array : Object Object_Arr[]=new Obj[ ... I wonder if there is another way to do this?
🌐
GeeksforGeeks
geeksforgeeks.org › java › how-to-convert-json-array-object-to-java-object
How to Convert JSON Array Object to Java Object? - GeeksforGeeks
July 23, 2025 - In the above code, we have a JSON string jsonString representing an array of user objects. We have created an ObjectMapper instance from the Jackson library, which helps with JSON processing. We have used the readValue() method of the ObjectMapper to convert the JSON string into an array of User objects.