---
title: Migrate from Java SDK to Kotlin SDK
---
import Tabs from '@theme/Tabs';
import TabItem from '@theme/TabItem';
import CodeBlock from '@theme/CodeBlock';
import ThreadingKotlinSDK from '!!raw-loader!./../../generated/kotlin/MigrateFromJavaToKotlinSDKTest.codeblock.threading.kt';
import ThreadingJavaSDKKotlin from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.threading.kt';
import ThreadingJavaSDKJava from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.threading.java';
import OpenARealmKotlinSDK from '!!raw-loader!./../../generated/kotlin/MigrateFromJavaToKotlinSDKTest.codeblock.open-a-realm.kt';
import OpenARealmJavaSDKKotlin from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.open-a-realm.kt';
import OpenARealmJavaSDKJava from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.open-a-realm.java';
import OpenARealmAdvancedKotlinSDK from '!!raw-loader!./../../generated/kotlin/MigrateFromJavaToKotlinSDKTest.codeblock.open-a-realm-advanced.kt';
import ROMKotlinSDK from '!!raw-loader!./../../generated/kotlin/MigrateFromJavaToKotlinSDKTest.codeblock.realm-object-model.kt';
import ROMJavaSDKKotlin from '!!raw-loader!./../../generated/java/Sample.codeblock.realm-object-model.kt';
import ROMJavaSDKJava from '!!raw-loader!./../../generated/java/SampleJava.codeblock.realm-object-model.java';
import OneToOneKotlinSDK from '!!raw-loader!./../../generated/kotlin/MigrateFromJavaToKotlinSDKTest.codeblock.one-to-one-relationship.kt';
import OneToOneJavaSDKKotlin from '!!raw-loader!./../../generated/java/Child.codeblock.one-to-one-relationship.kt';
import OneToOneJavaSDKJava from '!!raw-loader!./../../generated/java/ChildJava.codeblock.one-to-one-relationship.java';
import OneToManyKotlinSDK from '!!raw-loader!./../../generated/kotlin/MigrateFromJavaToKotlinSDKTest.codeblock.one-to-many-relationship.kt';
import OneToManyJavaSDKKotlin from '!!raw-loader!./../../generated/java/Kid.codeblock.one-to-many-relationship.kt';
import OneToManyJavaSDKJava from '!!raw-loader!./../../generated/java/KidJava.codeblock.one-to-many-relationship.java';
import SchemaTypesKotlinSDK from '!!raw-loader!./../../generated/kotlin/MigrateFromJavaToKotlinSDKTest.codeblock.schema-types.kt';
import SchemaTypesJavaSDKKotlin from '!!raw-loader!./../../generated/java/CollegeStudent.codeblock.schema-types.kt';
import SchemaTypesJavaSDKJava from '!!raw-loader!./../../generated/java/CollegeStudentJava.codeblock.schema-types.java';
import WritesAsyncKotlinSDK from '!!raw-loader!./../../generated/kotlin/MigrateFromJavaToKotlinSDKTest.codeblock.write-async.kt';
import WritesAsyncJavaSDKKotlin from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.write-async.kt';
import WritesAsyncJavaSDKJava from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.write-async.java';
import WritesSyncKotlinSDK from '!!raw-loader!./../../generated/kotlin/MigrateFromJavaToKotlinSDKTest.codeblock.write-sync.kt';
import WritesSyncJavaSDKKotlin from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.write-sync.kt';
import WritesSyncJavaSDKJava from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.write-sync.java';
import QueryFiltersKotlinSDK from '!!raw-loader!./../../generated/kotlin/MigrateFromJavaToKotlinSDKTest.codeblock.query-filters.kt';
import QueryFiltersJavaSDKKotlin from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.query-filters.kt';
import QueryFiltersJavaSDKJava from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.query-filters.java';
import SDLKotlinSDK from '!!raw-loader!./../../generated/kotlin/MigrateFromJavaToKotlinSDKTest.codeblock.query-sort-limit-distinct.kt';
import SDLJavaSDKKotlin from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.query-sort-limit-distinct.kt';
import SDLJavaSDKJava from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.query-sort-limit-distinct.java';
import DeletesKotlinSDK from '!!raw-loader!./../../generated/kotlin/MigrateFromJavaToKotlinSDKTest.codeblock.deletes.kt';
import DeletesJavaSDKKotlin from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.deletes.kt';
import DeletesJavaSDKJava from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.deletes.java';
import NotificationsKotlinSDK from '!!raw-loader!./../../generated/kotlin/MigrateFromJavaToKotlinSDKTest.codeblock.notifications.kt';
import NotificationsJavaSDKKotlin from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.notifications.kt';
import NotificationsJavaSDKJava from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.notifications.java';
import MigrationsKotlinSDK from '!!raw-loader!./../../generated/kotlin/MigrateFromJavaToKotlinSDKTest.codeblock.migrations.kt';
import MigrationsJavaSDKKotlin from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.migrations.kt';
import MigrationsJavaSDKJava from '!!raw-loader!./../../generated/java/MigrateFromJavaToKotlinSDKTest.codeblock.migrations.java';
:::info What is the Kotlin SDK?
The Kotlin SDK is a new Realm client SDK built entirely with the
Kotlin programming language. The Kotlin SDK uses an entirely
different codebase from the Java SDK. It is designed specifically
to take advantage of Kotlin language features such as coroutines and
suspend functions. The Java SDK also supports some of these features,
as well as Android applications written in Kotlin. But the
[Kotlin SDK](./intro) is more Kotlin-idiomatic than the
Java SDK.
:::
## Overview
The Java SDK and the Kotlin SDK differ in many ways. On this page, you'll
find a high-level comparison of most of the ways the SDKs differ.
## Kotlin SDK Architecture
The Java SDK provided live objects, queries, and realms that
automatically update when underlying data changes. The Kotlin SDK still
provides this live interface in write transactions, but otherwise relies
on a new frozen architecture that makes Realm objects easier to work
with. Here are some of the main differences between the Java SDK
architecture and the Kotlin SDK architecture:
- **Frozen by default**: All objects are now frozen. Unlike live objects,
frozen objects do not automatically update after database writes. You
can still access live objects within a write transaction, but passing
a live object out of a write transaction freezes the object.
- **Thread-safety**: All realm instances, objects, query results, and
collections can now be transferred across threads.
- **Singleton**: You now only need one instance of each realm.
[No need to open and close realms on individual threads.](/sdk/kotlin/realm-database/frozen-architecture)
:::tip See Also
[Kotlin SDK Frozen Architecture](/sdk/kotlin/realm-database/frozen-architecture)
:::
## Opening Realms
The Java SDK automatically detects Realm Object Models defined in your
application, and uses all of them in the schema of opened realms unless
you specify otherwise. The Kotlin SDK requires you to manually specify
the Realm Object Models to use in your realm schema. Additionally:
- The Kotlin SDK does not provide the ability to set and access a
default realm in your application. Since you can now share realms,
objects, and results across threads, you can rely on a global singleton
instead.
- The Java SDK used `RealmConfiguration.Builder().build()` to
generate instances of `RealmConfiguration`. With the Kotlin SDK,
use the [`RealmConfiguration.with()`](https://docs.mongodb.com/realm-sdks/kotlin/latest/library-base/-realm%20-kotlin%20-multiplatform%20-s-d-k/io.realm/-realm-configuration/-companion/with.html)
companion method `RealmConfiguration` instead.
- The Java SDK used the static `Realm.getInstance()` method to
open a realm with a given config. With the Kotlin SDK, use the static
`Realm.open()` method instead.
|
{OpenARealmJavaSDKKotlin}
{OpenARealmJavaSDKJava}
|
{OpenARealmKotlinSDK}
|
Optionally, use [`RealmConfiguration.Builder`](https://docs.mongodb.com/realm-sdks/kotlin/latest/library-base/-realm%20-kotlin%20-multiplatform%20-s-d-k/io.realm/-realm-configuration/-builder/index.html)
to customize your configuration even more:
{OpenARealmAdvancedKotlinSDK}
:::tip See Also
[Open & Close a Realm (Kotlin SDK)](/sdk/kotlin/realm-database/open-and-close-a-realm)
:::
## Realm Object Models
In the Java SDK, you declare Realm object models in one of two ways:
- extending `RealmObject`
- implementing `RealmModel`
The Kotlin SDK uses default methods in the `RealmObject` interface
instead. With the Kotlin SDK, inherit from `RealmObject` to
declare a Realm object model. Annotations work the same way they did
in java for fields with special properties, such as ignored fields,
primary keys, and indexes.
|
{ROMJavaSDKKotlin}
{ROMJavaSDKJava}
|
{ROMKotlinSDK}
|
:::tip See Also
[Supported Schema Types (Kotlin SDK)](/sdk/kotlin/realm-database/schemas/supported-types)
:::
## Relationships
Both the Java and Kotlin SDKs declare relationships through Realm object
fields:
### One-to-One
|
{OneToOneJavaSDKKotlin}
{OneToOneJavaSDKJava}
|
{OneToOneKotlinSDK}
|
### One-to-Many
With the Java SDK, you could define one-to-many relationships with fields
of type `RealmList`. The Kotlin SDK still uses fields of
type `RealmList`, but you should instantiate `RealmList`
instances with the [`realmListOf()`](https://docs.mongodb.com/realm-sdks/kotlin/latest/library-base/-realm%20-kotlin%20-multiplatform%20-s-d-k/io.realm/realm-list-of.html)
companion method.
|
{OneToManyJavaSDKKotlin}
{OneToManyJavaSDKJava}
|
{OneToManyKotlinSDK}
|
## Schema Types
With the Java SDK, you needed to use the `@Required` annotation to
make lists of primitives non-nullable in realm object models. The Kotlin
SDK makes lists of primitives non-nullable by default. Use the
`?` operator to make a list of primitives nullable.
|
{SchemaTypesJavaSDKKotlin}
{SchemaTypesJavaSDKJava}
|
{SchemaTypesKotlinSDK}
|
## Writes
The Kotlin SDK introduces new names for the methods that write to realms.
### Async
With the Java SDK, you could write asynchronously to a realm with
`realm.executeTransactionAsync()`. The Kotlin SDK uses
the suspend function [`realm.write()`](https://docs.mongodb.com/realm-sdks/kotlin/latest/library-base/-realm%20-kotlin%20-multiplatform%20-s-d-k/io.realm/-realm/write.html) instead.
|
{WritesAsyncJavaSDKKotlin}
{WritesAsyncJavaSDKJava}
|
{WritesAsyncKotlinSDK}
|
### Sync
With the Java SDK, you could write synchronously to a realm with
`realm.executeTransaction()`. The Kotlin SDK uses
[`realm.writeBlocking()`](https://docs.mongodb.com/realm-sdks/kotlin/latest/library-base/-realm%20-kotlin%20-multiplatform%20-s-d-k/io.realm/-realm/write-blocking.html):
|
{WritesSyncJavaSDKKotlin}
{WritesSyncJavaSDKJava}
|
{WritesSyncKotlinSDK}
|
:::tip See Also
- [Create a New Object (Kotlin SDK)](/sdk/kotlin/realm-database/create/create-a-new-object)
- [Modify an Object (Kotlin SDK)](/sdk/kotlin/realm-database/update/modify-an-object)
- [Delete an Object (Kotlin SDK)](/sdk/kotlin/realm-database/delete/delete-an-object)
:::
## Queries
There are several differences between queries in the Java SDK and queries
in the Kotlin SDK:
- With the Java SDK, you can query objects in realms using a fluent
interface or [Realm Query Language](/sdk/kotlin/realm-database/query-language) (RQL).
The Kotlin SDK only uses RQL.
- The Java SDK uses `realm.where()`
to query realms, whereas the Kotlin SDK uses [`realm.query()`](https://docs.mongodb.com/realm-sdks/kotlin/latest/library-base/-realm%20-kotlin%20-multiplatform%20-s-d-k/io.realm/query.html).
- With the Java SDK, you could query asynchronously with
`realmQuery.findAllAsync()` and `realmQuery.findFirstAsync()`.
In the Kotlin SDK, query asynchronously with
[`realmQuery.asFlow()`](https://docs.mongodb.com/realm-sdks/kotlin/latest/library-base/-realm%20-kotlin%20-multiplatform%20-s-d-k/io.realm.query/-realm-element-query/as-flow.html).
Once you have a flow of results, you can [`collect()`](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/collect.html)
the results.
- With the Java SDK, you could query synchronously with
`realmQuery.findAll()` and `realmQuery.findFirst()`.
In the Kotlin SDK, query synchronously with
[`realmQuery.find()`](https://docs.mongodb.com/realm-sdks/kotlin/latest/library-base/-realm%20-kotlin%20-multiplatform%20-s-d-k/io.realm.query/-realm-element-query/find.html).
### Filters
|
{QueryFiltersJavaSDKKotlin}
{QueryFiltersJavaSDKJava}
|
{QueryFiltersKotlinSDK}
|
## Sort, Distinct, Limit
|
{SDLJavaSDKKotlin}
{SDLJavaSDKJava}
|
{SDLKotlinSDK}
|
:::tip See Also
[Realm Query Language (Kotlin SDK)](/sdk/kotlin/realm-database/query-language)
:::
## Deletes
In both SDKs, you can only delete live objects. The Kotlin SDK provides
[`mutableRealm.findLatest()`](https://docs.mongodb.com/realm-sdks/kotlin/latest/library-base/-realm%20-kotlin%20-multiplatform%20-s-d-k/io.realm/-mutable-realm/find-latest.html)
to access a live version of any frozen object.
|
{DeletesJavaSDKKotlin}
{DeletesJavaSDKJava}
|
{DeletesKotlinSDK}
|
:::tip See Also
- [Delete all Objects of a Type (Kotlin SDK)](/sdk/kotlin/realm-database/delete/delete-all-objects-of-a-type)
- [Delete Multiple Objects (Kotlin SDK)](/sdk/kotlin/realm-database/delete/delete-multiple-objects)
- [Delete an Object (Kotlin SDK)](/sdk/kotlin/realm-database/delete/delete-an-object)
:::
## Notifications
In both SDKs, you can subscribe to change to collections of results.
With the Java SDK, you could receive notifications whenever realm results
changed with the following interfaces:
- `realmResults.addChangeListener()`
- RxJava through `asFlowable()`
- Kotlin Extensions with `toFlow()`
The Kotlin SDK replaces all of these options with [`realmQuery.asFlow()`](https://docs.mongodb.com/realm-sdks/kotlin/latest/library-base/-realm%20-kotlin%20-multiplatform%20-s-d-k/io.realm.query/-realm-element-query/as-flow.html).
Once you have a flow of results, you can call [`collect()`](https://kotlin.github.io/kotlinx.coroutines/kotlinx-coroutines-core/kotlinx.coroutines.flow/collect.html)
to subscribe to changes. Any object of type `UpdatedResults` emitted
by the flow represents a change to the results set.
|
{NotificationsJavaSDKKotlin}
{NotificationsJavaSDKJava}
|
{NotificationsKotlinSDK}
|
## Threading
With the Java SDK, realms, Realm objects, and results cannot be passed
between threads. The Kotlin SDK freezes these objects by default, making
them thread-safe. Unlike the live objects used by the Java SDK, the
frozen objects found in the Kotlin SDK do not automatically update when
underlying data changes. With the Kotlin SDK, you must use notifications to subscribe to
updates instead.
|
{ThreadingJavaSDKKotlin}
{ThreadingJavaSDKJava}
|
{ThreadingKotlinSDK}
|
:::tip See Also
[Frozen Architecture (Kotlin SDK)](/sdk/kotlin/realm-database/frozen-architecture)
:::
## Migrations
With the Java SDK, migrations were a manual process. The Kotlin SDK
automates migrations, but also gives you access to a similar dynamic
realm interface for custom tweaks to migration logic.
|
{MigrationsJavaSDKKotlin}
{MigrationsJavaSDKJava}
|
{MigrationsKotlinSDK}
|
## What Next?
Now that you understand the differences between the Java SDK and the
Kotlin SDK, check out the rest of the [Kotlin SDK documentation](/sdk/kotlin/intro).