Skip to content

Instantly share code, notes, and snippets.

View goindwalia's full-sized avatar

Gursimran Singh goindwalia

  • India
View GitHub Profile
fun MutableList<Int>.swap(index1:Int,index2:Int){
val tmp=this[index1]
this[index1]=this[index2]
this[index2]=tmp
}
val book = Book(“Kotlin”, “JetBrains”)
val copy = book.copy()
/* kotlin Code */
data class Book(var title:String,var author:Author)
/* Java Code */
class Book {
private String title;
private Author author;
public String getTitle() {
/* Kotlin Program */
data class Address(var street:String,
var streetNumber:Int,
var postCode:String,
var city:String,
/* Java program */
public class Address {
private String street;
private int streetNumber;
private String postCode;
/* won’t compile */
val name: String? = null
val len = name.length
/* correct way */
val name: String? = null
val len = name?.length
val name: String? = null //assigned null and it will compile also.
fun getName() : String? = null //returned null and it will compile too.
val name: String = null // tries to assign null, won’t compile.
fun getName() : String = null // tries to return null, won’t compile.
/* 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? :)