What Value Does Every Cell Contain in a New Integer Array in Java?

What Value Does Every Cell Contain in a New Integer Array in Java? Featured Image

Today, I wanted to get into a topic that involves one of the weirder behaviors of Java: implicit variable initialization for primitive types. In other words, if I create a collection of integers, what might you expect their initial value to be?

Table of Contents

Multiple Choice Question

If you create an integer array, what value does each cell contain?

int[] a = new int[10];

Select one of the following:

  • A. Random
  • B. 0
  • C. 1
  • D. 2,147,483,647


While you’re thinking about the answer (and so you don’t accidentally see the answer which is below), let’s talk briefly about how you can help The Renegade Coder grow.

The Renegade Coder, as it currently stands, is a solo operation. I’ve been at this since late 2016, and I’d love to make this style of content my full-time job. If you see value in the work I’ve put into these articles, you can show your support by subscribing to my PatreonOpens in a new tab.. Other ways to support the site including signing up for the mailing list and joining the DiscordOpens in a new tab..

In addition to writing, I dabble a bit in YouTube. Here’s my most popular video:

With that out of the way, let’s try to solve the problem.

Problem Solution

Similar to the previous problem, the solution to this problem requires a bit of underlying knowledge. Specifically, all primitive types have some default value in contexts where they’re not set explicitly—such as in arrays and fields. For most primitive types, that value is 0 (i.e., B). For all reference types, that value is null.

Don’t believe me? Give it a try! Here’s a quick script you can throw at an online compiler:

import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        int[] a = new int[10];

When I ran this, I get the following message:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

In the next section, we’ll look at some ways you could have reasoned this out.

Alternative Problem Solving Techniques

One way I like to approach multiple choice questions is by assuming that all possible answers were intentioned. In other words, all of the wrong answers were designed to get at some misconception. If you can reason about what that misconception might be, then you can figure out the right answer.

Starting with A, we have the option “Random.” The thought with this answer is likely that arrays come directly from memory. Therefore, there must be some data already there from previous use. While that may be true to some extent (e.g., I believe C leaves the values in their arrays “undefined”), it’s not true in Java. After all, Java was designed to be safer than its predecessors, so it’s unlikely that the language would let you pull random junk from memory.

Moving on to C, we have the option “1”. This answer is mostly nonsensical since even indices are zero-based in most languages. That said, it’s likely that this answer exists to trick folks into the common “off-by-one” error. As a result, I wouldn’t trust it.

Finishing with D, we have the option “2,147,483,647”. If you’re stumped by this very specific number, it’s the upper bound for integers. Again, this number is somewhat nonsensical, though I would probably buy it over 1 for the reason that initializing an array to max or min values makes some sense.

That said, the only sure fire way to get this answer right is to have this information memorized. Shame on folks who think this is information worth memorizing (i.e., folks who make exams for students), but it is nice to know if you find yourself in the habit of declaring arrays like this.

Happy Learning

Once again, thanks for taking the time to brush up on your programming knowledge. Hopefully, this was a fun one for you! I had fun writing it up.

With that said, if you like this and want to see more like it, here are some related articles:

Otherwise, take care! See you next time.

A Computer Science Problem a Day (3 Articles)—Series Navigation

As an educator, I tend to dislike examination as a form of summative assessment. However, I do like multiple choice questions as a form of formative assessment—especially in the context of peer instruction. If done correctly, multiple choice questions are great way of testing concepts and exposing misconceptions. As a result, I thought it would be fun to put together a little series covering questions I’ve seen or used as a way of teaching computer science concepts. Enjoy!

Jeremy Grifski

Jeremy grew up in a small town where he enjoyed playing soccer and video games, practicing taekwondo, and trading Pokémon cards. Once out of the nest, he pursued a Bachelors in Computer Engineering with a minor in Game Design. After college, he spent about two years writing software for a major engineering company. Then, he earned a master's in Computer Science and Engineering. Today, he pursues a PhD in Engineering Education in order to ultimately land a teaching gig. In his spare time, Jeremy enjoys spending time with his wife, playing Overwatch and Phantasy Star Online 2, practicing trombone, watching Penguins hockey, and traveling the world.

Recent Code Posts