Institute of Computer Science
  1. Courses
  2. 2019/20 spring
  3. Object-Oriented Programming (Narva College) (LTAT.NR.003)
ET
Log in

Object-Oriented Programming (Narva College) 2019/20 spring

  • Home
  • Materials
  • Grading
  • Java Glossary
  • Cheat sheet (S1-S6)
  • Source Example
  • Links
Chapter 2

Command-line arguments

Java's main(String[] args) method takes a String array named args as an argument. The elements of this array are known as command-line arguments, which correspond to the arguments provided by the user when the Java program is executed. For example, a Java program called HelloWorld can be invoked with additional command-line arguments as follows (in a "cmd" shell):

 java HelloWorld Tartu Tallinn 180

Each element in the array args is a String (including, for example, "180"). If we need integers or doubles as an input, the conversion is needed:

 int distance = Integer.parseInt(args[2]);
 double distance = Double.parseDouble(args[2]);

In the example, the first line converts the third element of the array args (string "180") into an integer and the second line converts the value into a double.

In IntelliJ, the command-line arguments can be input into the Program arguments (Run-menu - > Edit Configuration) like this:

Chapter 2
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment