Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays in Java  RSS feed

 
Tharindu Priyath
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What will be printed out if this code is run with the
following command line?

java MyProg good morning

class MyProg{
public static void main(String argv[]){
System.out.println(argv[2])
}
}

1. myprog 2. good 3. morning

How to assign values for argv array?
How to print these words?
 
Greg Charles
Sheriff
Posts: 3015
12
Firefox Browser IntelliJ IDE Java Mac Ruby
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems you've answered your first question yourself. As to how you assign variables to argv (which is conventionally args in Java), you generally don't. The runtime system builds this array from the parameters you pass to your program. You prin,t to the console at least, with System.out.print(), but you seem to have discovered that too, so maybe I'm not understanding your question.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tharindu Priyath wrote:What will be printed out if this code is run with the following command line?

I believe the easiest way to find out is to actually do it yourself. That should be pretty straightfoward.
Of course, you actually CAN'T run the above program, as it won't compile.
Tharindu Priyath wrote:How to assign values for argv array?

The only way to do it is to put them in your command-line call. It's not really done programatically, but is a feature of Java.
Tharindu Priyath wrote:How to print these words?

Which words are you talking about?
 
Campbell Ritchie
Marshal
Posts: 55681
162
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tharindu Priyath wrote: . . .
1. myprog 2. good 3. morning . . .
That sort of question should always have at least three other possible answers:
  • 4: Fails to compile.
  • 5: Compiles, but fails to run.
  • 6: Runs, but does not complete normally. In Java that means an Exception.
  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!