Java final review of fragmented knowledge points (organized by the question bank of "Java2 Practical Tutorial")

Organized according to the question bank system of "Java2 Practical Tutorial"

I have sorted out the knowledge points and concepts that are easy to make mistakes in the process of doing the questions.
Due to limited time, the update progress is relatively slow. The remaining chapters will be updated slowly. I
am currently studying and my ability is limited, so there are inevitably small errors in the article. Corrections are welcome. ~
If you need a question bank, you can chat with me privately. Come on, friends! ! ヾ(◍°∇°◍)ノ゙


Chapter One

  • The main contributor to the Java language is James Gosling

  • The extension of java source file is .java

  • The extension of the bytecode file obtained by compiling the java source file is .class

  • Source files must be written using a text editor

  • The java compiler is located in the \bin folder of the java installation directory, and its name is javac.exe


  • Java source files are composed of several classes that are written independently of each other.

  • There can be multiple classes in a java source file , and there can be no main class

  • If there are multiple classes in the source file , at most one must be a public class, or there may be no public class.

  • If there is a main class in java, it is not necessarily a public class

  • Java applications must have a main class, but it does not need to be a public class

Main class:
The class name is consistent with the file name, and the class containing the main function
is the entry point of the program. To allow the JVM to find the entry point for execution, it must:

  • The class name is consistent with the file name
  • 包含public static void main(String[] args) {}
  • Note that this class does not necessarily need to have public modification

Java source file naming:
The suffix of the source file must be .java.
If a public class is defined in the source code, the main file name of the source file must be the same as the class name of the public class (if there is a class that does not have public but is main , this class is the main function, but the name is still the same as the class name of the public class)

If there is no public class defined in the Java program source code, the main file name can be arbitrary (any class in the file)

You can refer to the article: Main class and file naming in Java


Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here

Java programming styles
Allmans style and Kernighan style.

Allmans style is also called "one-line style", that is, the left and right braces ("{", "}") each occupy its own line.
Kernighan style is also called "end-of-line style", that is, the left brace ("{") is on the previous line. end of the line, while the closing brace ("}") occupies its own line.

Insert image description here

Chapter two

basic type Bytes occupied (bits) Wrapping class (java.lang) minimum value maximum value
byte 1 Byte -128 127
short 2 Short -32768 32767
int 4 Integer -2147483648 2147483647
long 8 Long -9223372036854775808 9223372036854775807
float 4 Float 1.4E-45 3.4028235E38
double 8 Double 4.9E-324 1.7976931348623157E308
char 2 Character 0 65535
boolean 1/8 byte (1 bit) Boolean
  • 8 basic data types: boolean, byte, short, int, long, float, double, char

  • One-dimensional array declaration: both int[] a and int a[] are acceptable

  • int[] a,b declares two one-dimensional arrays of type int

  • int a[],b declares a one-dimensional array a of type int and an variable b of type int

  • int[] a, b[] declares a one-dimensional array a of type 1int and a two-dimensional array b of type int.

  • float a[20] is wrong array declaration

  • int[] a = new int[2], the values ​​​​of a[0], a[1], and a[2] are all 0

  • int a[][] = new int[6][4]; a.length = 6

  • boolean f = true is a correct variable declaration (TRUE is an error)

  • Adding F or f after the decimal represents a float type constant, otherwise it defaults to double type.

  • float foo = 0x123; is the correct float variable declaration (0x represents an integer, integer → floating point number, upward conversion will not overflow, so it can [float = 1.3, 1.3 defaults to double, double → float downward conversion, may cause Overflow, so the initial value of float assignment should be written as float = 1.3f])

  • float x = ‘a’; //float = 97.0

  • char ch = 97; is the correct char variable declaration (char = '' or char = ascii) //ch = 'a'

  • The position range of char in the Unicode table is 0~65535

  • \u is an escape character, indicating that it is followed by a hexadecimal number.
    char ch = '\u777f' is a correct char variable declaration.
    char ch = '\u777g' is an incorrect char variable declaration.

  • 1 e1It is 10 raised to the power of 1 (1st power) and is of type double

  • 1 e2 fIt is the 2nd power of 10 (2nd Insert image description here
    power), and is a float type

  • An array is a composite data type composed of data elements of the same type in order, so if two arrays of the same type have the same reference , they have exactly the same elements.

  • Keywords: int, import, extends

  • _int can be used as an identifier

  • _class can be used as an identifier

  • For Java identifiers, please refer to the article: Java identifiers

third chapter

  • The expression 10>20-7 evaluates to false
  • The operation results of relational and logical operators are of boolean type.
  • The operand of a logical operator must be boolean data
  • The value of expression 2 in the for (expression 1; expression 2; expression 3) statement and the expression in if (expression) must be boolean data.If the if statement is a boolean assignment statement, that is, f = true, f will be assigned first, and f will be used as a condition for judgment.
  • 12 = 12 is an illegal expression
  • 0.4 == 0.4f The result is false
  • The data type of 'apple'+'fruit' is int
    [Convert the two char type operands to the int type, and then perform the sum calculation, and the returned value is int]

Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here

Chapter Four

  • Class is the most important "data type" in the Java language. Variables declared by a class are called object variables , or objects for short (in simple terms, variables declared by a class are called objects)
  • If a constructor is defined in the class, the Java compiler will no longer provide a default constructor, and the constructor can be overloaded
  • Member variables are declared in the class body to reflect the properties of the object
  • The non-constructor methods defined in the class body content are to reflect the behavior of the object
  • The declaration of member variables and the definition of methods in the class body can be interleaved.
  • Steps when creating an object using the new operator and the constructor method:
    ① Allocate memory for member variables and specify default values
    ​​② Initialize member variables, that is, the default values ​​given when the user declares member variables
    ③ Execute the constructor
    ④ Calculate a reference value

  • Constructor has no type
  • Constructors can be overloaded

  • Class methods (static)
    in a class can be directly called with the class name, and can only operate on class variables (static variables), but not instance variables. Instance methods in a class cannot be directly called with the class name, and can only operate on class variables (static variables). ), you can also operate instance variables
    [class methods are static modified methods, instance methods are ordinary member methods]

  • There are two types of member variables of Java classes :
    ① Variables modified by the static keyword are called class variables or static variables ;
    ② Without static modification, they are instance variables
  • Member variables are valid within the entire class , and their validity has nothing to do with the order in which they are written in the class.
  • Instance variables in a class will only be allocated memory space when an object is created using the class.
  • Local variables defined in a method allocate memory when the method is executed.
  • Class variables (static) in a class will be allocated to memory space when the class is loaded into memory (even if the class is a created object)
  • A class can use an object as a member variable of its own
  • Variables assigned to an object are conventionally called entities of the object
  • The name of the parameter in the method cannot be the same as the name of the local variable declared in the method
  • The name of a member variable can be the same as that of a local variable ( member variable and local variable )
  • Member variables have default values
    ​​[boolean default value is false; byte, short, int, long are 0, character type (char) is '\u0000' (null character), floating point type (float, double) is 0.0, reference type (String) is null]
  • Local variables have no default value
    [Uninitialized local variables cannot be used, local variables must have a value before use]

  • this keyword represents an object
  • When the this keyword appears in an instance method, it represents the current object that is calling the method.
  • this can appear in instance methods and constructors, but cannot appear in class methods (static methods)

  • A Java application consists of several classes . These classes can be in the same source file or distributed among several source files.

  • The source file contains at most one package statement (package) , or there may be no
    [package statement is the first statement of the Java source file , indicating the package in which the class defined by the source file is located. The general format is: package package name]

  • Source files can have multiple import statements

  • A java source file has the **"import java.lang.*;"** statement by default


  • Naming rules
  • It is customary to lowercase the first letter of method names and variable names.

  • Overloading: There can be multiple methods with the same name in a class, but the parameters of these methods must be different
  • static methods can be overloaded
  • Constructors can be overloaded

  • Classes cannot be protected or private classes
  • Friendly class: class Test
    public class: public class Test
  • The access permission of the constructor can be public, protected, private or friendly
  • Local variables declared in a method cannot be modified with the access modifiers public, protected, and private, and cannot be modified with static.

  • Float is the name of a class in the java.lang package , and float is the keyword of java , used to declare float basic type variables.

  • If two objects declared by a class have the same reference , they have exactly the same variables (entities).

  • Objects without entities are called empty objects. They should be carefully checked to avoid using empty objects, that is, letting an empty object call methods to generate behavior.

  • The combination relationship is also called has a, which is a weak coupling relationship (between classes)

  • The compiler does not prompt that the program uses a null object (let an empty object call instance methods), but a NullPointerException occurs when the program is running.

  • In Java, all parameters of a method are "pass-by-value", that is to say, the value of the parameter variable in the method is a copy of the actual parameter value specified by the caller.

  • When the method is called, the parameters are allocated memory space, and the caller passes the value to the parameter (that is, when the method is called, the parameter variable must have a specific value )

Insert image description here
Insert image description hereInsert image description here
Insert image description here>Insert image description here
Insert image description here

Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here

chapter Five

  • A non-final class can have multiple subclasses
  • The class declaration Class A is equivalent to Class A extends Object

  • If the subclass and the parent class are not in the same package , the subclass does not inherit the friendly members of the parent class.
modifier kind Bag Subclass Other packages
public
protected
default(default)
private (encapsulation)
  • The constructor can be modified with public, protected, or private
  • Method modifications (if any), such as public and abstract, must be placed in front of the method type and cannot be inserted between the method type and the method name.
  • If the access rights of all constructors of a class are private , it means that this class cannot have subclasses
  • The private method of a class cannot be used in other classes , but the constructor of the subclass will definitely call a constructor of the parent class.
  • When a subclass overrides a method of a parent class, it is not allowed to reduce the method's access rights, but it can increase the access rights
    (access restriction modifiers are arranged in descending order of access rights: public, protected, friendly, private)

  • The Object class in the java.lang package is ~~the father of all other classes~ (it is the root class of all other classes, and children and parents extend directly)
  • The so-called subclass inherits the member variables of the parent class as one of its own member variables , as if they were directly declared in the subclass , and can be operated by any instance method defined in the subclass.
  • If you want to use instance member variables or instance methods hidden by the subclass in a subclass (usually inherited from the parent class and then rewritten), you need to use the keyword super
  • A subclass can declare member variables with the same name as those of the parent class.
  • Subclasses can define methods with the same name as parent class methods.
    Can subclasses inherit the constructor method of parent class?
  • The construction method is a method unique to a class and will not be inherited by subclasses . Subclasses do not inherit the construction method of the parent class, but they can be overloaded.
  • The subclass does not inherit the constructor method super of the parent class (calling, calling and inheriting are two different things).
    However, when the subclass instantiates an object , if the constructor of the subclass does not explicitly call the constructor of the parent class, the default constructor of the parent class will be automatically called. The constructor (no parameters) is equivalent to omitting super() by default
  • In the constructor of the subclass, if the super keyword is not explicitly written to call a certain constructor of the parent class, then the compiler defaults to:
    super(); calling the parameterless constructor of the parent class (if the parent class Without such a constructor, the code will have compilation errors)
  • The statement formed by the super keyword must be the first statement in the subclass constructor.
  • Subclasses and parent classes do not have to be in the same package
  • Polymorphism means that when a method of the parent class is overridden by a subclass, each can produce its own functional behavior.
  • If the variable names of the members declared by the subclass are the same as the variable names of the members inherited from the parent class, the subclass will hide the inherited member variables.
  • Methods rewritten or added by subclasses can also directly operate member variables hidden by subclasses (×)
  • The member variables operated by the methods inherited by the subclass must be the member variables inherited or hidden by the subclass.
    The member variables are hidden.

Hiding of member variables: If the member variables inherited from the parent class are redefined
in a subclass , the subclass variables will hide the parent class variables. The so-called hiding means that the subclass has two variables with the same name, one is inherited from the parent class, and the other is defined by itself.

When the subclass executes its own defined method , if it operates on the variable, it will operate on the self-defined variable , while the variables inherited from the parent class will be "hidden".

When a subclass performs an operation inherited from the parent class , if the variable is manipulated, the member variable inherited from the parent class is manipulated.

  • Subclasses can use the super keyword (override, call constructors, access parent class members) to call methods in the parent class

  • Subclasses can use the super keyword to call methods in the parent class of the parent class.


  • A static method can also be a final method at the same time

  • It is wrong for any class to have multiple subclasses . Modifiers must also be considered. Final and static have no subclasses because they cannot be inherited.

  • You cannot use final and abstract to modify the same method or class at the same time.

  • It is not allowed to use static (class method, not allowed to be overridden) and abstract to modify the same method at the same time

  • In an abstract class, there does not have to be an abstract method. There can be a non-abstract method. This method can be modified with final.

  • Constructors cannot be modified with final or static

  • When rewriting methods in a subclass, you cannot rewrite the class method (static) of the parent class into an instance method, nor can you rewrite the instance method of the parent class into a class (static) method.

  • If there is an abstract method in a class, the class must be an abstract class

  • If the subclass is an abstract class, the subclass is allowed to rewrite the non-abstract methods of the parent class into abstract methods.

  • abstract class cannot be final class

  • Objects declared by abstract classes can become up-cast objects of subclass objects.

  • Objects cannot be created using the new operator + the constructor method of the abstract class

  • If an abstract class is a subclass of an abstract class, it can rewrite the abstract method of the parent class as a non-abstract method, or it can inherit directly without rewriting the abstract method.


Upcasting: Declare a reference to a parent class that points to an instantiated object of the subclass
Upcasting object: Parent class

  • Upcast objects of subclass objects cannot operate the newly added member variables of the subclass (lost these attributes), and cannot call the new methods of the subclass (lost some behaviors).
  • The upcast object of a subclass object operates methods inherited by the subclass or instance methods overridden by the subclass. Its function is equivalent to calling these methods on the subclass object.
  • Abstraction-oriented programming means that when designing an important class, the class should not be oriented toward concrete classes, but toward abstract classes (that is, the important data in the designed class is the object declared by the abstract class , not the concrete class declaration. Object)
  • If a member variable is modified as final, it is a constant, and the user must specify the value of the constant when declaring it.
  • Except for the java.lang.Object class, any class has and only one parent class

Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here

Chapter Six

Reference for knowledge points related to interfaces: Detailed explanation of interfaces

  • A source file can define both an interface and a class.
  • Use the keyword interface to define the interface
  • An interface can be understood as a special class, which is composed of global constants and public abstract methods.
  • There are only constants in the interface , no variables
  • There can only be abstract methods in the interface , not non-abstract methods (JDK7 version)
  • The interface can have abstract methods or default-modified methods with method bodies (JDK8 version)Does it conflict with the previous one? default is a non-abstract method
  • Allows the interface to have specific methods , provided that the method needs to be modified by the default or static keywords
  • You cannot use final or protected to modify interfaces and methods in interfaces.
  • The interface can be modified with public. The access permissions of the methods in the interface must be public (public is allowed to be omitted)
  • The interface defaults to public interface (×) Why, the default is a friendly interface?
  • The methods in the interface cannot be modified with private or protected, but the interface can be modified with abstract.

  • The void f(); method declaration in the interface is equivalent to public abstract void f(); that is, the method defaults to an abstract method with public access rights.
  • Int x =10 in the interface is equivalent to public static final int x = 10, which means it is a constant by default.
  • An interface can have multiple parent interfaces (that is, inherit multiple interfaces), for example: interface A extends Com1,Com2
  • A class uses the keyword implements to implement one or more interfaces. The class declaration class A implements B,C means that class A implements two interfaces B and C.
  • Class declaration class A extends B implements C means that class A is a subclass of class B and implements the C interface.
  • A class cannot implement the same interface repeatedly, for example, class A implements B,B
  • inferfce A extends B means A is a sub-interface of B
  • The public interface can be implemented by any class
  • Final classes can implement interfaces(final is a class, and a class can implement an interface)
  • Public classes cannot implement any friendly interfaces
  • If a class is in the same package as a friendly interface , it is allowed to implement the interface

  • If a non-abstract class implements an interface, the class must override all abstract methods
  • If a class declares to implement an interface but does not override all the methods in the interface , then the class must be an abstract class , which means that the abstract class can either override the methods in the interface or directly own the methods in the interface.
  • The sub-interface will inherit all methods and constants in the parent interface
  • You can directly access the constants in the interface using the interface name.
  • Interface is also an important data type in Java. Variables declared with interface are called interface variables.
  • Interface variables can store references to instances of classes that implement the interface (object references)
  • You can assign an int data to the interface variable (x)
  • Assign the reference of the object created by the class that implements an interface to the interface variable declared by the interface, then the interface variable can call the interface method implemented by the class
  • Interface variables can store references to any objects (×)
  • Interfaces can only have constants , not variables. Abstract classes can have both constants and variables
    [Abstract classes can have both constants and variables]
  • Constants in the interface must specify an initial value
    [ Constants in the interface do not need to specify an initial value, the compiler will provide a default value (×)]
  • The class and the interface it implements are not necessarily in the same package

Insert image description here
Insert image description here
Insert image description here

Chapter VII

chapter eight

  • The String class is a final class and cannot have subclasses.
  • "\hello" is the wrong string constant, "\hello" is the correct string constant
  • The value of the expression "\t\nABC".length() is 5 [\t and \n are counted as one character respectively]

  • The value of the expression "A" .compareTo ("B") is a negative number
    [.compareTo(), comparison, former > latter → positive number, former < latter → negative number, former = latter → 0]
  • The value of the expression "I love this game" .contains ("love") is true
    [.contains(), determines whether it contains a certain string]
  • The value of the expression "RedBird".indexOf("Bird") is 3
    [.indexOf(), returns the subscript index of the target string, or -1 if it does not exist]
  • Integer.parseInt("12.9"); will trigger a NumberFormatException exception
    [Integer.parseInt() string → integer, 12.9 is a floating point number]
  • The value of the expression "D:/java/book/E.java" .lastIndexOf
    ("/") is 12 [.lastIndexOf(), returns the last index position where the target character appears]
  • The value of expression "89762.34" .matches ("[0-9.]+") is true
    expression "3.14" .matches ("[0-9]+[.]{1}[0-9]+" ) is true
    [ matches() , returns whether it matches the specified string]
  • The value of the expression "89762.34".startsWith("89") is true
  • The value of the expression "89762.34".endsWith("34") is true

  • The value of the expression "java" .equals ("java") is true
    [.equals(), which determines whether the values ​​are equal, is different from == (determines the address), the expression new String("abc")== "abc" The value is false (the addresses are different)]
  • The value of the expression "bird".contentEquals("bird") is true
    [ equals() and contentEquals() methods ]
  • The value of the expression "Bird" .equalsIgnoreCase ("bird") is true
    [.equalsIgnoreCase(), ignore case to determine whether they are equal]
  • The value of the expression new String("abc").contentEquals ("abc") is true
  • The value of the expression new String("abc").equals ("abc") is true
  • The expression "abc" == "abc" evaluates to true
  • The value of the expression new String("abc") == new String("abc") is false

  • The nextToken() method of the StringTokenizer object returns a String object
  • The countTokens() method of the StringTokenizer object returns an int type data
  • The character sequence of a String object cannot be modified.
  • The character sequence of a StringBuffer object is modifiable

  • The nextInt(int n) method of the Random object randomly returns an integer between [0, n)

Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here
Insert image description here

Guess you like

Origin blog.csdn.net/m0_50609545/article/details/118017091