盒子
盒子
文章目录
  1. Understanding Java Technology and environment
    1. Describe Java Technology and the Java development environment
      1. Package Declaration and Imports
      2. Constructors
      3. Initialzier Blocks
      4. Order of Initialization
    2. Identify key features of the Java language
  2. Working With Java Primitive Data Types and String APIs
    1. Using Operators and Decision Constructs
      1. Use Java operators; including parentheses to override operator precedence
      2. Create if and if/else and ternary constructs
      3. use a switch statement
    2. Declare and initialize variables (including casting and promoting primitive data types)
    3. Identify the scope of variables
    4. Use local variable type inference
    5. Create and manipulate Strings
    6. Manipulate data using the StringBuilder class and its methods
  3. Working with Java Arrays
    1. Declare, instantiate, initialize and use a one-dimensional array
    2. Declare, instantiate, initialize and use two-dimensional array
  4. Creating and Using Methods
  5. Reusing Implementations Through Inheritance
  6. Handling Exceptions
    1. throw 和 throws 区别
  • Describe the advantages of Exception handling
  • Differentiate among checked exceptions, unchecked exceptions, and Errors
  • Create a try-catch block and determine how exceptions alter normal program flow
    1. Adding a finally block
  • Create and invoke a method that throws an exception
  • Recognize common exception classes
    1. Catching various types of exceptions
    2. Throwing a Second Exception
    3. Types of execptions
    4. 考点
  • Creating a Simple Java Program
  • Using Operators and Decision Constructs
  • Describing Objects and Classes
  • Applying Encapsulation
  • Programming Abstractly Through Interfaces
  • Java SE 11 Programmer I | 1Z0-815

    Understanding Java Technology and environment

    An object is a runtime instance of a class in memory. Java classes have two primary elements: methods and fields.

    Classes v.s. Files

    If you do have a public class, it needs to match the filename.
    Most of the time, each Java class is defined in its own *.java file.
    And you can put two classes in the same file. When you do so, at most one of the classes in the
    file is allowed to be public.

    Describe Java Technology and the Java development environment

    source code .Java file –> bytecode .class file for the JVM

    javac Zoo.java
    java Zoo “San Diego” Zoo

    • If you want spaces inside an argument, you need to use quotes.

    Package Declaration and Imports

    • Java puts classes in packages.
    • Java automatically import java.lang package
    • Another case of redundancy involves importing a class that is in the same packages as the class importing it. Java automatically looks in the current pacakge for other classes.
    • Wildcards only matches class names
      import java.nio.file.Files –> import java.nio.file. not import java.nio.
    • Tricky case: when the class is found in multiple packages, Java gives you the compiler Error:
      import java.util.
      import java.sql.
      // Not compile, because both have the Date class
      “The type Date is ambiguous”
    • If you explicitly import a class name, it takes precedence over any wildcards present.
      import java.util.Date;
      import java.sql.* // need a whole pile of other classes in the java.sql packages but need
      java.util.Date

    • compile error: The import java.sql.Date collides with another import statement
      import java.util.Date;
      import java.sql.Date;

    • If you really need to Use two classes with the same name:

      1. one use the import, the other use the fully qualified class name, the package name, a dot, and the class name, to specify that it’s special.
    1
    2
    3
    4
    5
    6
    import java.util.Date;

    public class Conflicts {
    Date date;
    java.sql.Date sqlDate;
    }

    OR, just don’t use import and always use the fully qualified class name:

    1
    2
    3
    4
    public class Conflicts {
    java.util.Date date;
    java.sqal.Date sqlDate;
    }

    Default package is no package name.

    Constructors

    Two key points:

    1. the name of the constructor matches the name of the class
    2. there is no return type
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20

    public class Demo {

    int number = 0; // another way to initialize fields is to do so directly
    // on the line on which they're declared.
    String string;

    // constructor
    public Demo() {
    /* the purpose of a constructor is to initialize fields
    *
    */
    string = "Hello";
    }

    // not a constructor
    public void Demo() {

    }
    }

    Initialzier Blocks

    The code between { } is called a code block. Sometimes, code blocks are inside a method. Other times, code blocks appear outside a method. These are called instance initializers.

    Order of Initialization

    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.

    Order matters for the fields and blocks of code. See example below:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    public class Chick {
    // good
    private String name = "Fluffy";
    {
    System.out.println("setting field");
    }

    // DOES NOT COMPILE
    // Cannot refer to a variable before it has been initialized
    {
    System.out.println(name);
    private String name = "Fluffy";
    }


    }

    Identify key features of the Java language

    • Objected Oriented
    • Encapsulation
    • Platform Independent
    • Robust
    • Simple
    • Secure

    Working With Java Primitive Data Types and String APIs

    Using Operators and Decision Constructs

    Use Java operators; including parentheses to override operator precedence

    Create if and if/else and ternary constructs

    use a switch statement

    Declare and initialize variables (including casting and promoting primitive data types)

    Identify the scope of variables

    Use local variable type inference

    Create and manipulate Strings

    Manipulate data using the StringBuilder class and its methods

    Working with Java Arrays

    Declare, instantiate, initialize and use a one-dimensional array

    Declare, instantiate, initialize and use two-dimensional array

    Creating and Using Methods

    Create methods and constructors with arguments and return values
    Create and invoke overloaded methods
    Apply the static keyword to methods and fields

    Reusing Implementations Through Inheritance

    Create and use subclasses and superclasses
    Create and extend abstract classes
    Enable polymorphism by overriding methods
    Utilize polymorphism to cast and call methods, differentiating object type versus reference type
    Distinguish overloading, overriding, and hiding

    Handling Exceptions

    Categories of Exception:

    java.lang.Object –> java.lang.Throwable –> java.lang.Exception –> java.lang.RuntimeException
    |–> java.lang.Error

    Error means something went so horribly wrong that your program should not attempt to
    recover from it. E.G. a disk drive “disappeared”

    runtime exception is defined as the RuntimeException class and its subclasses. It tends
    to be unexpected but not necessarily fatal. E.G. accessing an invalid array index is
    unexpected. It is known as unchecked exceptions.

    checked exception includes Exception and all subclasses that do not extend RuntimeException.
    Checked exceptions tend to be more anticipated, like trying to read a file that doesn’t exist.

    – handle or declare rule: for checked exceptions, Java requires the code to either handle them
    or declare them in the method signature.

    throw 和 throws 区别

    throws: 在方法定义中用,declares that the method might throw an Exception.
    throw: 在方法体中抛出异常, tells Java that you want to throw an Exception and tells Java you want
    some other part of the code to deal with the exception.

    1
    2
    3
    4
    public void myFunction() throws Exception {

    throw new Exception();
    }

    Describe the advantages of Exception handling

    Differentiate among checked exceptions, unchecked exceptions, and Errors

    1. runtime exception == unchecked exception, 可以handle,也可以不handle

    2. checked exception 包括所有在Exception中但是不在RuntimeException里的Exception, checked exception 必须 handle 或则 declare

    Create a try-catch block and determine how exceptions alter normal program flow

    Java uses a try statement to separate the logic that might throw an exception from the logic
    to handle that exception

    考点:在 try 语句中有没有办法执行的语句

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15

    void exception() {
    try {
    fall();
    System.out.println("never get here");
    } catch (RuntimeException e) {
    getUp();
    }

    seeAnimals();
    }

    void fall() {
    throw new RuntimeException();
    }

    Adding a finally block

    The finally block always executes, whether or not an exception occurs in the try block.

    There are two paths through code with both a catch and a finally.

    • If an exception is thrown, the finally block is run after the catch block.
    • If no exception is thrown, the finally block is run after the try block completes.

    In OCA examp, a try statement must have catch and/or finally. In other words, catch is not
    required if finally is present. Having both is fine, Having neither is a problem.

    finally is typically used to close resources such as files or databases, so in OCA exam
    with finally are going to look contrived.

    Create and invoke a method that throws an exception

    考点: the vast majority of questions have a choice about not compiling and
    about throwing an exception.

    calls a method on a null/ an invalid arry index --> code will throw an exception.
    

    Recognize common exception classes

    Catching various types of exceptions

    Steps:

    1. you must be able to recognize if the exception is a checked or an unchecked exception.
    2. you need to determine if any of the exceptions are subclasses of the others.

    A rule exists for the order of the catch blocks. Java looks at them in the order they appear.
    If it is impossible for one of the catch blocks to be executed, a compiler error about unreachable
    code occurs.

    考点: E.G. This happens when a superclass is caught before a subclass.

    Throwing a Second Exception

    A catch or finally block can have any valid java code in it – including another try statement.

    Types of execptions

    Type How to recognize Okay for program to catch? Is program required to handle or declare?
    Runtime exception Subclass of RuntimeException Yes No
    Checked exception Subclass of Exception but not subclass of RuntimeExcpetion Yes Yes
    Error Subclass of Error No No

    runtime exception 可能是 programmer 或者是 JVM 抛出的。

    一些常见的继承于RuntimeException的异常:

    JVM

    • ArithmeticException 1除0
    • ArrayIndexOutOfBoundsException
    • ClassCastException 什么时候不能编译
    • NullPointExeception (unchecked exception)

    Programer

    • IllegalArgumentExecption
    • NumberFormatException

    常见的chekced exception 有:

    • FileNotFoundException (IOException 的子类)

    • IOException

    考点

    • system.exit() 在 try/catch 中的话, finally block 不会 run
      When System.exit is called in teh try or catch block finally does not run.
    • 如果有一个catch 不会 run,则是 compiler error 会爆出来
    • Error:
      ExceptionInInitializerError : thrown by JVM when a static initializer throws an exception and doesn’t handle it

    StackOverflowError : thrown by JVM when a method calls itself too many times, (infinite loop)

    NoClassDefFoundError : thrown by JVM when a class that the code uses is available at compile time but not runtime.

    Creating a Simple Java Program

    Create an executable Java program with a main class
    Compile and run a Java program from the command line
    Create and import packages

    Using Operators and Decision Constructs

    Use Java operators including the use of parenthesis to override operator precedence
    Use Java control statements including if, if/else, switch
    Create and use do/while, while, for and for each loops, including nested loops, use break and continue statements

    Describing Objects and Classes

    Declare and instantiate Java objects, and explain objects’ lifecycles (including creation, dereferencing by reassignment, and garbage collection)
    Define the structure of a Java class
    Read or write to object fields

    Applying Encapsulation

    Apply access modifiers
    Apply encapsulation principles to a class

    Programming Abstractly Through Interfaces

    Create and implement interfaces
    Distinguish class inheritance from interface inheritance including abstract classes
    Declare and use List and ArrayList instances
    Understanding lambda Expressions
    Understanding Modules
    Describe the Modular JDK
    Declare modules and enable access between modules
    Describe how a modular project is compiled and run

    支持一下
    扫一扫,支持forsigner
    • 微信扫一扫
    • 支付宝扫一扫