Defining Text Blocks Earlier we saw a simple String with the value “hello”. What if we want to have a String with something more complicated? For example, let’s figure out how to create a String with this value: “Java Study Guide” by Scott & Jeanne Building this as a String requires two things you haven’t […]
Distinguishing between Primitives and Reference Types There are a few important differences you should know between primitives and reference types. First, notice that all the primitive types have lowercase type names. All classes that come with Java begin with uppercase. Although not required, it is a standard practice, and you should follow this convention for […]
Writing Literals There are a few more things you should know about numeric primitives. When a number is present in the code, it is called a literal. By default, Java assumes you are defining an int value with a numeric literal. In the following example, the number listed is bigger than what fits in an […]
Understanding Data Types Java applications contain two types of data: primitive types and reference types. In this sec-tion, we discuss the differences between a primitive type and a reference type. Using Primitive Types Java has eight built-in data types, referred to as the Java primitive types. These eight data types represent the building blocks for […]
Executing Instance Initializer Blocks When you learned about methods, you saw braces ({}). The code between the braces (some-times called “inside the braces”) is called a code block. Anywhere you see braces is a code block. Sometimes code blocks are inside a method. These are run when the method is called. Other times, code blocks […]
Creating Objects Our programs wouldn’t be able to do anything useful if we didn’t have the ability to create new objects. Remember that an object is an instance of a class. In the following sections, we look at constructors, object fields, instance initializers, and the order in which values are initialized. Calling Constructors To create […]
Creating a JAR File Some JARs are created by others, such as those downloaded from the Internet or created by a teammate. Alternatively, you can create a JAR file yourself. To do so, you use the jar command. The simplest commands create a jar containing the files in the current directory. You can use the […]
Compiling to Another Directory By default, the javac command places the compiled classes in the same directory as the source code. It also provides an option to place the class files into a different directory. The -d option specifies this target directory. Java options are case sensitive. This means you cannot pass -D instead of […]
Creating a New Package Up to now, all the code we’ve written in this chapter has been in the default package. This is a special unnamed package that you should use only for throwaway code. You can tell the code is in the default package, because there’s no package name. On the exam, you’ll see […]
Redundant Imports Wait a minute! We’ve been referring to System without an import every time we printed text, and Java found it just fine. There’s one special package in the Java world called java.lang. This package is special in that it is automatically imported. You can type this package in an import statement, but you […]