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
- There are eight basic data types in Java: four integers, two floating-point types, characters, and Booleans
- Each base data type has data range constraints and defaults
Reference data type
- A reference type refers to an object, and variables that point to an object are reference variables
- A reference variable is specified as a specific type when it is declared
- In Java, strings, arrays, and objects are reference data types
- All reference type defaults to bit null
- A reference variable can be used to reference any compatible type
Enumerated type
-
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
-
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
-
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
- public
- Visible to all classes
- Use objects: classes, interfaces, variables, methods
- 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
- The default
- Visible within the same package without any modifiers
- Use objects: classes, interfaces, variables, methods
- private
- Visible in the same class
- Use objects: variables, methods, inner classes
Other modifiers
- 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.
- static
- Used to modify class methods and class variables
- Decorates inner classes, called static inner classes
- abstract
- Used to create abstract classes and abstract methods
- The interface and its methods default to public Abstract
methods
A constructor
- Action: Constructors are methods that initialize object members when the object is created
- 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
- 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
- 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
-
Class methods, that is, static methods, are declared static
-
Independent of the object, belonging to the class, using the class name. Method name
-
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
-
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
-
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
- Object methods, that is, non-static methods
- After the object is instantiated, pass the object name. Method name
- Non-static methods can call both non-static and static methods directly
- 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
- Class variables, that is, static variables, are declared static
- Independent of the object, belonging to the class, by class name. Variable name to access
Object variables
- Object variables, that is, non-static variables
- After the object is instantiated, pass the object name. Variable name to call
A local variable
- Local variables, that is, temporary variables, such as variables declared inside a method, method parameters
- Local variables are stored on the stack, and the memory occupied by local variables is freed after the method ends
- Manual assignment is required for initialization, otherwise an error is reported
- Local variables cannot use static and access modifiers
The inner class
Non-static inner class
-
A non-static inner class is defined directly inside the outer class
-
Non-static inner classes, which belong to objects, only make sense if an outer class object exists
-
A non-static inner class can access static and non-static members of an external class
-
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
- Static inner classes use the static modifier
- Static inner classes belong to classes
- Static inner classes cannot access external class non-static variables and non-static methods
- Static inner classes can access static members of external classes
- You don’t need an instance of an external class to base it on; you can instantiate it directly
Anonymous inner class
-
Typically, to use an interface or abstract class, you must create a subclass
-
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
-
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
-
Inner classes must declare the position of members, equal to that of properties and methods
-
Local classes, like anonymous classes, are declared directly in code blocks, either in main methods, for loops, and so on
-
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