Value types and wrappers: C# and Java

A value type is copied when it is assigned; a reference type is shared. C# lets a programmer choose with struct and class, and a record can be either. Java has primitives, which are values, and everything else is a reference; a record is a class. The task copies a small point, boxes it, and compares two boxed numbers that a cache decides about.

The task in C#

A mutable struct copied into a second variable, a readonly record struct passed to a method, a record boxed into object, and two boxed integers that are compared by reference:

Console.WriteLine("values 1.0"); var left = new MutablePoint { X = 3, Y = 4 }; var right = left; right.X = 10; Console.WriteLine($"left {left.X} right {right.X}"); Console.WriteLine($"sum {Add(new Pair(2, 3), new Pair(4, 5))}"); var pair = new Pair(2, 3); var boxedPair = (object)pair; Console.WriteLine($"boxed {boxedPair} same value {boxedPair.Equals(pair)}"); foreach (var number in new[] { 127, 128 }) { object first = number; object second = number; Console.WriteLine($"{number}: same reference {ReferenceEquals(first, second)}"); } static Pair Add(Pair left, Pair right) => new(left.X + right.X, left.Y + right.Y); struct MutablePoint { public int X; public int Y; } readonly record struct Pair(int X, int Y);

dotnet run
echo "status: $?"

values 1.0
left 3 right 10
sum Pair { X = 6, Y = 8 }
boxed Pair { X = 2, Y = 3 } same value True
127: same reference False
128: same reference False
status: 0

The same task in Java

The same point as a record, which is a reference type, a boxed record, and the same two integers boxed into Integer:

public class ValueTypes { record Pair(int x, int y) { } public static void main(String[] args) { System.out.println("values 1.0"); Pair point = new Pair(3, 4); Pair copy = point; copy = new Pair(10, point.y()); System.out.println("left " + point.x() + " right " + copy.x()); System.out.println("sum " + new Pair(2 + 4, 3 + 5)); Object boxed = point; System.out.println("boxed " + boxed + " same value " + boxed.equals(point)); for (int number : new int[] { 127, 128 }) { Integer first = number; Integer second = number; System.out.println(number + ": same reference " + (first == second)); } } }

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

values 1.0
left 3 right 10
sum Pair[x=6, y=8]
boxed Pair[x=3, y=4] same value true
127: same reference true
128: same reference false
status: 0

What differs

The next page is about text: the string, the builder and the span in C# against the string, the builder and the substring in Java.

Exercises

  1. Change the struct to a class in C# and explain the two printed values.
  2. Compare the boxed integers 1000 and 1000 in both languages.
  3. Measure the size of an array of a million ints in both and compare the heap figures with the memory page.
  4. Write a Java class with two mutable fields and copy it; explain what the second variable shares.

Questions

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

  1. What happens when a C# struct is assigned to another variable?

    Answer

    The values are copied

    The measured output shows left 3 and right 10.

  2. Why are two boxed 127 integers the same reference in Java?

    Answer

    Integer.valueOf caches the values from -128 to 127

    The measured 127 line is true and the 128 line is false.

  3. What does this program print? var left = new MutablePoint { X = 3 }; var right = left; right.X = 10; Console.WriteLine($"{left.X} {right.X}"); struct MutablePoint { public int X; }

    Answer

    3 10

    The copy changed, the original did not.

  4. What is a Java record at run time?

    Answer

    A final class with accessor methods and value equality

    The page calls it a reference type with no mutation.

  5. What does this program print? object first = 127; object second = 127; Console.WriteLine(ReferenceEquals(first, second));

    Answer

    False

    A C# box is a new object for every value.

  6. When is a C# value type boxed?

    Answer

    When it is used as object or as an interface

    The page boxes the record into object to show it.

  7. What does this program print? Console.WriteLine(new Pair(2 + 4, 3 + 5)); readonly record struct Pair(int X, int Y);

    Answer

    Pair { X = 6, Y = 8 }

    A record struct prints its members in braces.

  8. Why can List<int> store four bytes per element in C#?

    Answer

    The type argument can be a value type, so no boxing occurs

    Java cannot use a primitive as a type argument.

  9. What does this program print? Console.WriteLine(new Pair(1, 2) == new Pair(1, 2)); readonly record struct Pair(int X, int Y);

    Answer

    True

    A record struct compares its two values.

  10. What does the Java equality of two records compare?

    Answer

    Their components, with equals

    The page prints same value true for the boxed record.

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