Today, a friend of TJ came to ask TJ. He said that he used JAVA as a thief and wanted to learn Kotlin again. He asked TJ how difficult it was to learn Kotlin.
How can I put it? In fact, TJ has always felt that the language aspect is familiar, since JAVA has been stolen, I must learn other languages must be more effective with half the effort.
Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin: Kotlin
Instead of talking big, the project uses practical code to show the differences. TJ:
Print log
//Java
System.out.print("Amit Shekhar");
System.out.println("Amit Shekhar");
//Kotlin
print("Amit Shekhar")
println("Amit Shekhar")
Copy the code
Constants and variables
//Java
String name = "Amit Shekhar";
final String name = "Amit Shekhar";
//Kotlin
var name = "Amit Shekhar"
val name = "Amit Shekhar"
Copy the code
Null value declaration
//Java
String otherName;
otherName = null;
//Kotlin
var otherName : String?
otherName = null
Copy the code
The for loop
//java for (int i = 1; i <= 10 ; i++) { } for (int i = 1; i < 10 ; i++) { } for (int i = 10; i >= 0 ; i--) { } for (int i = 1; i <= 10 ; i+=2) { } for (int i = 10; i >= 0 ; i-=2) { } for (String item : collection) { } for (Map.Entry<String, String> entry: map.entrySet()) { } //Kotlin for (i in 1.. 10) { } for (i in 1 until 10) { } for (i in 10 downTo 0) { } for (i in 1.. 10 step 2) { } for (i in 10 downTo 0 step 2) { } for (item in collection) { } for ((key, value) in map) { }Copy the code
Method definition
//Java
void doSomething() {
// logic here
}
void doSomething(int... numbers) {
// logic here
}
//Kotlin
fun doSomething() {
// logic here
}
fun doSomething(vararg numbers: Int) {
// logic here
}
Copy the code
Is it more intuitive to look at it this way? There are more differences waiting for you to see, if you feel good, remember to give TJ a thumbs-up! The project address is github.com/MindorksOpe…
TJ has sorted out all kinds of projects and tools sent to GitHub project, welcome to Star, the address is as follows: github.com/Wechat-TJ/T…