Order of Initialization
- Fields and instance initializer blocks are run in the order in which they appear in the file.
- The constructor runs after all fields and instance initializer blocks have run.
- The name must begin with a letter or the symbol $ or _.
- Subsequent characters may also be numbers.
- You cannot use the same name as a Java reserved word. As you might imagine, a reserved word is a keyword that Java has reserved so that you are not allowed to use it. Remember that Java is case sensitive, so you can use versions of the keywords that only differ in case. Please don’t, though.
- A local variable is a variable defined within a block (eg. method).
- Local variables must be initialized before use.
- They do not have a default value and contain garbage data until initialized.
Java primitive types
Keyword - Type - Example
- boolean - true or false - true
- byte - 8-bit integral - 100
- short - 16-bit integral - 10’000
- int - 32-bit integral - 1’000’000
- long - 64-bit integral - 10’000’000’000L
- float - 32-bit floating-point - 123.456f
- double - 64-bit floating-point - 123.456
- char - 16-bit integral - ‘a’ (97)
true or false
8-bit integral value 16-bit integral value 32-bit integral value 64-bit integral value
Ordering Elements in a Class
- Package declaration - no - first line
- Import statements - no - immediately after package
- Class declaration - yes - immediately after import
- Field declarations - no - anywhere inside class
- Method declarations - no - anywhere inside class
- Initializers - no - anywhere inside class
- The object no longer has any references pointing to it.
- All references to the object have gone out of scope.
Java allows objects to implement a method called finalize() that might get called. This method gets called if the garbage collector tries to collect the object. If the garbage collector doesn’t run, the method doesn’t get called. If the garbage collector fails to collect the object and tries to run it again later, the method doesn’t get called a second time.
In practice, this means you are highly unlikely to use it in real projects. Luckily, there isn’t much to remember about finalize() for the exam. Just keep in mind that it might not get called and that it de nitely won’t be called twice.
Benefits of Java
- Object Oriented Java is an object-oriented language, which means all code is de ned in classes and most of those classes can be instantiated into objects. We’ll discuss this more throughout the book. Many languages before Java were procedural, which meant there were routines or methods but no classes. Another common approach is functional programming. Java allows for functional programming within a class, but object oriented is still the main organization of code.
- Encapsulation Java supports access modifiers to protect data from unintended access and modi cation. Most people consider encapsulation to be an aspect of object-oriented languages. Since the exam objectives call attention to it specifically, so do we.
- Platform Independent Java is an interpreted language because it gets compiled to bytecode. A key bene t is that Java code gets compiled once rather than needing to be recompiled for different operating systems. This is known as “write once, run everywhere.” On the OCP exam, you’ll learn that it is possible to write code that does not run every- where. For example, you might refer to a le in a speci c directory. If you get asked on the OCA exam, the answer is that the same class les run everywhere.
- Robust One of the major advantages of Java over C++ is that it prevents memory leaks. Java manages memory on its own and does garbage collection automatically. Bad memory management in C++ is a big source of errors in programs.
- Simple Java was intended to be simpler than C++. In addition to eliminating pointers, it got rid of operator overloading. In C++, you could write a + b and have it mean almost anything.
- Secure Java code runs inside the JVM. This creates a sandbox that makes it hard for Java code to do evil things to the computer it is running on.