The data type

A variable is an application for memory to store values. That is, when creating variables, you need to allocate space in memory

The memory management system allocates storage space for variables based on their types. The allocated space can only be used to store data of this type

Basic data types

  1. There are eight basic data types in Java: four integers, two floating-point types, characters, and Booleans
  2. Each base data type has data range constraints and defaults

Reference data type

  1. A reference type refers to an object, and variables that point to an object are reference variables
  2. A reference variable is specified as a specific type when it is declared
  3. In Java, strings, arrays, and objects are reference data types
  4. All reference type defaults to bit null
  5. A reference variable can be used to reference any compatible type

Enumerated type

  1. Enum enum is a special class that makes it easy to define constants using enumerations

    public enum Season {
    	SPRING,SUMMER,AUTUMN,WINTER
    }
    Copy the code
  2. A common case is in switch statements, where enumerations are used to determine

    public static void main(String[] args) {
        Season season = Season.SPRING;// Enumerate the class name
        switch (season) {
            case SPRING:
                System.out.println("Spring");
                break;
            case SUMMER:
                System.out.println("Summer");
                break;
            case AUTUMN:
                System.out.println("Autumn");
                break;
            case WINTER:
                System.out.println("Winter");
                break; }}Copy the code
  3. You can use foreach to walk through enumerations

    public static void main(String[] args) {
        for(Season s : Season.values()) { System.out.println(s); }}Copy the code

The modifier

Access control

  1. public
    • Visible to all classes
    • Use objects: classes, interfaces, variables, methods
  2. protected
    • Protected variables, methods, and constructors in the same package can be accessed by any other class in the same package
    • A subclass is not in the same package as the base class. Instances of subclasses can access protected methods inherited from the base class, but cannot use base instances to access protected methods
    • Use objects: variables, methods, inner classes
  3. The default
    • Visible within the same package without any modifiers
    • Use objects: classes, interfaces, variables, methods
  4. private
    • Visible in the same class
    • Use objects: variables, methods, inner classes

Other modifiers

  1. final
    • Modifies classes, methods, and variables
    • Final – modified classes cannot be inherited
    • Modified methods cannot be redefined by inherited classes
    • The modified variable is a constant and cannot be changed by reassignment.
  2. static
    • Used to modify class methods and class variables
    • Decorates inner classes, called static inner classes
  3. abstract
    • Used to create abstract classes and abstract methods
    • The interface and its methods default to public Abstract

methods

A constructor

  1. Action: Constructors are methods that initialize object members when the object is created
  2. The name is the same as the class name and cannot have a return value and void because the return value is the class itself
  3. If a constructor is not defined, it defaults to a no-argument constructor, and if a constructor is defined, it does not have a default constructor
  4. In succession,
    • A parent class does not define a constructor or a constructor without arguments. A child class does not need to define a constructor
    • When the parent class has only a constructor with arguments, the subclass cannot have a parameterless constructor either. It must define a parameterless constructor and call the parent constructor explicitly on the first line via super

Class method

  1. Class methods, that is, static methods, are declared static

  2. Independent of the object, belonging to the class, using the class name. Method name

  3. A static method cannot use a class’s non-static variable, and cannot call a non-static method directly from an object

    Static methods are declared during class loading, when the object is not yet instantiated. Properties of instance objects cannot be used in static methods

  4. Static methods cannot be inherited. A subclass may declare a static variable of the same name that does not affect the parent class, known as hiding

  5. Abstract methods cannot be static, either in abstract classes or interfaces

    The purpose of abstract methods is to override methods; static methods cannot be overridden

    Abstract methods have no method body, so static abstract methods make no sense

    Abstract classes can have static non-abstract methods

    In JDK8, interfaces can declare static non-abstract methods

Object methods

  1. Object methods, that is, non-static methods
  2. After the object is instantiated, pass the object name. Method name
  3. Non-static methods can call both non-static and static methods directly
  4. Non-static methods use this to represent the current object

variable

An identifier used to name data

Member variables are global variables, also called fields

Class variables

  1. Class variables, that is, static variables, are declared static
  2. Independent of the object, belonging to the class, by class name. Variable name to access

Object variables

  1. Object variables, that is, non-static variables
  2. After the object is instantiated, pass the object name. Variable name to call

A local variable

  1. Local variables, that is, temporary variables, such as variables declared inside a method, method parameters
  2. Local variables are stored on the stack, and the memory occupied by local variables is freed after the method ends
  3. Manual assignment is required for initialization, otherwise an error is reported
  4. Local variables cannot use static and access modifiers

The inner class

Non-static inner class

  1. A non-static inner class is defined directly inside the outer class

  2. Non-static inner classes, which belong to objects, only make sense if an outer class object exists

  3. A non-static inner class can access static and non-static members of an external class

  4. Create non-static Inner class objects by new Outer().new Inner() or this.new Inner(), this can be omitted

    public class Outer {
        private int i;
        private static int s;
    
        private void outMethod(a) {
            new Inner();// this is omitted
            this.new Inner(a);
        }
    
        public static void outStaticMethod(a) {
            new Outer().new Inner(a);// External class instance object. new inner class ()
        }
    
        class Inner {
            public void inMethod(a) {
                outMethod();// Access external private methods
                outStaticMethod();// Access external static methods
                System.out.println(i);// Access external static variables
                System.out.println(s);// Access external non-static variables}}}Copy the code

Static inner class

  1. Static inner classes use the static modifier
  2. Static inner classes belong to classes
  3. Static inner classes cannot access external class non-static variables and non-static methods
  4. Static inner classes can access static members of external classes
  5. You don’t need an instance of an external class to base it on; you can instantiate it directly

Anonymous inner class

  1. Typically, to use an interface or abstract class, you must create a subclass

  2. For quick use, instantiate an abstract class directly and implement its abstract methods. This class is unnamed and called an anonymous inner class

    public class Outer {
        public static void main(String[] args) {
            int a = 0;
            new Inner() {
                @Override
                public void inMethod(a) {
                    // overwriteSystem.out.println(a); }}; }}abstract class Inner {
        public abstract void inMethod(a);
    }
    Copy the code
  3. Anonymous inner classes allow easy access to local variables, which are declared final, and are implicitly declared final if omitted in JDK8

    Declare final, then local variables can no longer be assigned in anonymous inner classes

    Local variables accessed by the inner class are copied to the inner class. If the local variable is not final, its value can be modified, and data synchronization will occur

A local class

  1. Inner classes must declare the position of members, equal to that of properties and methods

  2. Local classes, like anonymous classes, are declared directly in code blocks, either in main methods, for loops, and so on

  3. The difference from anonymous classes is that native classes have custom class names

    public class Outer {
        public static void main(String[] args) {
            class Test extends Inner {
                @Override
                public void inMethod(a) {
                    // overwrite}}}}abstract class Inner {
        public abstract void inMethod(a);
    }
    Copy the code