[java] Java SE 8 Silver Note

Examination tips

--Read the questions carefully (do not overlook the correct, incorrect, 3 etc.) --Check the variable type and access modifier carefully. Beware of type mismatches and permissions issues

java basics

--Classes belonging to anonymous packages can only be accessed by packages belonging to anonymous packages When protected is inherited by a class in another package, it can be accessed from the inherited class, but anonymous packages cannot be accessed (constraints of anonymous packages take precedence). --Package has no concept of hierarchy

Manipulating java data types

--Underscore (_) can be used for literals (with the following restrictions) --Cannot be described at the beginning and end of literals (NG: 0) --Cannot be used before or after the symbol (NG: 0.0) --The default for decimal literals is double Therefore, when assigning a literal to a float variable, it is necessary to specify the type (suffix).

  float f = 0.0f;

--Names (identifiers) of variables, methods, classes, etc. have the following restrictions --Reserved words cannot be used --Only underscores (_) and currency symbols ($, etc.) can be used. --Don't start with numbers --byte type is an 8-bit integer (-128 or ~ 127)

  byte b = 0b1000000;
→ 128 due to error
  byte b = (byte)0b1000000;
  →-OK for 128

--Cast is not required for assignment to a higher type (long if int) (upcast)

  int a = 1;
  long b = a;

  float f = 0.0f;
  double d = f;

--Cast is required for assignment to lower type (downcast)

  long a = 1;
  int b = (int)a;

  double d = 0.0;
  float f = (float)d;

--Same rules for classes

Use of operators and judgment structures

--The equal method is defined in the Object class and is an instance check (identity check) --The equal method of the String class is a string check (equivalence check) --Be aware of the above if you have problems overriding the equal method --Since string literals are placed in a constant pool and the same literals are shared, the following conditions are true:

  String a = "aaa"
  String b = "aaa"
  System.out.println(a == b);
→ true is displayed

--If you write a new line in the if statement, the compiler will add curly braces as appropriate (be careful of the trigger).

  int num = 10;
  if (num == 100)
    System.out.println("A");
  else if (num > 10)
    System.out.println("B");
  else
  if (num == 10)
    System.out.println("C");
  else
  if (num == 10)
    System.out.println("D");

Compiler interpretation

  int num = 10;
  if (num == 100) {
    System.out.println("A");
  } else if (num > 10) {
    System.out.println("B");
  } else {
    if (num == 10) {
      System.out.println("C");
    } else {
      if (num == 10) {
        System.out.println("D");
      }
    }
  }

--Long and boolean are not included in the types that can be passed to the switch statement! --Variables cannot be used in switch conditions

Creating and using arrays

--The number of elements is not specified in the array type declaration (compile error)

  //Uninflected word
  int [] a;
  //This is no good
  int [3] a;

--Do not set the number of elements and the initial value at the same time when initializing the array (compile error)

  //Uninflected word
  int [] a = new int []{1, 2};
  //This is no good
  int [] a = new int [2]{1, 2};

--Do not declare the array and set the initial value separately (compile error)

  //Uninflected word
  int [] a = new int []{1, 2};
  //This is no good
  int [] a;
  a = {1, 2};

Use of loop structure

--No particular precautions

Methods and encapsulation operations

--Variadic argument declaration --Define as follows

  void sample(int... num) {
    for (int i = 0; i < num.length; i++) {
      System.out.println(num[i]);
    }
  }

--Variadic arguments are passed as an array --Variadic argument can be specified only at the end

  void sampl(int... num, int pos) {
→ Error
  }

--Access strength of access modifier --public: accessible from all classes --protected: accessible only from subclasses that belong to or inherit from the same package --No qualifier: accessible from classes belonging to the same package --private: accessible only from within the class --Note: Classes belonging to anonymous packages can only be accessed by classes belonging to the same anonymous package --The qualifier of the inherited method is the same as or looser than the original qualifier. (ex: protected: protected or public) --The access modifier of the interface abstract method is "public" --Only statically qualified methods can be accessed from statically qualified methods.

  public class Main {
    int num;
    private static void test() {
      num++;
→ Compile error
      System.out.ptintln(num)
    }

--Implicit type conversion causes compilation error "Ambiguous method call"

  public class Main {
    public static void main(String [] args) {
      Main m = new Main();
      System.out.println(m.calc(2, 3));
      →2,3 can also be interpreted as a double by implicit type conversion, so
I can't determine which of the overloaded methods to call. .. ..
→ Compile error in "Ambiguous method call"
    }

    private double calc(double a, int b) {
      return (a + b) / 2;
    }

    private duoble calc(int ak double b) {
      return (a + b) / 2;
    }
  }

--The constructor call (super, this) must be done at the beginning of the process

  public class Main {
    public Main() {
      System.out.println("A");
      this("B");
→ Compile error
    }
    public Main(String str) {
      System.out.println("B");
    }
  
    public static void main(String [] args) {
      Main m = new Main();
    }
  }

--protected can be referenced in inherited classes (but cannot be called as an instance method) Of course, it ’s likely to be misunderstood.

  package other;
  
  public class Book {
    private String isbn;
    public void setIsbn(String isbn) {
      this.isbn = isbn;
    }
    protected void printInfo() {
      System.out.println(this.isbn);
    }
  }
  package pkg;
  import other.Book;
  
  public class StoryBook extends Book {}
  package pkg;
  
  public class Main {
    public static void main(String [] args) {
      StoryBook sb = new StoryBook();
      sb.setIsbn("xxxx-xxxx-xxxx");
      sb.printInfo();
→ Compile error
    }
  }

Inheritance operation

--Subclasses do not inherit constructors, private fields or methods --Inheritance rules --Classes have only single inheritance --Multiple inheritance is possible between interfaces --Polymorphism is not only a class that has an inheritance relationship, but also an interface and an implementation relationship.

  interface Worker() {
    void work();
  }
  
  class Employee {
    public void work() {
      System.out.println("work");
    }
  }
  
  class Engineer extend Employee implements Worker { }
  
  public class Main {
    public static void main(String [] args) {
      Worker w = new Engineer();
→ work is output (compile error does not occur)
    }
  }

Handling exceptions

--Exception classes are roughly divided into Error, Exception, and RuntimeException. --Error: An error that cannot be recovered from the program (OutOfMemoryError, StackOverflowError, NoClassDefFoundError, etc.) → Unrecoverable even after inspection --Checked Exception: Exception and its subclasses --Unchecked Exception: Error due to programming error (RuntimeException and its subclass. NullPoiterException etc.) --Checked exceptions must handle the exception (forced to try-catch or throws clause declaration) --When a problem occurs while processing a static initializer, the JVM raises an ExceptionInitializerError and forcibly terminates the program because there is no notification partner.

  public class Main {
    private static String name;
    static {
      if (name.length() == 0) {
→ length with name uninitialized()Causes an error because you called
        name = "sample"
      }
      public static void main(String[] args){
        System.out.println("hellow " + name);
      }
  }

--For double try-catch-finally the finally clause is always executed

  public class Main() {
    public static void main(String[] args) {
      try {
        try {
          String[] array= {"a", "b", "c"};
          System.out.println(array[3]);
        } catch (ArrayIndexOuntOfBoundsException e) {
          System.out.println("D");
        } finally {
          System.out.println("E");
→ executed
        }
      } catch (ArrayIndexOutOfBoundsException e) {
        System.out.println("F");
      } finally {
        System.out.println("G");
→ executed
      }
    }
  }
  →"D E G"Is displayed
  public class Main() {
    public static void main(String[] args) {
      System.out.println(test(null));
→ Display A
    }
    private static String test(Object obj) {
      try {
        System.out.println(obj.toString());
      } catch (NullPointerException e) {
        return "A";
→ A is put on the stack
      } finally {
        System.out.println("B");
→ Output B before return
      }
    }
  }
  →"B A"Is displayed

Main class operations of java API

--How to generate a String object

  String a = new String("sampl");
  String b = "sample"
  String c = String.valueOf("sample");

--When you concatenate (+) a String object to which null is assigned, it is converted to "null" and concatenated.

  String a = null;
  a += "null";
  System.out.println(a)
  →"nullnull"Is displayed

--StringBulider manages data internally with char [] and has a 16 character buffer by default

  StringBuilder sb = new StringBuilder("abcde");
  System.out.println(sb.capacity());
→ 21 is often used(16 characters in buffer+5)

--Regarding the syntax of lambda expressions --If you omit the middle parenthesis in the lambda expression, the process can be described in one sentence. At this time, return cannot be described. --If you write a middle parenthesis in a lambda expression, return cannot be omitted.

```java
Function f = str -> "hello " + str;
Function f = str -> { return "hello " + str };
```

--A variable with the same name as a local variable declared in a lambda method cannot be used as a lambda expression variable

  public class Main {
    public static void main(String[] args) {
      String val = "A";
      Function f = (val) -> { System.out.println(val); };
→ Compile error
    }
    interface Function { void test(String val); }
→ Functional interface
  }

--To access a local variable declared outside a lambda expression from a lambda expression, it must be a virtually final variable (otherwise a compilation error).

  public class Sample {
    public static void main(String[] args) {
      int cnt = 0;
      Runnable r = () -> {
        for (cnt = 0;  cnt < 10; i++) {
→ Compile error(Not final. .. ..)
          System.out.println(cnt++);
        }
      }
      new Thread(r).start();
    }
  }

--StringBuilder's equals method compares objects (does not compare values) --If you specify a date that cannot be the constructor of the LocalDate and LocalDateTime classes, an exception will be thrown. --LocalData, LocalDateTime classes are immutable (internal data is not updated)

Recommended Posts

[java] Java SE 8 Silver Note
[Note] Java Silver SE8 qualification acquired
Passed Java SE8 Silver
Java SE 8 Silver (Java SE 8 Programmer I) Pass Note
[Experience] Passed Java SE 8 Silver
Java SE8 Silver Pass Experience
How to study Java Silver SE 8
Road to Java SE 11 Silver acquisition
Diary for Java SE 8 Silver qualification
[Qualification] Java Silver SE11 Passing Experience
Java Silver memo
Java SE 7 memo
Java SE Subscription
Study Java Silver 1
Java SE8 Silver ~ The Road to Pass ~
Oracle Certified Java Silver SE 8 Passing Experience
The story received by Java SE11 silver
Java abstract modifier [Note]
[Java] Internal Iterator Note
java bronze silver passed
Java Silver Study Day 1
Java Silver passing experience
Is Java SE8 Silver useful for Java development work? ??
Java JUnit brief note
java se 8 programmer Ⅰ memo
[Java Silver] About initialization
[Java SE 11 Silver] Arrays class method summary [Java beginner]
[Note] Java: String search
[Note] Java: String survey
About inheritance (Java Silver)
[Java certification] Freshly picked! SE 11 Silver Passing Experience (2020/12/26)
My Study Note (Java)
[Qualification Exam] Java SE 8 Silver Learning Method Summary
Java8 Silver exam memorandum
java: Add date [Note]
Oracle Certified Java Programmer, Silver SE 8 Certification Examination Experience
[Java Silver] Study method that passed Java SE 8 Silver [Passing experience]
Summary of knowledge required to pass Java SE8 Silver
Story of passing Java Silver SE8 (Oracle Certified Java Programmer, Silver SE 8)
Obtained Oracle Certified Java Programmer Silver SE 8 Examination experience
A note about Java GC
Java SE 8 Sliver exam questions
(Note) Java classes / variables / methods
JAVA Silver qualification exam record
On passing Java Gold SE 8
Java Silver exam preparation memo
I took Java SE8 Gold.
[Java Silver] About equals method
[Java] [Spring] Spring Boot 1.4-> 1.2 Downgrade Note
Study Java with Progate Note 1
[Java Silver] Array generation method
Time taken to acquire Java SE11 Silver, teaching materials used
What you learned when you acquired Java SE 8 Silver and Gold
Note
Object-oriented design learned from "Thorough capture Java SE 11 Silver problem collection"
[Java] Points to note with Arrays.asList ()
Summary of [Java silver study] package
Get Java Silver on your commute!
Note
I fell into Java Silver (crying)
Java Silver Repo (Failure & Pass Experience)