Kotlin is a statistically typed language. It has been built over Java, hence it inherits all the object-oriented programming concepts of Java. In this article, we will see the difference between a "class" and an "object" in Kotlin.A "class" is a blueprint of a runtime entity and an "object" is its state, which includes both its behavior and state. In Kotlin, class declaration consists of a "class header" and a "class body" surrounded by curly braces, similar to Java. A general-purpose class definition looks like this −// class Header Class myClass { // class Body }There are ... Read More
Kotlin is a statistically typed language. It has different options to handle higher-order functions. Kotlin came up with a wonderful solution for higher-order functions by introducing inline functions.An Inline function is a kind of function that is declared with the keyword "inline" just before the function declaration. Once a function is declared inline, the compiler does not allocate any memory for this function, instead the compiler copies the piece of code virtually at the calling place at runtime.You should opt for an inline function in Kotlin in the following situations −When you need to access higher-order functions.When you need to ... Read More
In any programming language, generics are a powerful feature using which developers can create custom data type to manipulate a program in a different manner. There are many ways in which we can define generics in Kotlin.In this article, we will demonstrate the difference between "*" and "Any" keywords in Kotlin.When we define a collection with "*", it should contain the object of only that type. There should not be any mix and match between the data types inside a collection.If we use "Any", we can mix and match the data types, which means we can have multiple data types ... Read More
Kotlin is a cross-platform, statistically typed, general-purpose programming language. It is very popular among the developer community because of its interoperable nature with JVM. In the programming world sometimes it is required to check the type of an object to implement a business logic.Unlike Java, we don't have an "instance of" operator in Kotlin. However, we have an "is" operator in Kotlin for type checking and casting.ExampleThe following example demonstrates how the "is" operator works in Kotlin.fun main(args: Array) { val x: String = "TutorialsPoint" // checking the instance and matching the type ... Read More
"static" is a Java keyword that helps developers to define a class member, whereas the keyword "final" is used to declare a constant variable in Java. Once a variable is declared as static in Java, the value of the variable remains unchanged in every instance of the object. Similarly, once a variable is declared as final, its value cannot be altered.In this article, we will see how we can implement the same concept using Kotlin library function.Example - using companion objectA "companion object" is an object which is declared in the same file as a class. Both the class and ... Read More
A list is a type of collection in which data is stored in a sequential manner. We might come across a situation where the contents of one List have to be copied into another. In this article, we will see how to use Kotlin's built-in methods to clone a list.Example - Using toList()toList() is the basic method which can convert a collection into a list. We can use this method to clone an existing list too.fun main(args: Array) { val x = listOf(1, 2, 3, 4, 5, 6, 7, 8, 9) val copiedList=x.toList(); println("Given collection: " +x) ... Read More
Kotlin is a cross-platform and statically typed general-purpose programming language. Kotlin provides many optional methods to traverse through the collections. fold() and reduce() are two different methods, both are helpful for traversing a collection. In this article, we will see how and when to use these two methods.Example – fold()If we want to traverse through a collection serially, then we can use fold().fold() takes one initial value and one operation to perform the operation on the initial value.There are different kinds of fold(), for example, foldRight() folds from right to left. By default, fold() will traverse from left to right.The ... Read More
To find the moving standard deviation in a matrix is done in the same way as in a data frame, we just need to use the matrix object name in place of data frame name. Hence, we can make use of rollapply function of zoo package for this purpose.For example, if we have a matrix called M and we want to find the 2 moving standard deviations then we can use the below given command −rollapply(M,width=2,FUN=sd,fill=0,align="r")Example 1Following snippet creates a matrix −M1
Kotlin is very efficient in managing the memory. Unlike Java, developers in Kotlin need not have to manage the memory explicitly. We do have different kinds of memory management techniques and Try-with-resource is one of them. In Kotlin, we have a function called 'use' which takes the burden of managing the resources automatically. This is a part of std library function provided by Kotlin.As per Kotlin documentation, use() is defined as a generic extension on all closeable types. The implementation looks like this −public inline fun T.use(block: (T) -> R): R {}In the above function, the definition block is the ... Read More
To round the output of summary function in R, we can use digits argument while applying the summary function.For example, if we have a data frame called df then to find the summary statistics with two digits in the output we can use the below given command −summary(df, digits=2)Example 1Following snippet creates a dataframe −head(iris, 20) The following dataframe is created − Sepal.Length Sepal.Width Petal.Length Petal.Width Species 1 5.1 3.5 1.4 0.2 setosa 2 4.9 3.0 1.4 ... Read More
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP