Skip to content

About

Example code for the HappyCoders articles on arrays in Java – declaring, initializing, filling, and copying arrays, array length, memory layout measured with JOL, the maximum array size, and 2D arrays, plus a JMH benchmark of row-wise vs. column-wise traversal

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

12 Commits

Folders and files

Repository files navigation

Java Arrays

Build Maintainability Rating Reliability Rating Security Rating

The code of the HappyCoders.eu articles on arrays in Java – one package per article. Every class with a main() method is one chapter of its article and prints the outputs shown there.

Articles

Article Package
How to Initialize Arrays in Java (English, German) eu.happycoders.initialize
Array Length in Java (English, German) eu.happycoders.length
2D Arrays in Java (English, German) eu.happycoders.twodimensional

Running the examples

The examples use Java 25 language features and need Java 25 or newer. Build once with mvn compile, then run a class from the repository root:

java -cp target/classes eu.happycoders.<package>.<ClassName>

How to Initialize Arrays in Java – eu.happycoders.initialize

  • Ch1Declare – declaring arrays, initializing them with values (with new, with var, without new, C-style), separately from the declaration, with a length, and the default values of all element types
  • Ch2Fill – filling an array element by element, with Arrays.fill() (whole array and ranges), Arrays.setAll(), and Arrays.parallelSetAll()
  • Ch3Copy – clone() for primitive and object arrays, System.arraycopy(), Arrays.copyOf() (same length, shorter, longer), and Arrays.copyOfRange()
  • Ch4Convert – arrays from a List and a Set with the four toArray() variants, from an object stream, and from primitive streams

The outputs on JDK 27 are in results/initialize/outputs-jdk27.txt.

Array Length in Java – eu.happycoders.length

  • Ch1GetLength – length, Array.getLength(), height and width of a 2D array, and IntSummaryStatistics of the row lengths, printed with an English and a German default locale
  • Ch2SetLength – the length is set when the array is created, with values or with a size
  • Ch3MemoryLayout – the layout and size of the article's arrays measured with JOL (Java Object Layout): int[5], short[5], byte[5], long[3], long[5], String[4], and the footprint of int[2][3]
  • Ch4MaxArraySize – byte and long arrays with Integer.MAX_VALUE, Integer.MAX_VALUE - 1, and Integer.MAX_VALUE - 2 elements; needs a heap of 20 GB

JOL needs the JAR with its dependencies, and without the following options it prints warnings about attaching to the JVM:

mvn package
java -Djdk.attach.allowAttachSelf -XX:+EnableDynamicAgentLoading \
     -Djol.skipHotspotSAAttach=true --sun-misc-unsafe-memory-access=allow \
     -cp target/benchmarks.jar eu.happycoders.length.Ch3MemoryLayout

Add -XX:-UseCompactObjectHeaders for the layout up to Java 26. The maximum array size:

java -Xmx20g -cp target/classes eu.happycoders.length.Ch4MaxArraySize

The outputs on JDK 27 are in results/length/outputs-jdk27.txt.

2D Arrays in Java – eu.happycoders.twodimensional

  • Ch1Structure – a 2D array is an array of arrays: each row is an int[] of its own
  • Ch2Declare – the brackets at the type, C-style at the name, or mixed
  • Ch3Initialize – with values, with default values (new int[2][3]), and row by row (new int[2][])
  • Ch4Access – reading and writing elements, a row as an array, and the ArrayIndexOutOfBoundsException for a wrong row or column index
  • Ch5Length – the number of rows and columns, the empty array, and a row 0 that is null
  • Ch6Iterate – nested for loops, for-each loops, and a stream with flatMapToInt()
  • Ch7Print – println(), Arrays.toString() and Arrays.deepToString(), one line per row, and aligned columns with printf()
  • Ch8Fill – Arrays.setAll() and Arrays.fill() per row, and the pitfall of Arrays.fill() with one row object for all rows
  • Ch9Jagged – rows of different lengths, and Pascal's triangle as a jagged array
  • Ch10Copy – clone() copies only the outer array; a deep copy with a loop and with a stream; Arrays.equals() vs. Arrays.deepEquals()
  • Ch11Sort – every row for itself, and the rows by one of their columns with a Comparator
  • Ch12Multidimensional – a 3D array, and Array.newInstance() for dimensions known only at runtime
  • Ch13Flat – a 3 x 4 matrix in a flat 1D array, element [row][col] at index row * columns + col

The outputs of all chapters on JDK 27 are in results/twodimensional/outputs-jdk27.txt.

Benchmarks

Each article's JMH benchmarks are in the benchmark subpackage of its package:

  • TraversalBenchmark – sums all elements of a square int matrix with 64, 1,024 and 4,096 rows and columns, stored as a 2D array and as a flat 1D array, each in row-major order (the inner loop walks along a row) and in column-major order (the inner loop walks down a column)

mvn package builds target/benchmarks.jar with all of them. Run the benchmarks of one article with its package as filter:

mvn package
java -jar target/benchmarks.jar eu.happycoders.twodimensional.benchmark

The traversal benchmark takes about four minutes. The results of the articles' runs are in results/<package>/, e.g. results/twodimensional/apple-m5-pro/. The file traversal-jdk27-size-4100.log there is a check run with 4,100 instead of 4,096 columns: at 4,096 columns the elements of one column of the flat array are exactly 16 KB apart and evict each other from the same cache sets, which makes the column-major flat traversal slower than the 2D one; at 4,100 columns it is faster.


Additional Resources


Java Versions PDF Cheat Sheet

Stay up-to-date with the latest Java features with this free PDF Cheat Sheet!

Java Versions PDF Cheat Sheet Mockup

  • Avoid lengthy research with this concise overview of all Java versions from Java 10 to Java 27.
  • Discover the innovative features of each new Java version, summarized on a single page.
  • Impress your team with your up-to-date knowledge of the latest Java version.

👉 Download the free Java Versions PDF

(Hier geht's zur deutschen Version → Java-Versionen PDF)


The Big O Cheat Sheet

With this free 1-page PDF cheat sheet, you'll always have the 7 most important complexity classes at a glance.

Big O PDF Cheat Sheet Mockup

  • Always choose the most efficient data structures and thus increase the performance of your applications.
  • Be prepared for technical interviews and confidently present your algorithm knowledge.
  • Become a sought-after problem solver and be known for systematically tackling complex problems.

👉 Download the free Big O Cheat Sheet

(Hier geht's zur deutschen Version → O-Notation Cheat Sheet)


HappyCoders Newsletter

👉 Want to stay on top of modern Java? Sign up for the HappyCoders newsletter – Modern Java: new versions & features, performance, and JVM insights – once a month.

(Hier geht's zur deutschen Version → HappyCoders-Newsletter deutsch)


🇩🇪 An alle Java-Entwickler:innen, die durch fundierte Kenntnisse über Datenstrukturen besseren Code schreiben wollen

Trage dich jetzt unverbindlich auf die Warteliste von „Mastering Data Structures in Java“ ein, und erhalte das beste Angebot!

Mastering Data Structures Mockup

👉 Zur Warteliste

About

Example code for the HappyCoders articles on arrays in Java – declaring, initializing, filling, and copying arrays, array length, memory layout measured with JOL, the maximum array size, and 2D arrays, plus a JMH benchmark of row-wise vs. column-wise traversal

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages