- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Difference between "*" and "Any" in Kotlin generics
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 in a collection.
Example - usage of "*"
fun main(args: Array<String>) { val num = arrayOf(1, 2, 3, 4) ArrayUtil().findElement(num,2) } class ArrayUtil(){ // passing generic array to a function fun findElement(array:Array<*>,element:Int){ for(i in array.indices){ if(array[i]==element){ println("Element found at " +i) } } } }
Output
It will generate the following output:
Element found at 1
Example – Use of "Any"
fun main(args: Array<String>) { var objet = genericsExample<String>("JAVA") var objet1 = genericsExample<Int>(10) } class genericsExample<Any>(input:Any?) { init { println("I am getting called with the value "+input) } }
Output
It will generate the following output
I am getting called with the value JAVA I am getting called with the value 10
- Related Articles
- Difference between "fold" and "reduce" in Kotlin
- Difference between isNullOrEmpty and isNullOrBlank in Kotlin
- Difference between List and Array types in Kotlin
- Equality checks in Kotlin (Difference between "==" and "===" Operators)
- Difference between Java and Kotlin in Android with Examples
- Difference between a "class" and an "object" in Kotlin
- What's the difference between "!!" and "?" in Kotlin?
- What is the difference between "const" and "val" in Kotlin?
- What is the difference between "var" and "val" in Kotlin?
- Generics vs non-generics in C#
- What is the difference between All and Any in C# Linq?
- Explain generics and How to apply method or variable generics in Swift?
- What is the difference between artificial satellite and natural satellite ? State any 5 difference between them.
- Find minimum difference between any two element in C++
- Generics in C#

Advertisements