C# and Java side by side

These pages are for a programmer who knows one of the two languages and wants to read the other. Each page takes one task and writes it twice: the C# program and the Java program are complete, both were built and run in the container described at the end of the page, and the two outputs are shown as they printed. Then the page says what differs and why, and whether the difference is a matter of taste, of the language, or of the runtime.

The C# programs use the .NET SDK 10.0.303 and the Java programs use OpenJDK 21.0.13.1 with javac 21.0.13.1, both on the same four-processor Alpine Linux container. That matters for the three pages with numbers: throughput, memory and startup. A number there was measured on this machine with the flags printed on the page, five runs each, and the minimum and the median are quoted rather than a single run.

The pages are independent. The first three are about the type system, the next three about the shapes a program is written in, then the errors and the tools, then the measurements, and the last page is the fair summary: it says what C# is better at, what Java is better at, and when each one is the wrong choice. The C# side of every page follows the C# course of this site; the Java side is written for a reader who has never compiled a Java file.

with Records and sealed hierarchies A positional record with with and equality against a Java record, and a closed set of shapes against a sealed interface. Open <T> Generics: reified and erased Two list types in C# and one in Java, and the raw list Java accepts and then throws on. Open Properties and accessors An auto-property with init and required against getters and setters, and a computed property against a method. Open LINQ and the Stream API A grouped report in both, with the same printed answer and different machinery. Open async/await and CompletableFuture Three waits that overlap, CompletableFuture.allOf, and the virtual threads of Java 21. Open null Null: nullable references and Optional Two C# warnings and a NullReferenceException against no warning and a helpful NullPointerException. Open Value types and wrappers A struct copied by value, and the boxed 127 and 128 that differ in one language. Open """ Strings and text blocks Interpolation against concatenation, StringBuilder in both, a span against substring, and a text block. Open Checked and unchecked exceptions An exception filter against a checked exception that javac refuses, and the same message in both. Open Build tools and deployment dotnet build and publish against javac, jar, source-file mode and Maven, with measured sizes and times. Open Throughput, measured The same loop five times in each, minimum and median, with the flags and the machine line. Open Memory, measured A million records in both, the heap each costs, and why one number is exact. Open Startup, measured Hello five times in both, from the built artifacts, minimum and median. Open When C# is the right choice The closing article: what each language is better at, and when Java is the right answer. Open

Start with records if you write Java and are learning C#, or with properties if you write C# and are learning Java. Read the closing article last: it needs the other pages, and it is the one that answers the question the title of the group asks.

Questions

A new set of questions in a new order every time. Choose one answer for each, then check.

  1. What is on every page of this group?

    Answer

    One task, a complete C# program and a complete Java program, both built and run

    The group compares two runs, not two syntax lists.

  2. Which versions were used here?

    Answer

    .NET SDK 10.0.303 and OpenJDK 21.0.13.1 with javac 21.0.13.1

    Both are on the note of every page and in the measurements.

  3. How are the three performance pages measured?

    Answer

    Five runs each with the flags on the page, minimum and median

    A single run on a shared machine measures the noise.

  4. Which page should a Java programmer read first?

    Answer

    Records and sealed hierarchies

    It is the type system both languages share in a modern form.

  5. What does the closing article argue?

    Answer

    What each language is better at, and when each is the wrong choice

    The page must be fair to both, as the group promises.

  6. Why is the same container used for both languages?

    Answer

    So a measured difference is the language and the runtime, not the hardware

    Both ran on the same four-processor Alpine container.

  7. What does each page show after the two programs?

    Answer

    What differs and why

    The difference is the subject; the programs are the evidence.

  8. Which of these is a page of the group?

    Answer

    Generics: reified and erased

    The group compares C# and Java only.

  9. Why does the group avoid a single benchmark number?

    Answer

    A single number on a shared machine is noise, so the pages quote five runs

    The minimum and median of five runs are on each measured page.

  10. What should a reader who knows neither language read first?

    Answer

    The C# course of this site, then this group

    The C# pages assume the language, and the group assumes the task.

The C# programs on these pages were built with the .NET SDK 10.0.303 and run on Alpine Linux 3.24.3 (x86_64, four processors); the Java programs were built and run with OpenJDK 21.0.13.1 and javac 21.0.13.1, in a Docker container.