String arrays and how to use them in Java

Java is very interesting because you can easily declare an array and use a string declaration throughout your entire program with a simple array reference.

For example, let’s say your favorite fashion models are Naomi Campbell, Kristy Turlington and Kate Moss. If you want to initialize a string variable SuperModel to start with the first model in your array, your Java code should look something like this:

String[] models = { “Naomi Campbell”,
“Cristy Turlington”,
“Kate Moss”
};
String SuperModel = models[0];

The compiler will see that with numbers starting from 0, 1, 2 that the first model listed in the array is Naomi Campbell so String SuperModel is set to the first element in the array. Can you guess if models[2], which model would initialize String SuperModel?

 

Leave a Comment

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s