Ever since when I first started to learn Java, never did I realize that something so similar could be made. I started with Core, and Advanced and began my journey.
Java is one of the oldest and easiest languages. It has been at the top for more than two decades and it is still a very popular and widely used programming language. Being fluent in Java is a very good advantage to be able to learn new and similar languages.
Kotlin Overview:
Kotlin is a cross-platform, statically typed, general-purpose programming language with type inference. Kotlin is designed to interoperate fully with Java, and the JVM version of its standard library depends on the Java Class Library, but type inference allows its syntax to be more concise. Kotlin is a very concise language with handy mutability and immutability features.
How to create project in Kotlin:
1.Go to new project from file.
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/new_project.png)
2.Choose new activity.
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/activity_new.png)
3.Choose kotlin language.
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/project.png)
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/language-1.png)
How to add new Activity in Kotlin:
- Go to the app folder.
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/app_folder.png)
2. Create Activity.
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/activity.png)
The val
and var
keywords can be used only when the type can be inferred. Otherwise you need to declare the type.
var
var, Kotlin’s keyword representing mutable, non-final variables. Once initialized, we’re free to mutate/change the data held by the variable.
How this works:
var myVariable = 1
Variables declared as one data type and then initialized with a value of the wrong type will result in an error.
var myVariable: Int = b //ERROR! as b is a different type
There is example for var(Mutable property):
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/var.png)
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/var_error.png)
There is error on btnSigin because of mutable property of Button.
val
Val keyword works the same as the var keyword, but with one key difference, the variable is read-only/nonmutable. The use of Val is like declaring a new variable in Java with the final keyword.
For example, in Kotlin:
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/strng-1.png)
Whereas in Java:
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/string_java.png)
lateinit
lateinit means late initialization. If you do not want to initialize a variable in the constructor instead you want to initialize it later on and if you can guarantee the initialization before using it, then declare that variable with lateinit keyword. It will not allocate memory until initialized.
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/lateinit.png)
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/lateinit1.png)
Method Definitions:
Kotlin:
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/method.png)
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/boolean_method.png)
Java:
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/method_java-1.png)
Use inner class:
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/inner_class.png)
Kotlin:
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/intent.png)
Java:
![](http://localhost/anviam-blog/wp-content/uploads/2020/07/intent_java.png)