Skip to content

Instantly share code, notes, and snippets.

@felansu
Last active June 2, 2017 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save felansu/534fa676dc4bf964363fd1af534e5d6c to your computer and use it in GitHub Desktop.
Save felansu/534fa676dc4bf964363fd1af534e5d6c to your computer and use it in GitHub Desktop.
Java OCA SE 8
  • The full declaration of a method is called a method signature.

      Example: public static void main
    
  • If you do have a public class, it needs to match the filename.

  • To compile Java code, the file must have the extension .java.

  • Bytecode consists of instructions that the JVM knows how to execute.

  • Notice that we must omit the .class extension to run Zoo.java because the period has a reserved meaning in the JVM.

      $ javac Zoo.java
      $ java Zoo
    
  • Each file can contain only one class.

  • The filename must match the class name, including case, and have a .java extension.

  • The keyword public is what’s called an access modifier. It declares this method’s level of exposure to potential callers in the program. Naturally, public means anyplace in the program.

  • The keyword static binds a method to its class so it can be called by just the class name, as in, for example, Zoo.main().

  • If a main() method isn’t present in the class we name with the .java executable, the process will throw an error and terminate. Even if a main() method is present, Java will throw an exception if it isn’t static. A nonstatic main() method might as well be invisible from the point of view of the JVM.

  • The keyword void represents the return type. In general, it’s good practice to use void for methods that change an object’s state. In practice, you can write String[] args, String args[] or String... args; the compiler accepts any of these. The variable name args hints that this list contains values that were read in (arguments) when the JVM started.

  • An array is a fixed-size list of items that are all of the same type. The characters ... are called varargs (variable argument lists).

  • If you want spaces inside an argument, you need to use quotes java Zoo "San Diego" Zoo

  • You do not need to have a JDK to run the code—a JRE is enough.

  • Java calls more detailed packages child packages.

  • You’ll see package names on the exam that don’t follow this convention. The exam may try to trick you with invalid variable names. Luckily, it doesn’t try to trick you by giving invalid package names.

  • Classes in the same package are often imported together. You can use a shortcut to import all the classes in a package:

      import java.util.*; // imports java.util.Random among other things
      
      public class ImportExample {
          public static void main(String[] args) {
              Random r = new Random();
              System.out.println(r.nextInt(10));
          }
      }
    
  • You don’t need to memorize this package for the OCA exam (but you should know it for the OCP exam).

  • When the class is found in multiple packages, Java gives you the compiler error: The type Date is ambiguous

  • Default package: On the exam, you’ll see the default package used a lot to save space in code listings. In real life, always name your packages to avoid naming conflicts and to allow others to reuse your code.

  • Not all questions will include the imports. If the exam isn’t asking about imports in the question, it will often omit the imports to save space. You’ll see examples with line numbers that don’t begin with 1 in this case.

  • Remember that an object is an instance of a class.

  • There are two key points to note about the constructor: the name of the constructor matches the name of the class, and there’s no return type.

  • For most classes, you don’t have to code a constructor the compiler will supply a “do nothing” default constructor for you.

  • Reading a variable is known as getting it and writing to a variable is known as setting it.

  • When you learned about methods, you saw braces ({}). The code between the braces is called a code block.

  • 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.

  • Let’s look at what’s happening here. We start with the main() method because that’s where Java starts execution. On line 9, we call the constructor of Chick. Java creates a new object. First it initializes name to "Fluffy" on line 2. Next it executes the print statement in the instance initializer on line 3. Once all the fi elds and instance initializers have run, Java returns to the constructor. Line 5 changes the value of name to "Tiny" and line 6 prints another statement. At this point, the constructor is done executing and goes back to the print statement on line 10.

      1.  public class Chick {
      2.      private String name = "Fluffy";
      3.      { System.out.println("setting field"); }
      4.      public Chick() {  
      5.          name = "Tiny";
      6.          System.out.println("setting constructor");
      7.      }
      8.      public static void main(String[] args) {
      9.          Chick chick = new Chick();
      10.         System.out.println(chick.name); 
      11.     } 
      12.    
      13. }
    
  • You can’t refer to a variable before it has been initialized:

      { System.out.println(name); } // DOES NOT COMPILE
      private String name = "Fluffy";
    
  • Fields and blocks are run first in order.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment