Skip to main content

Documentation Index

Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt

Use this file to discover all available pages before exploring further.

A data object in Kotlin is a singleton declaration that instructs the compiler to automatically generate toString(), equals(), and hashCode() functions based on the object’s name and type. Introduced in Kotlin 1.9.0, it provides the structural symmetry of a data class while maintaining the single-instance guarantee of a standard object.
data object ConfigurationState

Compiler-Generated Functions

When you declare a data object, the Kotlin compiler synthesizes three specific member functions:
  1. toString(): Returns the exact literal name of the object as a String.
  2. equals(other: Any?): Returns true if the other reference is an instance of the exact same data object type.
  3. hashCode(): Returns a consistent, pre-calculated integer hash specific to the singleton’s type, ensuring stable behavior in hash-based collections.

Behavioral Differences from Standard Objects

A standard object relies on the default Any implementation for its string representation, which appends the object’s memory address hash. A data object overrides this to return its clean identifier.
object StandardSingleton
data object DataSingleton

fun main() {
    println(StandardSingleton) // Output: StandardSingleton@5a07e868
    println(DataSingleton)     // Output: DataSingleton
}

Omitted Data Class Functions

Unlike a data class, a data object does not generate the following functions:
  • copy(): Omitted because singletons inherently forbid the creation of cloned or mutated instances. The type system enforces that only one instance of the object exists in the JVM.
  • componentN(): Omitted because a data object lacks a primary constructor and therefore has no properties to destructure.

Inheritance Rules

A data object adheres to the exact same inheritance rules as a standard object. It can extend open or abstract classes and implement interfaces.
interface State
abstract class BaseEvent

data object Idle : State
data object Initialized : BaseEvent()

Equality Semantics

Because a data object is a singleton, referential equality (===) and structural equality (==) will always yield the same result when comparing it against itself. However, the generated equals() method ensures that if the data object is serialized and deserialized (or instantiated via reflection in edge cases), structural equality remains intact based on the type rather than the memory reference.
Master Kotlin with Deep Grasping Methodology!Learn More