Before we study other features of object-oriented programming (OOP), let’s examine several important classes that are commonly used in Java. These classes are included in the Java core libraries that come with the JDK. Mastering them will help you understand the examples that accompany the next OOP lessons.
The most prominent class of all is definitely java.lang.Object. However, it is hard to talk about this class without first covering inheritance, which we will do in Chapter 6, “Inheritance.” Therefore, java.lang.Object is only discussed briefly in this chapter. Right now we will concentrate on classes that you can use in your programs. We’ll start with java.lang.String and other types of strings: java.lang.StringBuffer and java.lang.StringBuilder. Then, we’ll discuss arrays and the java.lang.System class. The java.util.Scanner class is also included here because it provides a convenient way to take user input.
There are also two concepts explained at the end of the chapter, boxing/unboxing and varargs. They are explained after the core classes because the latter are the prerequisite for these concepts.
Note
When describing a method in a Java class, presenting the method signature is always helpful. A method often takes as parameters objects whose classes belong to different packages than the method’s class. Or, it may return a type from a different package than its class. For clarity, fully qualified names will be used for classes from different packages. For example, here is the signature of the toString method of java.lang.Object:
public String toString()
A fully qualified name for the return type is not necessary because the return type String is part of the same package as
java.lang.Object. On the other hand, the signature of the toString method in java.util.Scanner uses a fully qualified name because
the Scanner class is part of a different package (java.util).
public java.lang.String toString()