Skip to content

Instantly share code, notes, and snippets.

View kalaiselvan369's full-sized avatar
🎯
Focusing

Kalaiselvan kalaiselvan369

🎯
Focusing
View GitHub Profile
@kalaiselvan369
kalaiselvan369 / Api.kt
Last active June 9, 2023 07:13
Importance of package
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
@kalaiselvan369
kalaiselvan369 / access_modifiers.kt
Created June 9, 2023 05:41
Top level class, function and property visibility
// 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) {
@kalaiselvan369
kalaiselvan369 / constructor.kt
Created June 9, 2023 05:37
Constructor definition
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())
@kalaiselvan369
kalaiselvan369 / class_dog.kt
Created June 9, 2023 05:34
Class definition
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
@kalaiselvan369
kalaiselvan369 / for_loop_statement.kt
Created June 8, 2023 11:26
Statement producing side effect
fun main() {
var j = false
for (i in 1..112) {
if (i == 10) {
println(true)
j = true // side effect
}
}
}
@kalaiselvan369
kalaiselvan369 / using_in.kt
Created June 8, 2023 11:24
Checking for membership
fun main() {
val n = 130
if(n in 100..200)
println("$n is a member of 100 to 200")
inFloatRange(4.5)
}
fun main() {
val numbers = listOf<Int>(1, 2, 3, 4)
for(n in numbers) {
print(n)
}
println()
for (n in 1..5) {
@kalaiselvan369
kalaiselvan369 / increment_decrement.kt
Created June 8, 2023 11:18
Increment and Decrement
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
fun main() {
// assignment operators
var sum = 3
sum += 3
println(sum)
var sub = 13
sub -= 3
println(sub)
@kalaiselvan369
kalaiselvan369 / jvm_number_representation.kt
Created June 8, 2023 11:16
JVM representation for primitive types
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