Java memorandum (list)

A memorandum of memo

◆ Compile error

-Error: \ <identifier > is missing.

Check if the type is specified.

◆ Warning (class file has been generated)

-Caution: The operation of "File name" is unchecked or unsafe.

Note: For more information, recompile with the -Xlint: unchecked option. Check if type inference by generics <> is described.

◆ Others

-Instance initializer

ArrayList and HashMap can be initialized in one line.


// {{}}← This is the instance initializer syntax

List<String> list = new ArrayList<>() {{add("A"); add("B"); add("C");}}

//Considering readability, it is as follows.

List<String> list = new ArrayList<> {
  { add("A");
    add("B");
    add("C");
  }
}

・ Instanceof operator

If the left side (instance) has the same type, inheritance relationship, or implementation relationship as the right side (type), "true" is returned. If the types are not at all </ font>, you will get a compile error.


//·syntax
//<instance> instanceof <Type type>

class Main{
  public static void main(String... args){
    System.out.println(new A() instanceof B); // false
    System.out.println(new B() instanceof A); // true
    System.out.println(new C() instanceof A); //Compile error
  }
}

class A {}
class B extends A {}
class C {}

-Static initializer's (executed) behavior

***-case ① At the time of class instantiation: *** static initializer ⇒ initializer (initialization block) ⇒ constructor (The static initializer runs even if there are no static members.)

***-case② When accessing the static field: *** ★ When there is no final ... The static initializer runs regardless of whether the field is initialized or not. ★ When there is final ... If the field is initialized, it will not be executed. If the field is not initialized, it will be executed.

***-case ③ When accessing the staic method: *** Executed with or without final.

· Rules to follow when overriding equals () and hashCode ()

(1) The hashCode method for the same instance should return the same integer value. (2) If the equals method is "true", the hashCode method should also return "true". (3) If the equals method is "false", the hashCode method may return either. ④ If the hashCode method is "false", the equals method should also return "false".

· Overridden method priority

Exact match> Inheritance relationship> Implicit type conversion (dilation) (bit conversion)> Autoboxing> Variadic argument

-Notes on basic data type (wrapper class)

★ Type conversion (Implicit type conversion) ➡︎

byte short int long float double
      |  char  |

⬅︎ (Type conversion by cast)

★ Reduction conversion without casting For byte, short, char, (int), If it is within the valid range, the int value can be received without casting. (Long, float, double, numbers are not allowed)

★ Notes on using arithmetic operators In the defined variable, the operand on the right side is treated as an int type when the operator (takes two or more operands) is used.

  • This does not apply if the variable type is long, float, or double.
byte b1 = 1;
b1 = 1 + 1;    //No problem if you don't use variables
b1 = --b1;     //No problem in case of increment
b1 = b1 * 1;   //Compile error
b1 = b1 / b1;  //Compile error

//approach
int b1 = 1;    //Declared as int type
b1 = (byte)(b1 * 1);  //To cast

・ Nested class

★ Types of nesting classes

--Nested class --Inner class (non-static class) --Local class (class in method) --Anonymous class --static class

★ Nested class rules

--static / non-static common --The class name of the same name of the outer class cannot be used --Access modifiers can be used --Abstract, final modifiers can be used --Interface and abstract class can be defined --static class only --Can have non-static / static members --Inaccessible instance variables defined in the outer class --Non-static classes only --Static members cannot be defined --Accessible to instance variables defined in the outer class --Other --Nested classes (IF, abstract, class) can be defined in interfaces and abstract classes --Outer classes can be implemented and extended

//Inner class(Non-static class)Instantiation syntax for
Outer class name.Inner class name variable name=new outer class().new inner class name();

//Static class instantiation syntax
Outer class name.static class name variable name= new Outer class name.static class name();

//Definition example
class Outer{
  abstract class A {abstract void hoge();}
  class B extends A {void hoge(){}}
  static interface X {void bar();}
  static class Y implements X {public void bar(){}}
} 

★ Local class rules

--Access modifier not available --Static modifier not available --Abstract, final modifiers available --Accessible to members of the outer class --When accessing the arguments and local variables of the methods of the outer class from the local class, each variable must be a practically final variable (constant). (Substantial is treated as final even if it is not final-qualified. Therefore, it cannot be reassigned.)

★ Anonymous class rules

--Access modifier not available --Static modifier not available --Abstract, final modifier not available --Accessible to members of the outer class --Accessible to outer class method arguments and local variables as substantive final variables --Construct cannot be defined --A semicolon is required at the end because it is treated as an expression

-Functional interface

★ Requirements for becoming a functional interface

--The interface must have a single abstract method --Static methods and default methods can be defined --The public method of java.lang.Object class can be declared as abstract method </ font> --If you want to specify it as a functional interface, add "@FunctionalInterface" annotation.

Recommended Posts