This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Conversion is not perfect, but can be helpful in the beginning | |
/* | |
public class MyJavaClass { | |
private int num = 5; | |
public void printNum() { | |
System.out.println(num); | |
switch (num) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* MUST: | |
* - be an extension function OR a member function | |
* - have a single parameter with no default value | |
* - have the "infix" keyword | |
*/ | |
infix fun Int.plus(that: Int) = this + that | |
// infix fun doStuff1() = println("blablabla") // COMPILER ERROR | |
// infix fun Int.doStuff2(a: Int, b: String) = println("blablabla") // COMPILER ERROR | |
// infix fun doStuff3(a: Int) = println("blablabla") // COMPILER ERROR |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyOwnClass { | |
// Probably should be avoided in most cases, but nice to have the flexibility | |
val `my strange field name` = mutableListOf("a", "b", "c") | |
fun `some method`() { } | |
fun anotherMethod() { | |
`my strange field name`[1] = "d" | |
`some method`() | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class LambdaGoodies { | |
// When the lambda expression has only one param, we use "it" to access it | |
fun method1() { | |
val ints = listOf(0, 1, 2, 3).filter { // note that we use CURLY braces | |
it > 1 | |
} | |
ints.forEach { println(it) } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun awesomeMethod(a: Int, b: Int) = a + b - 101 // No need to specify return type or use "return" keyword | |
// This can come in handy when combining it with Kotlin's expression features | |
fun totallyAmazingMethod(str: String?, a: Int) = | |
str?.uppercase() ?: | |
if (a == 5) "some string" else "some other string" | |
// Can be helpful when using functional programming... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Expressions { // Really useful for reducing lines of code | |
fun myMethod() { | |
val num = Math.random().toInt() | |
val str = if (num > 10) "hello" else "world" // can be viewed as a replacement for the Java ternary operator | |
var anotherNum: Int? = 50 | |
/* | |
var numCopy = 0 | |
if (anotherNum != null) { | |
numCopy = anotherNum |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ExampleClass { | |
fun printDetails(firstName: String = "John", lastName: String = "Doe", yearsOfExperience: Int = 5) { | |
println("First name: $firstName, Last name: $lastName, Years of experience: $yearsOfExperience") | |
} | |
fun myMethod() { | |
printDetails() // uses default values | |
printDetails("Bob", "Johnson", 5) // classic Java way | |
printDetails(firstName = "Bob", yearsOfExperience = 10) // with named params, some can be skipped | |
printDetails(lastName = "Johnson", yearsOfExperience = 7, firstName = "Bob") // order can be switched |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// We can add new functionality to a class without inheriting it | |
fun String.removeFirstLastChar() = this.substring(1, this.length-1) | |
class MyCoolClass { | |
fun myMethod() { | |
println( "hello".removeFirstLastChar() ) | |
println( "505".toLong() ) // Kotlin itself also gives us some extension functions | |
} | |
/* |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MyClass { // This is very helpful for preventing NPEs more easily. | |
var field1: String = "hello" // null value NOT allowed | |
var field2: String? = null // null value allowed | |
fun myMethod() { | |
// field1 = null // COMPILER ERROR | |
field2 = "world" | |
field2 = null | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object ObjectDeclarationTest { // sort of like a Singleton | |
var i = 5L | |
fun doStuff() { | |
println("hello!") | |
} | |
} | |
fun test() { // no need to instantiate, everything is static | |
ObjectDeclarationTest.i = 10L |
NewerOlder