Careeroza
Kotlin vs Java
Same Program in Both Languages
A Java program needs a class and a public static void main method:
public class Main {
public static void main(String[] args) {
String name = "Asha";
int age = 22;
System.out.println("Hello " + name + ", you are " + age);
}
}Example
The Kotlin version is shorter: no class, no semicolons, types are inferred and values go straight into the string.
fun main() {
val name = "Asha"
val age = 22
println("Hello $name, you are $age")
}Output
Hello Asha, you are 22
Key Differences
| Feature | Java | Kotlin |
|---|---|---|
| Semicolons | required | optional (normally omitted) |
| Null safety | any reference can be null | String can never be null, String? can |
| Variables | int x = 5; / final | var x = 5 / val x = 5 |
| Type inference | only var for locals (Java 10+) | everywhere |
| String formatting | "Hi " + name | "Hi $name" (string templates) |
| switch | switch | when (more powerful, is an expression) |
| Checked exceptions | yes | no |
| Primitive types | int, double + wrappers | only Int, Double; compiler picks the primitive |
| Data holders | class + getters/setters/equals (or records) | data class in one line |
Tip: You do not have to choose one forever. Kotlin calls Java code and Java calls Kotlin code in the same project, so teams migrate old Android apps one file at a time.