Skip to content

Instantly share code, notes, and snippets.

View dvt32's full-sized avatar

Dimitar Trifonov dvt32

View GitHub Profile
// 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) {
/**
* 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
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`()
}
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) }
}
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...
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
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
// 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
}
/*
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
}
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