4.7. Summary

In this chapter, you learned about the String class, its properties, and its methods. Because this is one of the most frequently used classes in Java, I’ll reiterate that a good understanding of this class in terms of why its methods behave in a particular manner will go a long way toward helping you successfully complete the OCA Java SE 8 Programmer I exam.

You learned how to initialize String variables using the operator new and the assignment operator (=) with String literals. You also learned the differences between how String objects are stored using these two approaches. If you use the assignment operator to initialize your String variables, they’re stored in a common pool of String objects (also known as the String constant pool) that can be used by others. This storage is possible because String objects are immutable—that is, their values can’t be changed.

You learned how a char array is used to store the value of a String object. This helps explain why the methods charAt(), indexOf(), and substring() search for the first character of a String at position 0, not position 1. We also reviewed the methods replace(), trim(), and substring(), which seem to modify the value of a String but will never be able to do so because String objects are immutable. You also learned the methods length(), startsWith(), and endsWith().

Because not all operators can be used with Strings, you learned about the ones that can be used with String: +, +=, ==, and !=. You also learned that the equality of Strings can be determined using the method equals. By using the operator ==, you can only determine whether both of the variables are referring to the same object; it doesn’t compare the values stored by Strings. As with all the other object types, you can assign null to a String variable.

You worked with the class StringBuilder, which is defined in the package java.lang and is used to store a mutable sequence of characters. The class StringBuilder is usually used to store a sequence of characters that needs to be modified often—such as when you’re building a query for database applications. Like the String class, StringBuilder also uses a char array to store its characters. Many of the methods defined in the class StringBuilder work exactly as defined by the class String, such as the methods charAt, indexOf, substring, and length. The append method is used to add characters to the end of a StringBuilder object. The insert method is another important StringBuilder method that’s used to insert either single or multiple characters at a specified position in a StringBuilder object. The class StringBuilder offers the same functionality as offered by the class StringBuffer, minus the additional feature of methods that are synchronized where needed.

An array is an object that stores a collection of values. An array can store a collection of primitive data types or a collection of objects. You can define one-dimensional and multidimensional arrays. A one-dimensional array is an object that refers to a collection of scalar values. A two-dimensional (or more) array is referred to as a multidimensional array. A two-dimensional array refers to a collection of objects, where each of the objects is a one-dimensional array. Similarly, a three-dimensional array refers to a collection of two-dimensional arrays, and so on. Arrays can be declared, allocated, and initialized in a single step or in multiple steps. A two-dimensional array doesn’t need to be symmetrical, and each of its rows can define different numbers of members. You can define arrays of primitives, interfaces, abstract classes, and concrete classes. All arrays are objects and can access the variable length and methods inherited from the class java.lang.Object.

ArrayList is a resizable array that offers the best combination of features offered by an array and the List data structure. You can add objects to an ArrayList using the method add. You can access the objects of an ArrayList by using an enhanced for loop or by using the get() method or an iterator. An ArrayList preserves the order of insertion of its elements. ListIterator, Iterator, and the enhanced for loop will return the elements in the order in which they were added to the ArrayList. You can modify the elements of an ArrayList using the method set. You can remove the elements of an ArrayList by using the method remove, which accepts the element position or an object. You can also add multiple elements to an ArrayList by using the method addAll. The method clone defined in the class ArrayList returns a shallow copy of this ArrayList instance. Shallow copy means that the method creates a new instance of the ArrayList to be cloned, but the ArrayList elements aren’t copied.

You can compare the objects of your class by overriding the equals method. The equals method is defined in the class java.lang.Object, which is the base class of all classes in Java. The default implementation of the method equals only compares the object references for equality. Because instance variables are used to store the state of an object, it’s common to compare the values of these variables to determine whether two objects should be considered equal in the equals method. The Java API documentation defines a contract for the equals method. In the exam, for a given definition of the method equals, it is important to note the differences between an equals method that compiles successfully, one that fails compilation, and one that doesn’t follow the contract.

The Date and Time API in Java 8 simplifies how you work with the date and time classes. You worked with LocalDate, which is used to store only dates of the format 2016-08-14. LocalTime stores time in the format 14:09:65:23 (hours:minutes:seconds:nanoseconds). The class LocalDateTime stores both date and time. The class Period is used to work with a duration of, say, a period of 4 months or 4 days. The class DateTimeFormatter is used to format date and time using a predefined or custom format.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.33.235