Difference Between const and val in Kotlin

Soumak De
Updated on 27-Oct-2021 08:54:22

7K+ Views

const KeywordThe const keyword is used in Kotlin whenever the variable value remains const throughout the lifecycle of an application. It means that const is applied only on immutable properties of the class. In simple words, use const to declare a read-only property of a class.There are some constraints that are applied to the const variable. They are as follows −const can only be applied to the immutable property of a class.It cannot be assigned to any function or any class constructor. It should be assigned with a primitive data type or String.The const variable will be initialized at compile-time.ExampleIn ... Read More

Kotlin Property Initialization: by lazy vs lateinit

Soumak De
Updated on 27-Oct-2021 08:51:06

11K+ Views

Kotlin library provides two different access modifiers for property declaration.In this article, we will highlight the difference between these two access modifiers and how we can use them in our application.LateinitIn order to create a "lateInit" variable, we just need to add the keyword "lateInit" as an access modifier of that variable. Following are a set of conditions that need to be followed in order to use "lateInit" in Kotlin.Use "lateInit" with a mutable variable. That means, we need to use "var" keyword with "lateInit"."lateInit" is allowed only with non-NULLable data types."lateInit" does not work with primitive data types."lateInit" can ... Read More

Check If a lateinit Variable Has Been Initialized in Kotlin

Soumak De
Updated on 27-Oct-2021 08:41:14

3K+ Views

Any variable which is initialized after its declaration is known as a "late initialized variable". In conventional programming languages, any non-NULL type of variable need to be initialized in the constructor. But sometimes, by mistake, developers forget to do these NULL checks which causes a programming error. In order to avoid this situation, Kotlin introduced a new modifier called as "lateInit". Along with this modifier, Kotlin provides a couple of methods to check whether this variable is initialized or not.In order to create a "lateInit" variable, we just need to add the keyword "lateInit" as an access modifier of that ... Read More

Equivalent of Java Static Methods in Kotlin

Soumak De
Updated on 27-Oct-2021 08:22:43

483 Views

In Java, "static" keyword is used for efficient memory management. Once a variable or method is declared as static, then the JVM will allocate memory for these variable only once. Usually static variables are used to declare common properties of a class, for example, "Name of the institution". In the following example, we will see how to use the static keyword.Example of Static in using JavaIn order to demonstrate how static works in Java, we will access our online Java compiler and we will create a Test class. Inside Test, we will try to create a static variable along with ... Read More

Throw Custom Exception in Kotlin

Soumak De
Updated on 27-Oct-2021 07:56:24

1K+ Views

Exception is an important aspect of any programming language. It prevents our code from generating incorrect output at runtime.The concept of exception in Kotlin is very much same as it is in Java. All the exceptions in Kotlin are the descendants of Throwable class. In Kotlin, developers do have the privilege to create their own custom Exception.Custom Exceptions are a part of unchecked exceptions which means they will be thrown at the runtime.Before getting into custom exceptions in Kotlin, let's take a look checked and unchecked exceptions.Checked ExceptionsChecked exceptions are those which are checked at the compile time. In the ... Read More

Node.js Timers Module: Scheduling Timers

Mayank Agarwal
Updated on 27-Oct-2021 07:51:47

737 Views

The timers module contains functions that can execute code after a certain period of time. You do not need to import the timer module explicitly, as it is already globally available across the emulated browser JavaScript API.Timers module is mainly categorised into two categoriesScheduling Timers − This timer schedules the task to take place after a certain instant of time.setImmediate()setInterval()setTimeout()Cancelling Timers − This type of timers cancels the scheduled tasks which is set to take place.   ClearImmediate()clearInterval()clearTimeout()Scheduling Timers1. setTimeout() MethodThe setTimeout() method schedules the code execution after a designated number of milliseconds. Only after the timeout has occurred, the code ... Read More

Positive Net Present Value (NPV) Implications

Probir Banerjee
Updated on 27-Oct-2021 06:44:01

766 Views

The Net Present Value (NPV) is a measure of an investment’s profitability. It can be either positive or negative. Positive NPVs are preferred because they point toward a profitable investment, while negative NPV investments are rejected as they show large losses.NPV is important because it is the best rule to determine the profitability of an investment project. NPV is the most important investment rule for the following reasons.Note − A positive NPV implies that the profitability of a firm in the present time and unit exceeds the NPV of another in the same time and unit.The Time Value of MoneyNPV ... Read More

Multiples of 3 and 5 Without Using Operator in C++

Hafeezul Kareem
Updated on 27-Oct-2021 06:28:22

702 Views

We can find the multiples using % operator without any hurdles. But, the problem states that we can't use % operator.Here, we make use of the + operator. We can get the multiples by adding 3 or 5 to the previous multiple. Let's see an example.Input15Output1 2 3 - Multiple of 3 4 5 - Multiple of 5 6 - Multiple of 3 7 8 9 - Multiple 3 10 - Multiple of 5 11 12 - Multiple of 3 13 14 15 - Multiple of both 3 and 5AlgorithmInitialise the number n.Initialise two number to keep track of next ... Read More

Iterate Over a HashMap in Kotlin

Soumak De
Updated on 27-Oct-2021 06:22:01

3K+ Views

A Map is a collection where data is stored as a key-value pair and the corresponding key has to be unique. A HashMap is a collection class based upon MutableMap interface and it does that by implementing MutableMap interface of HashTable. Kotlin provides four types of constructor to define and manipulate HashMap.HashMap() – It is the default constructor which helps us create an empty HashMap.HashMap(initialCapacity: Int, loadFactor: Float = 0f) – It helps us create a HashMap using initial capacity; if it is not provided, then it will be ignored and it will act as default HashMap().HashMap(initialCapacity: Int) – It ... Read More

Value Additivity Principle in the NPV Method

Probir Banerjee
Updated on 27-Oct-2021 06:00:33

1K+ Views

The Value Additivity Principle in NPV states that the value of the total NPV of a bigger project is equal to the summation of all smaller NPVs of projects. In other words, the summation of all smaller NPVs provides the bigger NPV of an investment project. The NPV of a group of the independent projects will be equivalent to the NPV of all the independent projects.If A and B are two smaller projects, then the total NPV of the bigger project (A+B) would be −NPV (A+B) = NPV (A) + NPV (B)Value Additivity is an important principle because using it, ... Read More

Advertisements