Skip to content

Instantly share code, notes, and snippets.

@toxakktl
Created November 15, 2019 09:45
Show Gist options
  • Select an option

  • Save toxakktl/6267ebf900af1f178611919d50e35ee2 to your computer and use it in GitHub Desktop.

Select an option

Save toxakktl/6267ebf900af1f178611919d50e35ee2 to your computer and use it in GitHub Desktop.
package kz.technodom.merchant.service
//Simple hello world
fun main() {
println("Hello, World")
printMessage("Hello!")
printMessageWithPrefix("Hello", "Toxa")
printMessageWithPrefix("Hello2" )
multiply(2,3)
//infix functions
println("dude ".repeat(3))
infix fun Int.times (str:String) = str.repeat(this)
println(3 times "Bob ")
printAll("test1", "test2", "test3")
//variables
val a : String = "teststring"
val b = "kotlin"
var x : Int = 5
x =4
//null safety
var str : String ? = "Koltin tutorial"
str = null
//safe call ?.
length(str)
//bob?.department?.head?.name
val listWithNulls : List<String?> = mutableListOf("MutTest", null, "Modric")
for ( l in listWithNulls){
l?.let { println(it) }
}
//elvis operator
val be : String ? = null
val l = if (be != null) be.length else -1
val c = be?.length ?: -1
val p = Person("Alice")
println(p.name)
p.name = "New name"
val ani = Car(1, "monkey")
println(ani)
//inheritance
val dog: Animal = Dog()
dog.makeSound()
val list = listOf("1", "2", "3")
list.midElement()
}
fun length (str : String?) : Int? {
return str?.length
}
fun printMessage(message:String){
println(message)
}
fun printMessageWithPrefix(message : String, info : String = "Info"){
println("[$info] $message")
}
fun sum (x:Int, y:Int) : Int{
return x + y
}
fun multiply(x : Int, y: Int) = x * y
//Varargs allow you to pass any number of arguments by separating them with commas.
fun printAll(vararg messages : String){
for (m in messages){
println(m)
}
}
//classes
class Customer
class Person(var name: String){
fun printName(){
println(name)
}
}
data class Car (val id: Long, val name: String)
//Kotlin classes are final by default, in order to make them inheritable mark them as open classes
open class Animal(){
open fun makeSound(){
println("def def")
}
}
class Dog : Animal() {
override fun makeSound() {
println("au au")
}
}
class Cat : Animal() {
override fun makeSound() {
println("miau miau")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment