Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
dongji cui
2,464 PointsAssuming there is a compiled java class named HelloWorld with a static method named main that accepts an Array of String
What would be stored in args[1] if we ran java HelloWorld Luke Skywalker
why the answer is Skywalker?
2 Answers
jacobproffer
24,604 PointsHey Dongji,
Array indices start at zero. This is the basic practice across most, if not all, programming languages. So, if you had an array of Strings with two elements, the first element would be 0 and the second element would be 1, and so on.
For your question in specific, the second element of your String array appears to be 'Skywalker'.
I see how this can be confusing but let's take a look at this in practice:
String[] example = new String[2]; //This creates a new array of Strings with two elements
example[0] = Luke; //This sets the first element of the array to hold the String 'Luke'
example[1] = Skywalker; //This sets the second element of the array to hold the String 'Skywalker'
And you follow this format for creating arrays of integers, doubles, booleans, etc.
Hope this helps a bit.
Best,
Jacob
Dan Johnson
40,533 PointsYou have the part that invokes the program:
java HelloWorld
which leaves
Luke Skywalker
As the arguments. Since they're passed in as an array, and arrays are zero indexed you have:
args[0] = "Luke"
args[1] = "Skywalker"