Kotlin vs Java Example: Variable Initialization | Erselan Khan
Today I will show you the Syntax Differences between Kotlin and Java programming languages for Variable Initialization. But before moving forward, I would like to ask you to please follow my medium to get the latest updates about Android and other tech-related topics.

Background of Kotlin:
Java programming language is the oldest and most used programming language for Android app development. But in 2017, Google declared Kotlin as their official programming language for Android development. The syntax of Java and Kotlin differs in many aspects but their compilation process is almost the same. The Code of both the languages gets compiled into bytecode that is executable on Java Virtual Machine(JVM).
The Syntax for Variable Initialization:
Now let’s dive into some code examples of Kotlin as well as Java for variables initialization:
Kotlin doesn’t have primitive types like int, double, float, boolean, etc. It uses classes like Int, Float, Double, etc as an object wrapper for primitives. When Kotlin code is converted to JVM code, whenever possible, “primitive object” is converted to java primitive.
Kotlin Example:
fun main() {
// string value
var stringValue: String = "Erselan Khan"
println("String value $stringValue")
// int value
var intValue: Int = 1
println("Int value $intValue")
// float value
var floatValue: Float = 1f
println("Float value $floatValue")
// boolean value
var booleanValue: Boolean = false
println("Boolean value $booleanValue")
// double value
var doubleValue: Double = 1.0
println("Double value $doubleValue")
}
As you can see in our above example, we are only using Objects like Int, Float, Double, etc for variable initialization.
On the other hand, Java has primitive types as well as Objects for initialization of variables.
Java Example:
public static void main(String[] args) {
// String value
String stringValue = "Erselan Khan";
System.out.println("String value " + stringValue);
// Double value
Double doubleObjectvalue = 1.0;
double doubleValue = 1.0;
System.out.println("Double Object Value: " + doubleObjectvalue);
System.out.println("Double primitive type value " + doubleValue);
// Float Value
Float floatObjectValue = 1.0f;
float floatValue = 1.0f;
System.out.println("Float Object Value: " + floatObjectValue);
System.out.println("Float primitive type value " + floatValue);
// Boolean Value
Boolean booleanObjectValue = false;
boolean booleanValue = false;
System.out.println("Boolean Object Value: " + booleanObjectValue);
System.out.println("Boolean primitive type value " + booleanValue);
// Int Value
Integer integerObjectValue = 1;
int intValue = 1;
System.out.println("Integer Object Value: " + integerObjectValue);
System.out.println("Integer primitive type value " + intValue);
}
As you can see in our above example, we are using Objects like Int, Float, Double, etc, and primitive types like int, double, float, etc too for variable initialization.

That’s it for now. I will cover more topics on Android in my upcoming articles.
Next Tutorial:
- https://erselankhan.medium.com/android-kotlin-vs-java-part-2-erselan-khan-9b5f5d96de92
- https://erselankhan.medium.com/android-kotlin-vs-java-series-part-3-handle-null-objects-dd8354021e38
Show your love by sharing this article with your fellow developers.
(Again, the source for this demo is on https://github.com/arsalankhan994/kotlin-vs-java/tree/master/src/main. Follow me for more content about Android, Kotlin, and other technologies. If you have any questions, go ahead and ask me here or email me at arsalankhan994@gmail.com and I’ll do my best to respond.)