Learn Kotlin: Basic Kotlin Question for Android Beginner

December 07, 2018 , , , 0 Comments

Image result for kotlin

# Where should we keep constants in Kotlin?

  • if you need constants to be accessed globally use const as top-level properties (not inside a class).
In order to do this create a Kotlin file named Constants.kt. The file would look like this:
This way you will be able to access the constants from anywhere within the app just by typing the constant name (in Kotlin code). In Java code you will need to write ConstantsKt.MY_APP_ID.
  • if you have constants that are specific to a scope, like Employee for example, create a companion object

And this is how you use the constants.

# What do ?, ?., ?: and !! mean?

var b: String? 
=>b CAN be Null

=> if b is not Null return length else return Null

=> if b is not Null return length else return Empty String

=> if b is not Null return length else return Throw NullPointerException

# Object to make casts in Kotlin?

val listView = findViewById(R.id.listView) as ListView

# List in Kotlin Init

sample 1 : val list: List<String> = listOf("Apple", "Cherry", "Carrot")

In Kotlin, lists are immutable by default (you can’t add or remove elements after the list is created). If you need to make a list mutable, you can do this by using mutableListOf
val list: List<String> = mutableListOf<>("Apple", "Cherry", "Carrot")


by creating an ArrayList.

val list: ArrayList<String> = arrayListOf("Apple", "Cherry", "Carrot") //mutable

  • listOfNotNull() FUNCTION 

The output of the above code will be this:
Notice that null values are ignored.
  • Empty lists

There are 2 ways to create empty lists. They do the same thing, so it’s up to you which way you prefer.
emptyList<String>() returns an Immutable empty list.

# How to create a SET in Kotlin?

  • Create a Set in Kotlin

  • Create a HashSet

  • Create a SortedSet (TreeSet in Java)

  • Create a LinkedSet (LinkedHashSet in Java)

# How to create a Map in Kotlin?

  • Create a Map

  •  How to iterate a Map?

HashMap, LinkedMap (LinkedHashMap in Java) and SortedMap are all mutable.

# What collection methods are useful?


Returns true if collection has at least one element.

any(predicate: (T) -> Boolean

Returns true if at least one element matches the given predicate.


Returns a reversed read-only view of the original List.


Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google



Best Watch Store

Popular Posts

Contact Form


Email *

Message *