Chapter 1 - Java Building Blocks Flashcards Preview

Java OCA Exam > Chapter 1 - Java Building Blocks > Flashcards

Flashcards in Chapter 1 - Java Building Blocks Deck (8)
Loading flashcards...
1
Q

Order of Initialization

(2)

A
  1. Fields and instance initializer blocks are run in the order in which they appear in the file.
  2. The constructor runs after all fields and instance initializer blocks have run.
2
Q

Identifiers

A
  1. The name must begin with a letter or the symbol $ or _.
  2. Subsequent characters may also be numbers.
  3. 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.
3
Q

Local Variables

(3)

A
  1. A local variable is a variable defined within a block (eg. method).
  2. Local variables must be initialized before use.
  3. They do not have a default value and contain garbage data until initialized.
4
Q

Java primitive types

(8)

A

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

5
Q

Ordering Elements in a Class

(6)

A
  1. Package declaration - no - first line
  2. Import statements - no - immediately after package
  3. Class declaration - yes - immediately after import
  4. Field declarations - no - anywhere inside class
  5. Method declarations - no - anywhere inside class
  6. Initializers - no - anywhere inside class
6
Q

Garbage Collection

(2)

A
  • The object no longer has any references pointing to it.
  • All references to the object have gone out of scope.
7
Q

finalize()

A

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.

8
Q

Benefits of Java

(6)

A
  1. 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.
  2. 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.
  3. 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.
  4. 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.
  5. 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.
  6. 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.