Downloading a JDK – Building Blocks – 1Z0-829 Study Guide

Downloading a JDK

Every six months, Oracle releases a new version of Java. Java 17 came out in September 2021. This means that Java 17 will not be the latest version when you download the JDK to study for the exam. However, you should still use Java 17 to study with since this is a Java 17 exam. The rules and behavior can change with later versions of Java. You wouldn’t want to get a question wrong because you studied with 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 ver-sion 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 represen-tation 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 {
  • }

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 {
  • String name;
  • }

The line numbers aren’t part of the program; they’re just there to make the code easier to talk about.

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 {
  • String name;
  • public String getName() {
  • return name;
  • }
  • public void setName(String newName) {
  • name = newName;
  • }
  • }

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 param-eter 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).