--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
--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
--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
--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
--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};
--No particular precautions
--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
}
}
--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)
}
}
--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
--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