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 […]
Tracing Eligibility How does the JVM know when an object is eligible for garbage collection? The JVM waits patiently and monitors each object until it determines that the code no longer needs that memory. An object will remain on the heap until it is no longer reachable. An object is no longer reachable when one […]
Reviewing Scope Got all that? Let’s review the rules on scope: Local variables: In scope from declaration to the end of the block Method parameters: In scope for the duration of the method Instance variables: In scope from declaration until the object is eligible for garbage collection Not sure what garbage collection is? Relax: that’s […]
Tracing Scope The exam will attempt to trick you with various questions on scope. You’ll probably see a question that appears to be about something complex and fails to compile because one of the variables is out of scope. Let’s try one. Don’t worry if you aren’t familiar with if statements or while loops yet. […]
var in the Real World The var keyword is great for exam authors because it makes it easier to write tricky code. When you work on a real project, you want the code to be easy to read. Once you start having code that looks like the following, it is time to consider using var: […]
Type Inference of var Now that you understand the local variable part, it is time to go on to what type inference means. The good news is that this also means what it sounds like. When you type var, you are instructing the compiler to determine the type for you. The compiler looks at the […]
Passing Constructor and Method Parameters Variables passed to a constructor or method are called constructor parameters or method parameters, respectively. These parameters are like local variables that have been pre- initialized. The rules for initializing constructor and method parameters are the same, so we focus primarily on method parameters. In the previous example, check is […]
Initializing Variables Before you can use a variable, it needs a value. Some types of variables get this value set automatically, and others require the programmer to specify it. In the following sections, we look at the differences between the defaults for local, instance, and class variables. Creating Local Variables A local variable is a […]
Declaring Multiple Variables You can also declare and initialize multiple variables in the same statement. How many vari-ables do you think are declared and initialized in the following example? void sandFence() { String s1, s2; String s3 = “yes”, s4 = “no”; } Four String variables were declared: s1, s2, s3, and s4. You can […]
Declaring Variables You’ve seen some variables already. A variable is a name for a piece of memory that stores data. When you declare a variable, you need to state the variable type along with giving it a name. Giving a variable a value is called initializing a variable. To initialize a variable, you just type […]