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.
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
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.
-
What does each page show after the two programs?
Answer
What differs and why
The difference is the subject; the programs are the evidence.
-
Which of these is a page of the group?
Answer
Generics: reified and erased
The group compares C# and Java only.
-
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.
-
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.