Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

279
Views
Why is Array<String> the chosen parameter type for the main function in kotlin?

The main function in kotlin:

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

Why is an Array passed in?

over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

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.

over 4 years ago · Santiago Trujillo Report

0

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>.

over 4 years ago · Santiago Trujillo Report

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 ;-)

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!