Java Lesson 4: Arrays and Strings Arrays are a logical way to hold collections of related data. Whenever you declare an array, you must use the new operator to actually create it. Otherwise you will get a dreaded NullPointerException. Arrays can contain other arrays. Nesting them like this gives rise to two-dimensional arrays, three-dimensional arrays, and so on. Unlike C++, when an array contains other arrays, not all of the nested arrays have to have the same length. These are sometimes called triangular arrays (as opposed to C++'s square arrays). Strings are the first true objects we'll look at. They can be concatenated with the built-in + operator. A String variable holds a reference to a String, not the String itself. The reference is a memory address. Thus, you shouldn't compare two Strings using the == operator - you'll only find out if they point to the same location in memory, not whether they contain the same text. The String class contains methods to search and compare Strings. Once created, a String cannot be modified. If you need to modify a String, you should use a StringBuffer object instead, which has in interface similar to that of the String class. Essential Points: 1. You use an array to hold multiple values of the same type, identified through a single variable name. 2. You reference an individual element of an array by using an index value of type int. The index value for an array element is the offset of the element from the first element in the array. 3. An array element can be used in the same way as a single variable of the same type. 4. You can obtain the number of elements in an array by using the length member of the array object. 5. An array element can also contain an array, so you can define arrays of arrays, or arrays of arrays of arrays ... 6. A String object stores a fixed character string that can't be changed. However, you can assign a given String variable to a different String object. 7. You can obtain the number of characters stored in a String object by using the length() method for the object. 8. The String class provides methods for joining, searching, and modifying strings -- the modifications being achieved by creating a new String object. 9. A StringBuffer object can store a string of characters that can be modified. 10. You can get the number of characters stored in a StringBuffer object by calling the length() method, and you can find out what the current maximum number of characters it can store by using its capacity() method. 11. You can change both the length and the capacity for a StringBuffer object. 12. The StringBuffer class contains a variety of methods for modifying StringBuffer objects. 13. You can create a String object from a StringBuffer object by using the toString() method of the StringBuffer object.