Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

274
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda