Kotlin Vs Java
In this blog, we are going to see the difference between the syntax of implementation in java and Kotlin. The main motive behind this blog is to help the learners of Kotlin who already know java. There are many new features added to Kotlin which were not present in java language like.
So first start with variables
1. Variables are the container that can store some values and its value can be changed when required.
Constants are the container that can store some values but its value cannot be changed.
1.1. Declaration of variables in Java
Syntax : datatype variableName = value ;
Declaration and initialization in a single line
Example
Declaration and initialization of constant, we must initialize a constant at the time of declaration. In java, final keyword is used to declare constant values.
1.2 Declaration of variables in Kotlin
var keyword is used for declaring a variable
val keyword is used in Kotlin to declare a constant
lateinit keyword is used when we need to declare a variable and we want to initialize it somewhere else later.
Syntax :
Example
An Important Note
In Kotlin semicolon is not used at the end of variable declaration
1.3. Functions: Function is a block of code used to perform a specific task or operation it is used for achieving code reusability
Some terms related to function are
1.3.1. Parameters: the values which are provided to a function are known as its parameters.
1.3.2. Return type: The type of value returned by a function is known as its return type. The function that does not return any value is said to have a void return type. The return keyword is used to return a value
1.3.4. Function name: It is the name using which the function can be called whenever required. Example : sum()
1.4. Function in Java
Example :
Function with-param and return type
Function with-param and no return type
Function without param and without return type
Function without param and return type
1.5. Functions in Kotlin
Example :
Function with-param and return type
Function with-param and no return type
Function without param and without return type
Function without param and return type
1.6. Control Statements
1.6.1. if-else: Used to perform some operation based on the value of conditional expression passed inside if()
1.6.1.1. if-else in Java
Syntax
Example
1.6.1.2. if-else in Kotlin
Example
1.6.2. switch case: Used to perform an operation based on matching the case with the value passed inside the switch(ch) in java and when(ch) in Kotlin. There are some differences in switch case of java and Kotlin
1.6.2.1. Switch Case in Java
- There can be one or N number of case values for a switch expression.
- The case value must be of switch expression type only. The case value must be literal or constant. It doesn’t allow variables.
- The case values must be unique. In case of duplicate value, it renders compile-time error.
- The Java switch expression must be of byte, short, int, long (with its Wrapper type), enums and string.
- Each case statement can have a break statement which is optional. When control reaches to the break statement, it jumps the control after the switch expression. If a break statement is not found, it executes the next case.
- The case value can have a default label which is optional.
Example :
1.6.2.2. when in Kotlin
Keyword ‘when’ is an alternative for the switch in Kotlin. Some advantages of when over switch are as following :
- no complex case/break groups, only the condition followed by ->
- it can group two or more equivalent choices, separating them with a comma
- Instead of having a default branch, when has an else branch. The else branch is required if when is used as an expression. So, if when returns a value, there must be an else branch.
- No need to put a break after each case
- It can be used without an argument. In such a case it acts as a nicer if-else chain: the conditions are Boolean expressions. As always, the first branch that matches is chosen. Given that these are boolean expressions, it means that the first condition that results True is chosen.
1.6.3. Loop: Loop is used to repeat a task or operation for a certain number of times. There are three steps in a loop
1.6.3.A. Initialization: it involves providing initial value to the loop variable.
1.6.3.B. Condition: It is an expression based on the value of which the loop continues or stops its execution. The loop continues to execute until the condition is true and gets stopped when it becomes false.
1.6.3.C. Increment/Decrement: It increases or decreases the value of the loop variable after each execution.
1.6.3.1. Loop
1.6.3.1.1. For loop in java
Syntax
Examples
1.6.3.1.2. While loop
Syntax
Example
1.6.3.1.3. do-while loop
Syntax
do{
// code to repeated
increament/decreament
}
while(conditionExpression);
Example
- 6.3.2 Loop in Kotlin
1.6.3.2.1 For Loop
Examples of for loop in kotlin
for (x in 0..10) println(x) // Prints 0 through 10 (inclusive)
for (x in 0 until 10) println(x) // Prints 0 through 9
for (x in 0 until 10 step 2) println(x) // Prints 0, 2, 4, 6, 8
for (x in 10 downTo 0 step 2) println(x) // Prints 10, 8, 6, 4, 2, 0
1.6.3.2.2. While Loop
The condition must be an actual boolean expression, as there's no concept of truthy or falsy values .
Examples
Use of continue
1.6.3.2.3. do-while loop
The codes inside the body of do
construct is executed once (without checking the testExpression). Then, the test expression is checked.
If the test expression is evaluated true
, codes inside the body of the loop are executed, and test expression is evaluated again. This process goes on until the test expression is evaluated to false
.
When the test expression is evaluated to false
, do..while
loop terminates.
1.7 Class and Objects
1.7.1. Creating class in Java
1.7.2. Creating class in kotlin
1.7.3. Creating Objects in Java
1.7.3. Creating Objects in Kotlin
1.7 Advantages of using Kotlin over Java
- It’s Completely Interoperable With Java
- More Concise Than Java
- Null Safety