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 Data Structures Organizing Data Splitting Strings

Diego Marrs
Diego Marrs
8,243 Points

I need a refresher...

Hello,

Could someone explain 'String[]' for me? I'm nut entirely sure how it works and when to use it when writing a program.

Thanks!

It represent a String array. Array is like a variable that stores multiple values. You will create an array like this: datatype[] arrayName = {"valu1","value2",......}; Ex: String[] names= {"Victor","Sam","Jack","Albert"};

1 Answer

String[] is a String array. If you are thinking about the "public static void main(String[] args)" line this is the method main with a built in String array that will allow you to enter in arguments straight from the command line. Like this:

public class Example{

    public static void main(String[] args){

         System.out.println("Here are the arguments that you passed in:");
         for(int i = 0; i < args.length; i++){
             System.out.println(args[i]);
         }
    }
}

When you go to run this class from the command line and wanted to pass in your first and last name as the arguments you would do it like this:

java Example John Doe

...and the output would look like this if you ran the above program:

Here are the arguments that you passed in:

John

Doe