Following consistent coding standards makes the OpenJDK codebase easier to read, maintain, and contribute to. This guide covers the coding conventions for HotSpot (C++) and general Java development.
This guide is based on the official HotSpot Coding Style document, which is the authoritative source for HotSpot development.
// Include guards#ifndef SHARE_VM_RUNTIME_THREAD_HPP#define SHARE_VM_RUNTIME_THREAD_HPP// HotSpot includes (alphabetically sorted)#include "memory/allocation.hpp"#include "runtime/handles.hpp"#include "utilities/globalDefinitions.hpp"// System includes (separate section)#include <vector>class Thread : public ThreadShadow { // Class definition};#endif // SHARE_VM_RUNTIME_THREAD_HPP
All source files must have globally unique basenames
Keep include lines alphabetically sorted
Separate HotSpot and system includes with a blank line
// Opening brace on same lineif (condition) { do_something();} else { do_something_else();}// Functions: brace can be on same or next linevoid function() { // implementation}// Or for emphasis:void important_function(){ // implementation}// Always use braces, even for single statementsif (test) { single_statement();}// Exception: very simple one-liners can omit bracesif (ptr == nullptr) return;
Indentation: 2 spaces (no tabs)
No tabs: Set your editor to use spaces
Always use braces for substatements (with rare exceptions)
// Spaces around operatorsint result = a + b * c;if (x == y) { ... }// Space after control flow keywordswhile (condition) { ... }for (int i = 0; i < n; i++) { ... }if (test) { ... }// No space around control expressionswhile (test) { ... } // Correctwhile ( test ) { ... } // Wrong// Parentheses for clarityif ((a & b) != 0) { ... }int x = (a << 2) | (b & 0xFF);
// Use auto for clarity, not just convenienceauto it = map.find(key); // Clear: iterator type obviousauto lock = MutexLocker(mutex); // Clear: RAII pattern// Avoid when type is important to readabilityint count = get_count(); // Better than: auto count = get_count();
// Use HotSpot allocation, not global new/deleteThread* thread = new Thread(); // Wrong!// Use ResourceMark for arena allocationResourceMark rm;char* buffer = NEW_RESOURCE_ARRAY(char, size);// C-heap allocation with NMT supportchar* data = NEW_C_HEAP_ARRAY(char, size, mtInternal);FREE_C_HEAP_ARRAY(char, data);// RAII for automatic cleanupResourceMark rm;// Allocations automatically freed when rm goes out of scope
// Use assert for preconditions and invariantsassert(ptr != nullptr, "pointer must not be null");assert(index >= 0 && index < length, "index out of bounds");// Use guarantee for critical checksguarantee(heap != nullptr, "heap initialization failed");// Use ShouldNotReachHere for impossible casesswitch (type) { case TypeA: handle_a(); break; case TypeB: handle_b(); break; default: ShouldNotReachHere();}// Use HotSpot exception mechanism (not C++ exceptions)THREAD;Handle h = ...;if (HAS_PENDING_EXCEPTION) { // Handle error return;}
/** * Returns a hash code value for this object. * <p> * The general contract of {@code hashCode} is: * <ul> * <li>Consistency: multiple invocations return the same value * <li>Equality: equal objects must have equal hash codes * </ul> * * @return a hash code value for this object * @see #equals(Object) */@Overridepublic int hashCode() { return Objects.hash(field1, field2);}
When in doubt, look at existing code in the same area and follow those patterns. Consistency within a component is more important than strict adherence to any single style guide.