Can anyone help me in this : 'Any' keyword in kotlin | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Can anyone help me in this : 'Any' keyword in kotlin

Can you help me out in this... If Any keyword allows anything than why this doesn't works.. fun takeAny(arg: Any) { println(arg); } fun main(args: Any) { takeAny("Some string"); takeAny(42); } /* Change : Array<String> to Any*/ https://www.sololearn.com/learn/8793/?ref=app

2nd Apr 2020, 6:24 AM
Akshay Jain
Akshay Jain - avatar
6 Answers
+ 4
It works fine. But, JVM looks for main with Array<String> parameter and fails to find it. You can overload main same as any function and call the Any one from the Array<String> one to prove that fact.
3rd Apr 2020, 11:35 AM
John Wells
John Wells - avatar
+ 3
To be a valid main entry point, the prototype must be using Array<String>. You can change args to any name. But, the function will only be found with that type on it's argument. This is a limitation in the Java Virtual Machine (JVM) code.
3rd Apr 2020, 2:16 AM
John Wells
John Wells - avatar
+ 2
Thanks for answering 😊
3rd Apr 2020, 11:37 AM
Akshay Jain
Akshay Jain - avatar
+ 2
Any is not a keyword it is a super class of classes available in kotlin and if you creat any class for example (MyClass) than by default it will be extends (Any) class
3rd Apr 2020, 11:56 AM
Sumit Programmer😎😎
Sumit Programmer😎😎 - avatar
+ 2
ohh.. it wasn't mentioned anywhere. Thanks for telling
3rd Apr 2020, 11:59 AM
Akshay Jain
Akshay Jain - avatar
+ 1
John Wells so 'Any' Keyword not works for predefined functions..Is it??
3rd Apr 2020, 9:02 AM
Akshay Jain
Akshay Jain - avatar