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
- Intermission
- Problem Solution
- Alternative Problem Solving Techniques
- Happy Learning
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
Intermission
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 Patreon. Other ways to support the site including signing up for the mailing list and joining the Discord.
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]; System.out.println(Arrays.toString(a)); } }
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:
- Be Careful with String’s Substring Method in Java
- The Difference Between Private and Public in Java
- The Behavior of i=i++ in Java
Otherwise, take care! See you next time.
Recent Code Posts
While creating some of the other early articles in this series, I had a realization: something even more fundamental than loops and if statements is the condition. As a result, I figured we could...
Today, we're expanding our concept map with the concept of loops in Python! Unless you're a complete beginner, you probably know a thing or two about loops, but maybe I can teach you something new.