referenced and used by a program to temporarily store information, e.g.a calculator program uses variables to store operands. Type refers to the particular kind of data that is held by a variable
immutability using the keyword val. In kotlin variables defined using the keyword var can be reassigned whereas variables defined using val cannot be reassigned Two ways of defining a variable: Explicitly stating the variable type e.g. int Defining Variables in Kotlin
remain absolutely positively immutable and not to change at all. A constant must be declared outside any function because it is as assigned at compile time, while functions are called at run time. • String • Int • Double • Float • Long • Short • Byte • Char • Boolean
danger of null references from code, also referred to as The Billion Dollar Mistake. Accessing a null reference will result in a Null Pointer Exception Kotlin distinguishes references that can hold null (nullable references) and those that can not (non-null references)
exists a danger of performing an operation on a nullable type e.g. a.length() because it may not exist. Kotlin prevents you from calling functions on a value defined as nullable until you have accepted responsibility for this unsafe situation.
non-null assertion operator can be used to call a function on a nullable type. It is a more drastic option than safe call operator and should not be generally used. If the compiler encounters an operation on a null variable, it throws a null pointer exception.
must be initialized in the constructor, however in some cases this may not be convenient. Marking your variables with the lateinit modifier (late initialization) allows you to define the variables at a later point in the program flow, however a run time error will be thrown if the program attempts to use the variable and it is yet to be initialized.
prevent unnecessary initialization of objects, especially objects that have cumbersome initialization processes.Kotlin language has built in support for lazy initialization. When using lazy property, initialization occurs when the variable is first accessed, subsequent calls return the same instance.
of classes, objects, interfaces, functions, etc. Kotlin has four visibility modifiers; • private means visible inside this class only (including all its members); • protected — same as private + visible in subclasses too; • internal — any client inside this module who sees the declaring class sees its internal members; • public — any client who sees the declaring class sees its public members.
a specific function and may return a value of a specific type. In Kotlin, a function is defined using the keyword fun; it’s fun, right? Parameters Function parameters are declared using pascal notation, i.e. name:type Parameters are separated using commas
a range. A range includes all values from the left of the .. operator to the value on the right. 1 .. 5 includes 1,2,3,4,5. The in keyword is used to check whether a value is in range.
types using Any(), Any takes in any form of input, one can use an if/else expression or when to perform different operations depending on the data type.
and one or more secondary constructors. The primary constructor is part of the class header: it goes after the class name (and optional type parameters).
that is the default superclass for a class with no supertypes declared: Any has three methods: equals(), hashCode() and toString(). Thus, they are defined for all Kotlin classes.
is to hold data. In such a class some standard functionality and utility functions are often mechanically derivable from the data. In Kotlin, this is called a data class and is marked as data:
be declared abstract. An abstract member does not have an implementation in its class. Note that we do not need to annotate an abstract class or function with open – it goes without saying.
can be called without having a class instance but needs access to the internals of a class (for example, a factory method), you can write it as a member of an object declaration inside that class.