Eleven crazy learnings from the Java 11 certification: a few freaky array declarations (1/11)
In the summer of 2021, I got my Java 11 certification. I expected it to be quite a breeze, because I’d been a Java developer for 14 years and surely I should have seen it all by now, right? Turned out I was very wrong. I came across lots of things that I didn’t even know were possible with Java. In this weekly blog series I will go through 11 of these ‘crazy learnings’ that surprised me the most, even as an experienced developer. We start off with a few freaky array declarations.
Replacing array declarations by ‘var’
I really like local-varable type inference, so when the opportunity arises to replace an explicit local-variable type by var
I feel really happy.
When you do this for arrays, however, there is a small caveat.
You see, when I came across this line of code…
int[] elements = new int[] { 0, 1, 2, 3, 5, 8, 13, 21 };
…I just replaced int
by var
without thinking and thought I was done.
var[] elements = new int[] { 0, 1, 2, 3, 5, 8, 13, 21 }; // doesn't compile!
Because we’re dealing with an int array here, the square brackets are part of the data type, and so var
needs to replace them also. Which will result in:
var elements = new int[] { 0, 1, 2, 3, 5, 8, 13, 21 }; // much better
It’s not a very complicated thing to remember, but it’ll surely save you some time if you do.
C-style arrays and multiple declarations on a single line
Java supports two styles of array declaration.
int[] dailyHighscores; // style 1
int allTimeHighscores[]; // style 2
The second style is called ‘C-style’ array declaration and it was added to help C programmers get used to Java. Kinda hard to imagine in a world where 18 Java versions have been released to this day, but the story seems to check out. Defining an array in C indeed means putting the brackets after the variable name, not before.
The style of array declaration used isn’t really relevant as long as we exclusively write single declarations on a single line of code only. Things get a little hairier when we start declaring multiple arrays on a single line of code.
int dailyHighscores[], allTimeHighscores; // second array is an int!
In this example, dailyHighscores
is an int array, but allTimeHighscores
is an int primitive!
If you want both variables to be int arrays in a single declaration, you’d have to stick to the conventional style of array declaration:
int[] dailyHighscores, allTimeHighscores; // both variables are arrays now
To drive the point home, the following declaration…
int[] dailyHighscores, allTimeHighscores, highscoreMatrix[];
…would be equivalent to:
int[] dailyHighscores;
int[] allTimeHighscores;
int[][] highscoreMatrix;
So there you have it: a few freaky ways to declare arrays. Next week we’ll take a look at stream elements!
Image from PxHere
Other blog posts in this series
Did you miss a blog post in this series? Here’s a list of all posts that have been published so far:
- A few freaky array declarations
- Stream elements should implement Comparable
- Accessing static interface methods
- Anonymous subclasses in enums
- Division by zero
- Method overloading priorities
- The crazy stuff that is allowed in switch statements
- Equality in cloned arrays
- Wrapper objects: some are more equal than others
- Functional interfaces actually CAN contain multiple abstract methods
- Passing arguments to method references