This file contains hidden or 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 MutableList<Int>.swap(index1:Int,index2:Int){ | |
val tmp=this[index1] | |
this[index1]=this[index2] | |
this[index2]=tmp | |
} |
This file contains hidden or 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
val book = Book(“Kotlin”, “JetBrains”) | |
val copy = book.copy() |
This file contains hidden or 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
/* kotlin Code */ | |
data class Book(var title:String,var author:Author) |
This file contains hidden or 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
/* Java Code */ | |
class Book { | |
private String title; | |
private Author author; | |
public String getTitle() { |
This file contains hidden or 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
/* Kotlin Program */ | |
data class Address(var street:String, | |
var streetNumber:Int, | |
var postCode:String, | |
var city:String, |
This file contains hidden or 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
/* Java program */ | |
public class Address { | |
private String street; | |
private int streetNumber; | |
private String postCode; |
This file contains hidden or 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
/* won’t compile */ | |
val name: String? = null | |
val len = name.length | |
/* correct way */ | |
val name: String? = null | |
val len = name?.length |
This file contains hidden or 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
val name: String? = null //assigned null and it will compile also. | |
fun getName() : String? = null //returned null and it will compile too. |
This file contains hidden or 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
val name: String = null // tries to assign null, won’t compile. | |
fun getName() : String = null // tries to return null, won’t compile. |
This file contains hidden or 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
/* Kotlin Code*/ | |
/* Simple Hello Word Example*/ | |
//optional package header | |
package hello | |
//package level function, which return Unit and takes an array of string as parameter | |
fun main(args: Array < String > ) { | |
val scope = “world” | |
println(“Hello, $scope!”) //semicolons are optional, have you noticed that? :) |