OCP Oracle Certified Professional Java SE 17 Developer Study Guide. Jeanne Boyarsky
Читать онлайн книгу.a different version of Java!
You can download Oracle's JDK on the Oracle website, using the same account you use to register for the exam. There are many JDKs available, the most popular of which, besides Oracle's JDK, is OpenJDK.
Many versions of Java include preview features that are off by default but that you can enable. Preview features are not on the exam. To avoid confusion about when a feature was added to the language, we will say “was officially introduced in” to denote when it was moved out of preview.
Check Your Version of Java
Before we go any further, please take this opportunity to ensure that you have the right version of Java on your path.
javac -version java -version
Both of these commands should include version number 17.
Understanding the Class Structure
In Java programs, classes are the basic building blocks. When defining a class, you describe all the parts and characteristics of one of those building blocks. In later chapters, you see other building blocks such as interfaces, records, and enums.
To use most classes, you have to create objects. An object is a runtime instance of a class in memory. An object is often referred to as an instance since it represents a single representation of the class. All the various objects of all the different classes represent the state of your program. A reference is a variable that points to an object.
In the following sections, we look at fields, methods, and comments. We also explore the relationship between classes and files.
Fields and Methods
Java classes have two primary elements: methods, often called functions or procedures in other languages, and fields, more generally known as variables. Together these are called the members of the class. Variables hold the state of the program, and methods operate on that state. If the change is important to remember, a variable stores that change. That's all classes really do. It's the programmer's job to create and arrange these elements in such a way that the resulting code is useful and, ideally, easy for other programmers to understand.
The simplest Java class you can write looks like this:
1: public class Animal { 2: }
Java calls a word with special meaning a keyword, which we've marked bold in the previous snippet. Throughout the book, we often bold parts of code snippets to call attention to them. Line 1 includes the public
keyword, which allows other classes to use it. The class
keyword indicates you're defining a class. Animal
gives the name of the class. Granted, this isn't an interesting class, so let's add your first field.
1: public class Animal { 2: String name; 3: }
On line 2, we define a variable named name
. We also declare the type of that variable to be String
. A String
is a value that we can put text into, such as "this is a string"
. String
is also a class supplied with Java. Next we can add methods.
1: public class Animal { 2: String name; 3: public String getName() { 4: return name; 5: } 6: public void setName(String newName) { 7: name = newName; 8: } 9: }
On lines 3–5, we define a method. A method is an operation that can be called. Again, public
is used to signify that this method may be called from other classes. Next comes the return type—in this case, the method returns a String
. On lines 6–8 is another method. This one has a special return type called void. The void
keyword means that no value at all is returned. This method requires that information be supplied to it from the calling method; this information is called a parameter. The setName()
method has one parameter named newName
, and it is of type String
. This means the caller should pass in one String
parameter and expect nothing to be returned.
The method name and parameter types are called the method signature. In this example, can you identify the method name and parameters?
public int numberVisitors(int month) { return 10; }
The method name is numberVisitors
. There's one parameter named month
, which is of type int
, which is a numeric type. Therefore, the method signature is numberVisitors(int)
.
Comments
Another common part of the code is called a comment. Because comments aren't executable code, you can place them in many places. Comments can make your code easier to read. While the exam creators are trying to make the code harder to read, they still use comments to call attention to line numbers. We hope you use comments in your own code. There are three types of comments in Java. The first is called a single-line comment:
// comment until end of line
A single-line comment begins with two slashes. The compiler ignores anything you type after that on the same line. Next comes the multiple-line comment:
/* Multiple * line comment */
A multiple-line comment (also known as a multiline comment) includes anything starting from the symbol /*
until the symbol */
. People often type an asterisk (*
) at the beginning of each line of a multiline comment to make it easier to read, but you don't have to. Finally, we have a Javadoc comment:
/** * Javadoc multiple-line comment * @author Jeanne and Scott */
This comment is similar to a multiline comment, except it starts with /**
. This special syntax tells the Javadoc tool to pay attention to the comment. Javadoc comments have a specific structure that the Javadoc tool knows how to read. You probably won't see a Javadoc comment on the exam. Just remember it exists so you can read up on it online when you start writing programs for others to use.
As a bit of practice, can you identify which type of comment each of the following six words is in? Is it a single-line or a multiline comment?
/* * // anteater */ // bear // // cat // /* dog */ /* elephant */ /* * /* ferret */ */
Did you look closely? Some of these are tricky. Even though comments technically aren't on the exam, it is good to practice looking at code carefully.
Okay, on to the answers. The comment containing anteater
is in a multiline comment. Everything between /*
and */
is part of a multiline comment—even if it includes a single-line comment within it! The comment containing bear
is your basic single-line comment. The comments containing cat
and dog
are also single-line comments. Everything from //
to the end of the line is part of the comment, even if it is another type of comment. The comment containing elephant
is your basic multiline comment, even though it only takes up one line.
The line with ferret
is interesting in that it doesn't compile. Everything from the first /*
to the first */
is part of the comment, which means the compiler sees something like this:
/* */ */
We have a problem. There is an extra */
. That's not valid syntax—a fact the compiler is happy to inform you about.
Classes