3

The main function in kotlin:

fun main(args : Array<String>) { 
  println("Hello, world!") 
}

Why is an Array passed in?

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
Zorgan
  • 8,227
  • 23
  • 106
  • 207

4 Answers4

6

The signature of main is based on what the Java Virtual Machine expects:

The method main must be declared public, static, and void. It must specify a formal parameter (§8.4.1) whose declared type is array of String. Therefore, either of the following declarations is acceptable:

public static void main(String[] args)
public static void main(String... args)

This is what the Kotlin compiler compiles your main function to. As of Kotlin 1.3, the explicit Array<String> can be omitted but will still be available in the byte code.

s1m0nw1
  • 76,759
  • 17
  • 167
  • 196
  • Thanks. But is there a reason why they chose `String[]` to be the type passed through? – Zorgan Jan 23 '19 at 10:58
  • You typically start a jvm program like this: `java [...] ClassX arg1 arg2 arg3` which will make the args available inside the `String []` of ClassX's main – s1m0nw1 Jan 23 '19 at 10:59
  • What other type would you use to pass the command line arguments? – yole Jan 23 '19 at 11:04
  • It's the same for other languages like C: https://www.cprogramming.com/tutorial/c/lesson14.html – s1m0nw1 Jan 23 '19 at 11:15
1

Collections were not there in JAVA 1. Hence, Array was the default choice. Also the arguments provided from Command Line are in string format, hence we use Array<String>. Kotlin, to maintain interoperability with JAVA, followed the same convention. But, with the update to Kotlin 1.3, that too has been omitted. Now you can use main() function without passing args:Array<String>.

ankuranurag2
  • 2,300
  • 15
  • 30
0

The array contains the command line arguments passed to your program.

You can also omit it, if you do not want to use them, i.e. you can also just write:

fun main() {
  println("Hello, world!")
}

I am already too late to link to the JLS for Test.main here (s1m0nw1 already did; I just prepared and went away ;-))

But nonetheless I want to add something regarding the choice for String (i.e. my opinion why String was chosen): it's probably the most common denominator for all the possible command line arguments. Any/Object is too broad; you can only pass numbers or strings in the command line to a program (pipes are handled differently). But having a number type is too narrow, so the only acceptable type that remains is String which can represent both. Still you need to parse numbers if you want to use them, but that's better then interpreting a string out of numbers ;-)

Roland
  • 22,259
  • 4
  • 57
  • 84
  • If the main function doesn't do anything then why is it needed? I assumed the main function executed the other functions. – Zorgan Jan 23 '19 at 10:41
  • No wait... the `main`-function is actually the entry point to your application ... or in other words: without the `main`-function your program will not run... with it you can for example just call: `java TestKt` which will then run your program (if the `main` was placed in a file named `Test.kt` and was compiled to a `class`-file, etc. pp) – Roland Jan 23 '19 at 10:47
  • so for example: if you "run" your file using your IDE, without a `main`-function it will not run at all (exceptions to this exist, e.g. you can also run tests and other things...) – Roland Jan 23 '19 at 10:49
0

Your are defining the entry point of an application. in this case, an application will start running from the main function. Passing the argument Array of type String.