Variables and Data Types in Kotlin :
In this tutorial we will learn how to declare variables in Kotlin. We are having java comparison code for easy and quick learning.
In kotlin all data types are defined with keyword var. There is no final
In kotlin all data types are defined with keyword var. There is no final
Example 1 :
Java Code :
class A{
private int data=50;
public String mData = "Test";
procted float test1 = 1.23;
private double test2 = 2.33; //instance variable
public static final int FILES_TO_DOWNLOAD = 100 //static variable
void method(){
int n=90;//local variable
}
}//end of class
private int data=50;
public String mData = "Test";
procted float test1 = 1.23;
private double test2 = 2.33; //instance variable
public static final int FILES_TO_DOWNLOAD = 100 //static variable
void method(){
int n=90;//local variable
}
}//end of class
Converted Kotlin Code :
class A {
private val data = 50
var mData = "Test"
var test1 = 1.23f
private val test2 = 2.33//instance variable
fun method() {
val n = 90//local variable
}
companion object {
val FILES_TO_DOWNLOAD = 100//static variable
}
}//end of class
private val data = 50
var mData = "Test"
var test1 = 1.23f
private val test2 = 2.33//instance variable
fun method() {
val n = 90//local variable
}
companion object {
val FILES_TO_DOWNLOAD = 100//static variable
}
}//end of class
No comments:
Post a Comment