Kotlin basic information for developers
Val: val is
immutable variable type means non-changeable value. We say in other words this
is same as the final keyword in java.
Var : var is mutable variable type means changeable
value.
Lateinit: lateinit is late initialization. This is used
with var variable and non-nullable initialization. Lateinit is global
initialization. Allow initialization a non-null property outside of a
constructor
Lazy: Basically
lazy use with val variable. Like
val name =String by lazy{“samset”}
Difference between lateinit and lazy?
Both are
the delay the property of initialization but lateinit modifier use
with only var variable and lazy use with val variable. val is define run time
property.
What is default visibility access modifier?
Public is
default access modifier.
Public,internal,protected,private
Does the following inheritance structure is compile?
Class A{}
Class B:A(){
}
No, By default
classes are final in kotlin. To make them non-final, you need to add the open
modifier.
How is the type of constructor in kotlin?
Two type
1: Primary -> Theses are defined in the class headers. They can not hold any logic. They are only one constructor per classes.
2: Secondary-> They are defined in the class body. They can hold logic There is more than one secondary constructor.
1: Primary -> Theses are defined in the class headers. They can not hold any logic. They are only one constructor per classes.
2: Secondary-> They are defined in the class body. They can hold logic There is more than one secondary constructor.
What is the init block in kotlin?
Init block is
initializer block in kotlin.It is executed at once after initializing primary
constructor.
What is the type of argument in the constructor?
By default the
constructor holds val but you can change explicitly in var.
What is the data class in kotlin?
Data class is a
simple getter setter class that be provide toString(), hash() and copy()
method.
What is the difference between inline and infix
functions?
Inline
function is used
to save memory overhead by preventing object to allocations. It provides the
function body. This increases the bytecode slightly but saves a lot of
memory.
Infix
function is other use
to call the function without parentheses or brackets.
How to create a singleton class in kotlin?
To use object the pattern for our class we must use the keyword.
object MyClass{}
Does kotlin have the static keyword?
No, To create a static method in kotlin we use the companion object.
Class A{
companion
object
{
fun
myMethod(): Int =5
}
}
Internal : Declare and visible and accessible in current
module.
Open: Allow subclassing a class or overriding members.
Sealed : Restricted to subclassing.
Const: Const is compile-time variable. Const can be
used with var.When add cont with val this is work as a compile-time variable.
This is use with only val variable and string and primitive data type. This is
like static keyword in java.
No comments:
Post a Comment