Is storing elements with different data types in the same array possible with Java?
Tags: java, arrays
Published: 2023-10-12 02:39:46

---
title: Is storing elements with different data types in the same array possible with Java?
published: true
date: 2023-07-08 20:03:57 UTC
tags: Java,Arrays
canonical_url: https://crtv.dev/is-it-possible-to-store-elements-with-different-data-types-in-the-same-array/
---

 ![Is storing elements with different data types in the same array possible with Java?](https://images.unsplash.com/photo-1532996122724-e3c354a0b15b?crop=entropy&cs=tinysrgb&fit=max&fm=jpg&ixid=M3wxMTc3M3wwfDF8c2VhcmNofDR8fGJpbnN8ZW58MHx8fHwxNjg4ODQ2MTE0fDA&ixlib=rb-4.0.3&q=80&w=2000)

Kinda.

As you can see, if I try to store a floating point number in this int array, my IDE screams at me:

![Is storing elements with different data types in the same array possible with Java?](https://king.instructure.com/users/11006/files/2533032/preview?verifier=WjwgatSByd9RNhyA1s4XxfrpoJcaAKHR3MnWTunm)

TL;DR When you declare an array in Java, you have to specify the type, but the type doesn't have to be a primitive type. If you use the class Object for your type, you can pass in practically anything you want.

Let's look deeper...

Java has two different data types: Primitive and Reference. If you remember, primitive data types point to values stored directly in memory, while reference types point to other memory locations where the actual data is stored. Primitive data types are very efficient because they use much less memory than reference types. What you may not remember is that Java provides wrapper classes. These classes wrap all the primitive data types within a class and provide methods that help programs work with that data type.

Here's an example of wrapper classes in action:

![Is storing elements with different data types in the same array possible with Java?](https://king.instructure.com/users/11006/files/2533033/preview?verifier=TSoBmh37zZuqok5ervM5GiPgQSdhVwQv53caAucD)

Java is an OOP (object-oriented programming) language which means it supports polymorphism and inheritance. Java provides a class called Object. This class "is the root of the class hierarchy. Every class has Object as a superclass. All objects, including arrays, implement the methods of this class." (Oracle, n.d.) Since Object is the superclass of all other classes and wrapper classes are classes, that means we can use wrapper classes in our array of Objects.

Here's an example:

![Is storing elements with different data types in the same array possible with Java?](https://king.instructure.com/users/11006/files/2533034/preview?verifier=v4hcYXvw5U5MDcB1DvDxJZDMcICNPS27UnFTxFC1)

There are, however, some performance concerns when using this approach. Wrapper classes take significantly more memory, so when used for many values, your program could become inefficient compared to an approach using primitives.

You may not think it's a huge deal, but for this post, I did a quick test with four trials to estimate the actual performance differences between arrays using primitive data types and wrapper types. On average, the primitive data type benchmarks completed 9x faster than the wrapper alternative all the while consuming about 25% less memory. Even at a relatively small scale, those time differences can add up.

In addition, this isn't a great programming practice, and if it feels hacky, that's because it is. Having an Object array makes your code confusing and creates an environment where you can assign any type to a variable. This is a bad programming practice and should be avoided, but it is indeed possible to store elements with different types in the same array just as long as you use an object array of wrapper classes rather than primitives.

Happy coding!

![Is storing elements with different data types in the same array possible with Java?](https://media.tenor.com/VpZ2Nf5gdRYAAAAC/pc-banging.gif)