Java for Complete Beginners (Video), Part 11: Arrays of Strings

A tutorial on String arrays in Java, plus another way to iterate through an array, and more stuff on references vs. values.

When the video is running, click the maximize button in the lower-right-hand corner to make it full screen.

Code for this tutorial:


public class App {


	public static void main(String[] args) {
		
		// Declare array of (references to) strings.
		String[] words = new String[3];
		
		// Set the array elements (point the references
		// at strings)
		words[0] = "Hello";
		words[1] = "to";
		words[2] = "you";
		
		// Access an array element and print it.
		System.out.println(words[2]);
		
		// Simultaneously declare and initialize an array of strings
		String[] fruits = {"apple", "banana", "pear", "kiwi"};
		
		// Iterate through an array
		for(String fruit: fruits) {
			System.out.println(fruit);
		}
		
		// "Default" value for an integer
		int value = 0;
		
		// Default value for a reference is "null"
		String text = null;
		
		System.out.println(text);
		
		// Declare an array of strings
		String[] texts = new String[2];
		
		// The references to strings in the array
		// are initialized to null.
		System.out.println(texts[0]);
		
		// ... But of course we can set them to actual strings.
		texts[0] = "one";
	}

}

you
apple
banana
pear
kiwi
null
null

 

Learn to program Java Swing with my complete video course – desktop programming and applets. Includes 7 free videos. Click here for details.

All pages on this site are copyright © 2013 John W. Purcell

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Posted in Java, Java Video Tutorials (Beginners) | Tagged , , |