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
package api | |
// since the class is declared as internal, it is accessible to all the packages | |
// that are declared inside the same module. | |
internal class ApiClient |
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
// top level private property | |
private var score = 0 | |
// top level private class | |
private class Batsman() { | |
fun hitSix() = 6 | |
} | |
// top level private function | |
private fun recordScore(run: Int) { |
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 main() { | |
// constructor is a special type of member function that instantiate the class | |
val elephant = Elephant() | |
println(elephant) | |
val employee = Employee("Ram") | |
println(employee) | |
val student = Student("Arun") | |
println(student.toString()) |
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 main() { | |
val dog = Dog() | |
dog.bark() | |
} | |
// class is a user defined data type | |
class Dog { | |
// characteristics/properties of a class | |
val legs: Int = 4 |
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 main() { | |
var j = false | |
for (i in 1..112) { | |
if (i == 10) { | |
println(true) | |
j = true // side effect | |
} | |
} | |
} |
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 main() { | |
val n = 130 | |
if(n in 100..200) | |
println("$n is a member of 100 to 200") | |
inFloatRange(4.5) | |
} |
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 main() { | |
val numbers = listOf<Int>(1, 2, 3, 4) | |
for(n in numbers) { | |
print(n) | |
} | |
println() | |
for (n in 1..5) { |
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 main() { | |
// increment and decrement operators | |
var i = 0 | |
val j = i++ // post increment | |
var k = 0 | |
val l = ++k // pre increment | |
println("j:$j , l:$l") // prints j:0 , l:2 |
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 main() { | |
// assignment operators | |
var sum = 3 | |
sum += 3 | |
println(sum) | |
var sub = 13 | |
sub -= 3 | |
println(sub) |
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 main() { | |
val a: Int = 100 | |
val boxedA: Int? = a | |
val anotherBoxedA: Int? = a | |
// === means referential equality | |
println(boxedA === anotherBoxedA) // true | |
//JVM has predefined values for int between -128 to 127 hence above statement is true | |
val b: Int = 10000 |