ALL objects in Java extend Object.

Therefore it is possible to be completely non-descriptive when you create the array by declaring it an array of Objects:

Object[] arr = new Object[6];

This code creates an array of objects of length 6.

So for instance, you could create an array where entries come in pairs of two. In this case, the first object is a String and the second is an Integer.

Object[] arr = new Object[6];

arr[0] = new String("First Pair");
arr[1] = new Integer(1);
arr[2] = new String("Second Pair");
arr[3] = new Integer(2);
arr[4] = new String("Third Pair");
arr[5] = new Integer(3);

Now if you want to actually figure out what these objects are then it will require a cast:

int x = (Integer)arr[1];
Answer from M Sach on Stack Overflow
Top answer
1 of 10
14

Firstly, it's worth being clear about the difference between an array and an ArrayList - they're not the same thing at all.

However, in either case you can't do what you want. The closest you can probably come is declaring your own type. (EDIT: My original code had a double or a string... I've now changed it to be a double and a string. Let me know if this change isn't what you had in mind.)

public final class DoubleAndString
{
    private final String stringValue;
    private final double doubleValue;

    public DoubleAndString(String stringValue, double doubleValue)
    {
        this.stringValue = stringValue;
        this.doubleValue = doubleValue;
    }

    public String getString()
    {
        return stringValue;
    }

    public String getDouble()
    {
        return doubleValue;
    }
}

Then create an ArrayList<DoubleAndString> or a DoubleAndString[].

Now, this feels somewhat vanilla at the moment - presumably the double and string values actually have a greater meaning - a name and a score, for example. If so, encapsulate that in a type which describes the pairing more appropriately.

As for ordering - you could make DoubleAndString implement Comparable<DoubleAndString> - but unless that's the only natural ordering which makes sense, I'd write a Comparator<DoubleAndString>:

public class DoubleComparator implements Comparator<DoubleAndString>
{
    public int compare(DoubleAndString ds1, DoubleAndString ds2)
    {
        return Double.compare(ds1.getDouble(), ds2.getDouble());
    }
}

Then you can use Collections.sort to sort an ArrayList<DoubleAndString> or Arrays.sort to sort an array.

2 of 10
7

You can use ArrayList<Object> and you can then use anything you'd like. Encapsulate the double in a Double object and when you retrieve the object use instanceof to check if it's really a double or a String.

I must say, it's unlikely this 'design' would win you any awards. Is it possible to rethink the solution you're considering for your problem, and see if you could do with a different kind of approach?

Discussions

java - Multiple type array - Stack Overflow
I am working with an array and need some help. I would like to create an array where the first field is a String type and the second field is an Integer type. For result: Console out a 1 b 2 c 3 More on stackoverflow.com
🌐 stackoverflow.com
Java Array with multiple data types - Stack Overflow
Then to retrieve values you would ... were of the same class. ... Save this answer. ... Show activity on this post. ... You can use the built-in Object class to create an array that supports Strings, Integers, etc. ... Save this answer. ... Show activity on this post. I have created an array (array3 with type var from Java 10+ ) to hold values from two different data types ... More on stackoverflow.com
🌐 stackoverflow.com
java - How to store multiple datatypes in an array? - Stack Overflow
The most simplistic way of storing objects of different data types is just by declaring the type of your Array(or Collection) as an "Object". ... Java.lang.Object class is the root or superclass of the class hierarchy. All predefined classes and user-defined classes are the subclasses from ... More on stackoverflow.com
🌐 stackoverflow.com
An array with mixed data types?
As others have said it is doable, but if you think you need to do something like this, you should rethink your approach because there seems to be something seriously wrong design wise. More on reddit.com
🌐 r/learnjava
9
6
December 10, 2019
🌐
Coderanch
coderanch.com › t › 656501 › java › create-array-multiple-data-types
How to create an array with multiple data types ? (Beginning Java forum at Coderanch)
October 12, 2015 - in Java, arrays can hold one kind of thing - and only one kind. Now, that thing may itself hold multiple things, as Jeanne suggests (and she is very smart - you should pay attention to anything she tells you). There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors ... Your different types need to all extend the same class, then you can use generics to declare the type of Array, ArrayList, or other containers you want to use for your implementation.
🌐
Software Testing Help
softwaretestinghelp.com › home › java › array data types – int array, double array, array of strings etc.
Array Data Types - int Array, Double array, Array of Strings Etc.
April 1, 2025 - Some arrays like character arrays or string arrays behave little differently than the rest of the data types. In this tutorial, we will walk you through arrays with different data types and discuss their usage in Java programs by giving examples.
Top answer
1 of 4
28

Java is a strongly typed language. In PHP or Javascript, variables don't have a strict type. However, in Java, every object and primative has a strict type. You can store mutliple types of data in an Array, but you can only get it back as an Object.

You can have an array of Objects:

Object[] objects = new Object[3];
objects[0] = "foo";
objects[1] = 5;

Note that 5 is autoboxed into new Integer(5) which is an object wrapper around the integer 5.

However, if you want to get data out of the array, you can only get it as an Object. The following won't work:

int i1 = objects[1]; // Won't work.
Integer i2 = objects[2]; // Also won't work.

You have to get it back as an Object:

Object o = objects[0]; // Will work.

However, now you can't get back the original form. You could try a dangerous cast:

String s = (String) o;

However you don't know that o is a String.

You can check with instanceof:

String s = null;

if (o instanceof String)
    s = (String) o;
2 of 4
4

You could use an object array but that creates problems when the time comes to retrieve the objects you have stored. Instead I would use a typesafe heterogenous container as described in Effective Java (and linked to earlier in this sentence).

public class DateStuff{

    private Map<Class<?>, Object> dateMap =
        new HashMap<Class<?>, Object>();

    public <T> void putDate(Class<T> type, T instance){
          if(type == null)
              throw new NullPointerException("Type null");
          dateMap.put(type, instance);
    } 

    public<T> getDate(Class<T> type){
          return type.cast(dateMap.get(type));
    }

}

The typesafe heterogenous container solves the problem of retrieving objects later by mapping objects by their class. In your case I would combine this with other data structures - for example List<Date>, List<String>, or List<Integer>, as the base classes to provide a way to store multiple different kinds of objects in one collection. Then to retrieve values you would simply get the sub collection, e.g. a List<Date>, knowing that all items contained therein were of the same class.

Top answer
1 of 4
11

You can use an ArrayList.

ArrayList<Object> listOfObjects = new ArrayList<Object>();

And then add items to it.

listOfObjects.add("1");
listOfObjects.add(someObject);

Or create your own object that encapsulates all the field that you require like

public class LocationData {
   private double lat;
   private double longitude;

   public LocationData(double lat, double longitude) {
       this.lat = lat;
       this.longitude = longitude;
   }
   //getters
   //setters
}

and then add your lat/long pairs to an ArrayList of type LocationData

ArrayList<LocationData> listOfObjects = new ArrayList<LocationData>();

listOfObjects.add(new LocationData(lat, longitude));
2 of 4
7

You can create an array of your Custom-Class.

public class YourCustomClass {

     String id;
     String name;
     double longitude;
     // and many more fields ...

    public YourCustomClass() {  // constructor 

    }

    public void setID(String id) {
        this.id = id;
    }

    public String getID() {
        return id;
    }

    // and many more getter and setter methods ...
}

Inside your custom-class you can have as many fields as you want where you can store your data, and then use it like that:

// with array 
YourCustomClass [] array = new YourCustomClass[10];
array[0] = new YourCustomClass();
array[0].setID("yourid");

String id = array[0].getID();

// with arraylist
ArrayList<YourCustomClass> arraylist = new ArrayList<YourCustomClass>();
arraylist.add(new YourCustomObject());
arraylist.get(0).setID("yourid");

String id = arraylist.get(0).getID();

You can also let the AsyncTasks doInBackground(...) method return your Custom-class:

protected void onPostExecute(YourCustomClass result) {
 // do stuff...
}

Or an array:

protected void onPostExecute(YourCustomClass [] result) {
 // do stuff...
}

Or a ArrayList:

protected void onPostExecute(ArrayList<YourCustomClass> result) {
 // do stuff...
}

Edit: Of course, you can also make a ArrayList of your custom object.

Find elsewhere
🌐
Quora
quora.com › Is-it-possible-to-load-an-array-with-multiple-data-types-e-g-booleans-integers-doubles-and-strings-all-in-one-array-in-Java-If-so-how
Is it possible to load an array with multiple data types (e.g., booleans, integers, doubles, and strings) all in one array in Java? If so, how? - Quora
Answer (1 of 7): The question originally did not specify Java. My answer is generic. It depends on what language. For purposes of general programming, arrays are homogenous data types. That is they are data of the same type laid out contiguously in memory. Each element must be the same size of e...
🌐
Edureka Community
edureka.co › home › community › categories › java › how to declare an array of different data types
How to declare an array of different data types - Java
August 3, 2022 - In Java, I am working with arrays, and I have a query. I am aware that a Java array is ... other collection in Java that can hold various data types?
🌐
Oracle
docs.oracle.com › javase › specs › jls › se7 › html › jls-10.html
Chapter 10. Arrays
4 days ago - There are some situations in which an element of an array can be an array: if the element type is Object or Cloneable or java.io.Serializable, then some or all of the elements may be arrays, because any array object can be assigned to any variable of these types.
🌐
TutorialsPoint
tutorialspoint.com › What-are-the-types-of-arrays-in-Java
Java - Arrays
Following statement declares an array variable, myList, creates an array of 10 elements of double type and assigns its reference to myList −
Top answer
1 of 5
35
public class Book
{
    public int number;
    public String title;
    public String language;
    public int price;

    // Add constructor, get, set, as needed.
}

then declare your array as:

Book[] books = new Book[3];

EDIT: In response to O.P.'s confusion, Book should be an object, not an array. Each book should be created on it's own (via a properly designed constructor) and then added to the array. In fact, I wouldn't use an array, but an ArrayList. In other words, you are trying to force data into containers that aren't suitable for the task at hand.

I would venture that 50% of programming is choosing the right data structure for your data. Algorithms naturally follow if there is a good choice of structure.

When properly done, you get your UI class to look like: Edit: Generics added to the following code snippet.

...
ArrayList<Book> myLibrary = new ArrayList<Book>();
myLibrary.add(new Book(1, "Thinking In Java", "English", 4999));
myLibrary.add(new Book(2, "Hacking for Fun and Profit", "English", 1099);

etc.

now you can use the Collections interface and do something like:

int total = 0;
for (Book b : myLibrary)
{
   total += b.price;
   System.out.println(b); // Assuming a valid toString in the Book class
}
System.out.println("The total value of your library is " + total);
2 of 5
4

Notice the repetition of Book in Booknumber (int), Booktitle (string), Booklanguage (string), Bookprice (int)- it screams for a class type.

class Book {
  int number;
  String title;
  String language;
  int price;
}

Now you can simply have:

Book[] books = new Books[3];

If you want arrays, you can declare it as object array an insert Integer and String into it:

Object books[3][4]
🌐
DaniWeb
daniweb.com › programming › software-development › threads › 510327 › creating-an-array-of-different-data-types
java - Creating an array of different data types | DaniWeb
This keeps the design type-safe, extensible, and easy to maintain. ... Because every object extends Object, and primitives are auto boxed/unboxed into the corresponding objects as required, then you can put anything into a multi-dimensional array of Object (including other arrays). Having said that, it's a terrible … — JamesCherrill 4,733 Jump to Post ... Not in Java.
🌐
Quora
quora.com › Can-an-array-contain-different-datatypes
Can an array contain different datatypes? - Quora
Answer (1 of 11): Assuming you mean plain C arrays - as indicated by the list of topics - then no. There are a few ways to circumvent this restriction, such as using void pointers and perhaps tagging each one with some type information but this is generally speaking a bad idea. Is there a partic...
🌐
LearnYard
read.learnyard.com › java-fundamentals › types-of-array-in-java
Types of Arrays in Java: Simplified Guide for DSA Beginners
January 8, 2025 - Explore the types of arrays in Java—single-dimensional, multidimensional, and jagged arrays. Perfect for students learning Data Structures and Algorithms (DSA) and programming basics with examples and tips.
🌐
GeeksforGeeks
geeksforgeeks.org › java › creating-an-arraylist-with-multiple-object-types-in-java
Creating an ArrayList with Multiple Object Types in Java - GeeksforGeeks
July 23, 2025 - The code given below presents an example of the ArrayList with the Objects of multiple types. ... // Java program to create an ArrayList with // Multiple Object Types in Java import java.util.ArrayList; public class GFG { public static void main(String[] args) { // Creating an ArrayList of Object type ArrayList<Object> arr = new ArrayList<Object>(); // Inserting String value in arr arr.add("GeeksForGeeks"); // Inserting Integer value in arr arr.add(14); // Inserting Long value in arr arr.add(1800L); // Inserting Double value in arr arr.add(6.0D); // Inserting Float value in arr arr.add(1.99F); // arr after all insertions: ["GeeksForGeeks", 14, // 1800L, 6.0D, 1.99F] System.out.print( "ArrayList after all insertions: "); display(arr); // Replacing element at index 0 (i.e.