When you have 200 concurrent users hitting a single-threaded service, you’ll see java.lang.OutOfMemoryError: Java heap space errors due to the inefficient use of objects. This is where value classes and value objects come into play, allowing for more efficient memory management.
## PREREQUISITES To follow this tutorial, you’ll need: * Java 25 or later * Spring Boot 3.0 or later * Maven or Gradle * Project Valhalla dependencies:
<dependency> <groupId>org.projectvalhalla</groupId> <artifactId>valhalla</artifactId> <version>1.0</version> </dependency>
## UNDERSTANDING VALUE CLASSES AND OBJECTS Value classes and objects are a new feature in Java 25, introduced by Project Valhalla. They allow for more efficient memory management by reducing the overhead of object creation and garbage collection.
+---------------+ | Value Class | +---------------+ | | v +---------------+ | Value Object | +---------------+ | | v +---------------+ | Heap Memory | +---------------+
The following table compares the characteristics of value classes and objects with traditional classes and objects:
| Characteristic | Value Classes/Objects | Traditional Classes/Objects |
|---|---|---|
| Memory Allocation | Stack-based | Heap-based |
| Garbage Collection | Not applicable | Applicable |
| Object Creation Overhead | Lower | Higher |
## STEP-BY-STEP IMPLEMENTATION ### Step 1: Create a Value Class To create a value class, you need to use the value keyword in the class declaration.
public value class Person { private final String name; private final int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
This step is important because it allows you to define a class that can be used to create value objects. ### Step 2: Create a Value Object To create a value object, you need to instantiate the value class.
Person person = new Person("John Doe", 30);
This step is important because it allows you to create an instance of the value class. ### Step 3: Use the Value Object You can use the value object like any other object.
System.out.println(person.getName()); // Output: John Doe System.out.println(person.getAge()); // Output: 30
This step is important because it allows you to access the properties of the value object. ## COMPLETE WORKING EXAMPLE The following is a complete working example of a value class and object:
package com.example.valhalla; public value class Person { private final String name; private final int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } } public class Main { public static void main(String[] args) { Person person = new Person("John Doe", 30); System.out.println(person.getName()); // Output: John Doe System.out.println(person.getAge()); // Output: 30 } }
You can run this example using the following command:
java --enable-preview Main.java
This will output:
John Doe 30
## COMMON MISTAKES AND HOW TO FIX THEM ### Mistake 1: Not Using the Value Keyword If you forget to use the value keyword in the class declaration, you’ll get a compilation error.
// WRONG - causes compilation error public class Person { private final String name; private final int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
To fix this, add the value keyword to the class declaration.
public value class Person { private final String name; private final int age; public Person(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public int getAge() { return age; } }
## PERFORMANCE AND PRODUCTION TIPS
Production tip: Use the
--enable-previewflag when running your Java application to enable the use of value classes and objects.
Production tip: Use the
java.lang.invoke.MethodHandleclass to invoke methods on value objects, as it provides better performance than traditional method invocation.
For more information on Java Algorithms, check out our tutorial on the subject. ## TESTING To test the value class and object, you can use the following JUnit 5 test:
import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; public class PersonTest { @Test public void testPerson() { Person person = new Person("John Doe", 30); assertEquals("John Doe", person.getName()); assertEquals(30, person.getAge()); } }
This test checks that the Person class is correctly instantiated and that its methods return the expected values. ## KEY TAKEAWAYS * Value classes and objects are a new feature in Java 25, introduced by Project Valhalla. * They allow for more efficient memory management by reducing the overhead of object creation and garbage collection. * To create a value class, use the value keyword in the class declaration. * To create a value object, instantiate the value class. * Use the --enable-preview flag when running your Java application to enable the use of value classes and objects. * For more information on Java, check out our Java Tutorials Hub.
java-examples — Clone, Star & Contribute

Leave a Reply