new
operator, and many other facets of the Java language. Look at this code and see whether you can figure out when each object first becomes eligible for garbage collection:
1: public class Scope { 2: public static void main(String[] args) { 3: String one, two; 4: one = new String("a"); 5: two = new String("b"); 6: one = two; 7: String three = one; 8: one = null; 9: } }
When you are asked a question about garbage collection on the exam, we recommend that you draw what's going on. There's a lot to keep track of in your head, and it's easy to make a silly mistake trying to hold it all in your memory. Let's try it together now. Really. Get a pencil and paper. We'll wait.
Got that paper? Okay, let's get started. On line 3, write one and two (just the words—no need for boxes or arrows since no objects have gone on the heap yet). On line 4, we have our first object. Draw a box with the string "a" in it, and draw an arrow from the word one
to that box. Line 5 is similar. Draw another box with the string "b" in it this time and an arrow from the word two
. At this point, your work should look like Figure 1.4.
FIGURE 1.4 Your drawing after line 5
On line 6, the variable one
changes to point to "b"
. Either erase or cross out the arrow from one
and draw a new arrow from one
to "b"
. On line 7, we have a new variable, so write the word three and draw an arrow from three
to "b"
. Notice that three
points to what one
is pointing to right now and not what it was pointing to at the beginning. This is why you are drawing pictures. It's easy to forget something like that. At this point, your work should look like Figure 1.5.
FIGURE 1.5 Your drawing after line 7
Finally, cross out the line between one
and "b"
since line 8 sets this variable to null
. Now, we were trying to find out when the objects were first eligible for garbage collection. On line 6, we got rid of the only arrow pointing to "a"
, making that object eligible for garbage collection. "b"
has arrows pointing to it until it goes out of scope. This means "b"
doesn't go out of scope until the end of the method on line 9.
Code Formatting on the Exam
Not all questions will include package declarations and imports. Don't worry about missing package statements or imports unless you are asked about them. The following are common cases where you don't need to check the imports:
Code that begins with a class name
Code that begins with a method declaration
Code that begins with a code snippet that would normally be inside a class or method
Code that has line numbers that don't begin with 1
You'll see code that doesn't have a method. When this happens, assume any necessary plumbing code like the main()
method and class definition were written correctly. You're just being asked if the part of the code you're shown compiles when dropped into valid surrounding code. Finally, remember that extra whitespace doesn't matter in Java syntax. The exam may use varying amounts of whitespace to trick you.
Summary
Java begins program execution with a main()
method. The most common signature for this method run from the command line is public static void main(String[] args)
. Arguments are passed in after the class name, as in java NameOfClass firstArgument
. Arguments are indexed starting with 0
.
Java code is organized into folders called packages. To reference classes in other packages, you use an import
statement. A wildcard ending an import
statement means you want to import
all classes in that package. It does not include packages that are inside that one. The package java.lang
is special in that it does not need to be imported.
For some class elements, order matters within the file. The package statement comes first if present. Then come the import
statements if present. Then comes the class declaration. Fields and methods are allowed to be in any order within the class.
Primitive types are the basic building blocks of Java types. They are assembled into reference types. Reference types can have methods and be assigned a null
value. Numeric literals are allowed to contain underscores (_
) as long as they do not start or end the literal and are not next to a decimal point (.
). Wrapper classes are reference types, and there is one for each primitive. Text blocks allow creating a String
on multiple lines using """
.
Declaring a variable involves stating the data type and giving the variable a name. Variables that represent fields in a class are automatically initialized to their corresponding 0
, null
, or false
values during object instantiation. Local variables must be specifically initialized before they can be used. Identifiers may contain letters, numbers, currency symbols, or _
. Identifiers may not begin with numbers. Local variable declarations may use the var
keyword instead of the actual type. When using var
, the type is set once at compile time and does not change.
Scope refers to that portion of code where a variable can be accessed. There are three kinds of variables in Java, depending on their scope: instance variables, class variables, and local variables. Instance variables are the non-static
fields of your class. Class variables are the static
fields within a class. Local variables are declared within a constructor, method, or initializer block.
Constructors create Java objects. A constructor is a method matching the class name and omitting the return type. When an object is instantiated, fields and blocks of code are initialized first. Then the constructor is run. Finally, garbage collection is responsible for removing objects from memory when they can never be used again. An object becomes eligible for garbage collection when there are no more references to it or its references have all gone out of scope.
Exam Essentials
Be able to write code using a main() method. A main()
method is usually written as public static void main(String[] args
). Arguments are referenced starting with args[0]
. Accessing an argument that wasn't passed in will cause the code to throw an exception.
Understand the effect of using packages and imports. Packages contain Java classes. Classes can be imported by class name or wildcard. Wildcards do not look at subdirectories. In the event of a conflict, class name imports take precedence. Package and import
statements are optional. If they are present, they both go before the class declaration in that order.
Be able to recognize a constructor. A constructor has the same name as the class. It looks like a method without a return type.
Be able to identify legal and illegal declarations and initialization. Multiple variables can be declared and initialized in the same statement when they share a type. Local variables