Master Kotlin Quickly: Side-by-Side Java vs Kotlin Syntax Guide
This article presents a concise side‑by‑side comparison of Java and Kotlin syntax—including logging, variables, null handling, loops, and method definitions—through practical code snippets, helping Java‑experienced developers transition to Kotlin efficiently.
We’ve compiled a GitHub project “from-java-to-kotlin” that demonstrates side‑by‑side syntax differences between Java and Kotlin, helping Java‑proficient developers transition smoothly.
Logging
// Java
System.out.print("Amit Shekhar");
System.out.println("Amit Shekhar");
// Kotlin
print("Amit Shekhar")
println("Amit Shekhar")Constants and Variables
// Java
String name = "Amit Shekhar";
final String name = "Amit Shekhar";
// Kotlin
var name = "Amit Shekhar"
val name = "Amit Shekhar"Null Declaration
// Java
String otherName;
otherName = null;
// Kotlin
var otherName: String?
otherName = nullFor Loops
// Java
for (int i = 1; i <= 10; i++) { }
for (int i = 1; i < 10; i++) { }
for (int i = 10; i >= 0; i--) { }
for (int i = 1; i <= 10; i += 2) { }
for (int i = 10; i >= 0; i -= 2) { }
for (String item : collection) { }
for (Map.Entry<String, String> entry : map.entrySet()) { }
// Kotlin
for (i in 1..10) { }
for (i in 1 until 10) { }
for (i in 10 downTo 0) { }
for (i in 1..10 step 2) { }
for (i in 10 downTo 0 step 2) { }
for (item in collection) { }
for ((key, value) in map) { }Method Definitions
// Java
void doSomething() {
// logic here
}
void doSomething(int... numbers) {
// logic here
}
// Kotlin
fun doSomething() {
// logic here
}
fun doSomething(vararg numbers: Int) {
// logic here
}The project focuses on practical code examples rather than theory, making the differences clear and easy to grasp.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
