-
Notifications
You must be signed in to change notification settings - Fork 0
Basics
Taras Koshkin edited this page May 29, 2017
·
13 revisions
Java
final int a = 0;
final int b = 1;
int c = 0;
c = 1;
Kotlin
//val is immutable
val a: Int = 0
val b = 1 //Int is implied
//var is mutable
var c: Int = 0
c = 1
Java
//Simple Synthax
Date today = null;
long t1 = today.getTime(); //NullPointerException
Kotlin
//Simple Synthax
val today: Date? = null
//do
val t1 = today?.time //t1 (Long?) = null
val t2 = today?.time ?: 0L //t2 (Long) = 0L
//do not
val t4: Long = today.time //Compilation Error!
val t3 = today!!.time //NullPointer Exception DO NOT USE '!!'
Java - Android example
//Examples
TextView profileFirstName = (TextView) findViewById(R.id.first_name);
TextView homePhoneNumber = (TextView) findViewById(R.id.home_number);
Profile profile = dataSource.getProfile();
if(profile != null){
profileFirstName.setText(profile.getFirstName());
if (profile.getNumbers() != null) {
homePhoneNumber.setText(profile.getNumbers().getHomeNumber());
} else {
homePhoneNumber.setText("N/A");
}
} else {
profileFirstName.setText("Default");
}
Kotlin - Android example
val profileFirstname = findViewById(R.id.first_name) as TextView //use 'as' to cast
val homePhoneNumber = findViewById(R.id.home_number) as TextView
val profile = dataSource.getProfile()
profileFirstname.text = profile?.firstName ?: "Default" //Optionals with Elvis expression
homePhoneNumber.text = profile?.numbers?.homeNumber ?: "N/A" //Optionals Chaining!
Java
final String firstName = "Taras";
final String lastName = "Koshkin";
final String fullName = firstName + " " + lastName;
final String initials = firstName.substring(0, 1) + lastName.substring(0, 1);
Kotlin
val firstName = "Taras"
val lastName = "Koshkin"
val fullName = "$firstName $lastName"
val initials = "${firstName.substring(0, 1)}${lastName.substring(0, 1)}" //expressions within text
Java
EditText editText = (EditText) findViewById(R.id.edit_text);
if (activity instanceof AppCompatActivity) {
((AppCompatActivity) activity).onBackPressed();
}
Kotlin
val editText = findViewById(R.id.edit_text) as EditText
if (activity is AppCompatActivity) {
activity.onBackPressed() //Activity is automatically Smart Casted into AppCompatActivity
}
Java
int x = ...; // value
String result;
switch (x) {
case 1:
result = "primary number";
break;
case 3:
result = "primary number";
break;
case 7:
result = "primary number";
break;
default:
if (x >= 4 && x <= 6) {
result = "from 4 to 6";
break;
} else if (x < 8 && x > 12) {
result = "not from 8 to 12";
break;
} else {
result = isOdd(x) ? "add number" : "even number";
break;
}
}
//What if it's not a simple int?
if (bodyPart instanceof Arm) {
...
} else if (bodyPart instanceof Leg) {
...
} else {
...
}
Kotlin
val x: Int = ...
val result = when (x) {
1, 3, 7 -> "primary number"
in 4..6 -> "from 4 to 6"
!in 8..12 -> "not from 8 to 12"
else -> if (isOdd(x)) "odd number" else "even number"
}
when(bodyPart) { //It's a lot more versatile
is Arm -> ...
is Leg -> ...
else -> ...
}
Java
for (int i = 1; i <= 10; i++) System.out.print(i);
for (int i = 1; i <= 10; i += 2) System.out.print(i);
for (int i = 10; i > 0; i--) System.out.print(i);
for (int i = 10; i > 0; i -= 2) System.out.print(i);
ArrayList<String> collection = new ArrayList<>();
HashMap<String, String> map = new HashMap<>();
for (String item : collection) System.out.print(item);
for (String item : collection) System.out.print(collection.indexOf(item) + " " + item);
for (Map.Entry<String, String> entry : map.entrySet())
System.out.print(entry.getKey() + " " + entry.getValue());
Kotlin
for (i in 1..10) print(i)
for (i in 1..10 step 2) print(i)
for (i in 10 downTo 1) print(i)
for (i in 10 downTo 1 step 2) print(i)
val collection = ArrayList<String>()
val map = HashMap<String, String>()
for (item in collection) print(item)
for ((index, item) in collection.withIndex()) print("$index $item")
for ((key, value) in map) print("$key $value")
Java
List<String> list = Arrays.asList("one", "two", "three");
Map<String, Integer> map = new HashMap<>();
map.put("one", 1);
map.put("two", 2);
map.put("three", 3);
for (String item : list) {
System.out.print(item);
}
for (String item : list) {
if (item.equals("two")) {
System.out.print(map.get(item));
}
}
Kotlin
val list = arrayListOf("one", "two", "three") //or you can use listOf()
val map = hashMapOf("one" to 1, "two" to 2, "three" to 3) //or you can use mapOf()
list.forEach { print(it) } //by default it is the list item
list.filter { it != "two" } //skips "two"
.map { map[it] } //changes the object from "one" to 1
.forEach { print(it) } //will print >> 12