Records and sealed hierarchies: C# and Java

Both languages have a record now, and both mean the same thing by it: a small class whose whole state is its constructor, with value equality, a generated ToString, and an immutable shape. C# has had records since 2020 and Java since 2021, and the two are close enough that the same task is written the same way in each. The differences are in the details, and the details are what this page shows.

The task in C#

A point with two coordinates, copied with one of them changed, compared with an equal point, and used in a closed hierarchy of shapes that a switch expression turns into text. The hierarchy is abstract record Shape with two sealed records under it:

Console.WriteLine("records 1.0"); var origin = new Point(0, 0); var moved = origin with { X = 3 }; Console.WriteLine(origin); Console.WriteLine(moved); Console.WriteLine($"equal {origin == new Point(0, 0)}"); Console.WriteLine($"same hash {origin.GetHashCode() == new Point(0, 0).GetHashCode()}"); var shapes = new Shape[] { new Circle(2), new Square(3), new Circle(0) }; foreach (var shape in shapes) { Console.WriteLine(Describe(shape)); } static string Describe(Shape shape) => shape switch { Circle { Radius: 0 } => "a point", Circle circle => $"a circle of radius {circle.Radius}", Square square => $"a square of side {square.Side}", _ => "unknown", }; record Point(int X, int Y); abstract record Shape; sealed record Circle(double Radius) : Shape; sealed record Square(double Side) : Shape;

dotnet run
echo "status: $?"

records 1.0
Point { X = 0, Y = 0 }
Point { X = 3, Y = 0 }
equal True
same hash True
a circle of radius 2
a square of side 3
a point
status: 0

The same task in Java

Java has a record with the same positional syntax and a sealed interface with a permits clause. The copy is a new record because there is no with, and the switch that turns a shape into text uses a record pattern, which is a Java 21 feature:

import java.util.List; public class Records { public static void main(String[] args) { System.out.println("records 1.0"); Point origin = new Point(0, 0); Point moved = new Point(3, origin.y()); System.out.println(origin); System.out.println(moved); System.out.println("equal " + origin.equals(new Point(0, 0))); System.out.println("same hash " + (origin.hashCode() == new Point(0, 0).hashCode())); List<Shape> shapes = List.of(new Circle(2), new Square(3), new Circle(0)); for (Shape shape : shapes) { System.out.println(describe(shape)); } } static String describe(Shape shape) { return switch (shape) { case Circle(double radius) when radius == 0 -> "a point"; case Circle(double radius) -> "a circle of radius " + radius; case Square(double side) -> "a square of side " + side; }; } record Point(int x, int y) { } sealed interface Shape permits Circle, Square { } record Circle(double radius) implements Shape { } record Square(double side) implements Shape { } }

javac -g -d out Records.java
java -cp out Records
echo "status: $?"

records 1.0
Point[x=0, y=0]
Point[x=3, y=0]
equal true
same hash true
a circle of radius 2.0
a square of side 3.0
a point
status: 0

What differs

The next page is about the type parameter: C# keeps the type argument at run time and Java erases it, which changes what a program can ask about its own collections.

Exercises

  1. Add a third shape to both hierarchies and check that both compilers demand the new arm.
  2. Give the point a computed length in both and compare the two ToString outputs.
  3. Write the Java copy of a point with a static factory method and compare it with the C# with.
  4. Make the C# shape hierarchy a sealed interface instead of an abstract record and see what changes in the switch.

Questions

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

  1. What does a record give a C# or Java class?

    Answer

    Value equality, a generated ToString and an immutable shape

    Both languages generate the members from the component list.

  2. Which expression copies a record with one member changed in C#?

    Answer

    with

    The measured C# line is origin with { X = 3 }.

  3. What does this program print? Console.WriteLine(new Point(1, 2) == new Point(1, 2)); record Point(int X, int Y);

    Answer

    True

    A record compares its members, which the page shows with equal True.

  4. How does Java restrict the types of a sealed interface?

    Answer

    With permits and a record for each permitted type

    The page writes sealed interface Shape permits Circle, Square.

  5. What does this program print? Console.WriteLine(new { X = 0, Y = 0 });

    Answer

    { X = 0, Y = 0 }

    An anonymous type prints its members without a type name.

  6. Why does the C# switch expression in the page have a default arm?

    Answer

    The C# compiler does not require exhaustiveness, so the arm is written by hand

    The page names this difference in the list.

  7. What does this program print? var point = (X: 1, Y: 2) with { X = 5 }; Console.WriteLine(point);

    Answer

    (5, 2)

    A tuple supports with in C#, which copies it with one member changed.

  8. What does a Java record print for Point(0, 0)?

    Answer

    Point[x=0, y=0]

    The measured Java output shows that form.

  9. Which language allows a sealed type without an interface?

    Answer

    C#, with the sealed modifier

    The page names this as a difference between the hierarchies.

  10. What does this program print? var a = new Point(1, 2); var b = a with { X = 1 }; Console.WriteLine(a == b); record Point(int X, int Y);

    Answer

    True

    The copy has the same two values, so the records are equal.

The C# program was built with the .NET SDK 10.0.303 and the Java program with OpenJDK 21.0.13.1 (javac 21.0.13.1), both run on Alpine Linux 3.24.3 (x86_64, four processors) in a Docker container.

Article author: Arthur Isaev