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

Java Java Objects Delivering the MVP Forum

Julian Rios
Julian Rios
3,458 Points

Why is the args array used to pass in the firstName and lastName into the user?

I am not sure why this challenge used the args array to pass in the parameters for the user? I was trying to use a string with my first name and last name instead.

1 Answer

Linda de Haan
Linda de Haan
12,413 Points

I think this challenge wants to demonstrate that you can pass in values into the main methods parameters when running the program from the console. For example, if you run a program called Example with two parameters like this:

java Example hello world

hello and world are the parameters. The length of String[] args is now 2. So you can do:

if (args.length == 2) {
    System.out.println(args[0] + " " + args[1]);  // that prints 'hello world'
}
Julian Rios
Julian Rios
3,458 Points

That makes it so much clearer now thank you!