DEV Community

Mackenly Jones
Mackenly Jones

Posted on • Originally published at crtv.dev on

Is storing elements with different data types in the same array possible with Java?

Is storing elements with different data types in the same array possible with Java?

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?

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?

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?

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?

Top comments (0)