Quick Kotlin Cheat Sheet for Java Developers: 10 Essential Syntax Comparisons
This tutorial provides Java programmers with a concise side‑by‑side guide to Kotlin syntax, covering variable declarations, functions, static members, arrays, varargs, ternary alternatives, main methods, object instantiation, property accessors, lazy initialization, and class reflection, all without delving into advanced language features.
1. How to define variables
Java variable declaration: String string = "Hello"; Kotlin equivalent: var string: String = "Hello" Java final variable: final String string = "Hello"; Kotlin compile‑time constant: const val string: String = "Hello" Java non‑constant final variable: final String string = getString(); Kotlin mutable variable: val string: String = getString() Kotlin can infer the type, so the type annotation is optional.
2. How to define functions
Java method (inside a class):
public boolean testString(String name) {
...
}Kotlin equivalent:
fun testString(name: String): Boolean {
...
}Note that the return type appears after the parameter list.
3. How to define static variables and methods
Java static member:
public class Singleton {
private static Singleton instance = ...;
public static Singleton getInstance() {
return instance;
}
}Kotlin translation using a companion object:
class KotlinSingleton {
companion object {
private val kotlinSingleton = KotlinSingleton()
@JvmStatic
fun getInstance() = kotlinSingleton
}
}For static‑like functionality, Kotlin often prefers package‑level functions.
4. How to define arrays
Java array creation:
String[] names = new String[]{"Kyo", "Ryu", "Iory"};
String[] emptyStrings = new String[10];Kotlin array creation:
val names: Array<String> = arrayOf("Kyo", "Ryu", "Iory")
val emptyStrings: Array<String?> = arrayOfNulls(10)Kotlin provides specialized primitive arrays such as IntArray to avoid boxing:
val ints = intArrayOf(1, 3, 5)5. How to write varargs
Java varargs:
void hello(String... names) {
...
}Kotlin varargs:
fun hello(vararg names: String) {
...
}6. How to write a ternary operator
Java ternary expression: int code = isSuccessfully ? 200 : 400; Kotlin does not have a ternary operator; the same logic uses an if expression: val code = if (isSuccessfully) 200 else 400 In Kotlin, if is an expression that returns a value.
7. How to write the main function
Java entry point:
class Main {
public static void main(String... args) {
...
}
}Kotlin entry point using a companion object:
class KotlinMain {
companion object {
@JvmStatic
fun main(args: Array<String>) {
...
}
}
}Kotlin also allows a top‑level main function without a class:
fun main(args: Array<String>) {
...
}8. How to instantiate a class
Java uses the new keyword: Date date = new Date(); Kotlin omits new:
val date = Date()9. How to write getter and setter methods
Java classic getter/setter:
public class GetterAndSetter {
private int x = 0;
public int getX() { return x; }
public void setX(int x) { this.x = x; }
}Kotlin property with explicit accessor definitions:
class KotlinGetterAndSetter {
var x: Int = 0
set(value) { field = value }
get() = field
}Custom logic can be added inside the accessors, using field to refer to the backing field.
10. How to lazily initialize member variables
Java fields are initialized to default values (e.g., 0, false, null).
Kotlin nullable property:
class Hello {
private var name: String? = null
}Using lateinit for non‑nullable properties that will be set later:
class Hello {
private lateinit var name: String
}Kotlin lazy delegate for deferred computation:
class Hello {
private val name by lazy { NameProvider.getName() }
}11. How to obtain a Class instance
Java ways:
Class<?> clazz = Hello.class;
Hello hello = new Hello();
Class<?> clazz2 = hello.getClass();Kotlin obtaining a Java Class:
val clazz = Hello::class.java
val hello = Hello()
val clazz2 = hello.javaClassKotlin’s Hello::class yields a KClass; appending .java converts it to the Java Class object.
Tencent TDS Service
TDS Service offers client and web front‑end developers and operators an intelligent low‑code platform, cross‑platform development framework, universal release platform, runtime container engine, monitoring and analysis platform, and a security‑privacy compliance suite.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
