Getting started with Java -- some common basic knowledge points

Article directory

Preface

In order to consolidate the knowledge learned, the author tried to start publishing some study note blogs to facilitate future review. Of course, it would also be great if it could help some newbies learn new technologies. The author is a rookie. If there are recording errors in the article, readers and friends are welcome to criticize and correct them.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, you are welcome to ask me in the comment area)

discover treasure

A few days ago I discovered a giant artificial intelligence learning website. It is easy to understand and humorous. I couldn’t help but share it with everyone. 【Treasure Entrance】.

Chapter 1. Java Language Overview + Java Language Basics

1. Program documentation style and comments

  • Consistent indentation and whitespace: Binary operators should have a space on either side;
  • Block style: Code blocks are roughly divided into two styles, one is line mode and the other is sub-line mode;
  • Use in eclipseCTRL+SHIFT+F shortcut keys can format the source code; useALT+SHIFT+R Select the same name entry with one click;

2. Read data from the keyboard

To read data from the keyboard, you can use theScanner classnextInt method or nextDouble method or next method (character type) :

  1. Create an object of Scanner class;
  2. Takes standard input System.in as parameter;
  3. After getting the Scanner object, you can call its related methods to obtain data.

Insert image description here

3. Variables and assignment

Java has two types of variables:basic typevariables andreference typesVariable of .

  • Basic type Variables of include numeric type (integer type and floating point type), Boolean type and character type;
  • Variables of reference type include classes, interfaces, enumerations, arrays, etc.

4.java identifier

  • In programming, identifiers are used to name variables, methods and classes. There are two naming methods:
  • In java programs, class names and interface names generally use the PascalCase spelling, and should be named with words;
  • Variable names and method names generally use camelCase spelling.
  1. PacasalCase is called Pascal spelling, which means capitalizing the first letter of all named words and then connecting them directly without connectors between words, such as NumberOfStudent;
  2. cameCase is called camel spelling, which differs from PacasalCase's spelling in that the first letter of the first word is lowercase, such as fristName.

5. About data types and logical operators

  • To represent long type literal value, you can add l after it orL;
  • The scientific notation form of floating point type: for example, 256e3 represents 256*10^3;
  • If it represents float type literal data, it must be followed by F orf;
  • ‘\n’ means line break;
  • Boolean: "bollean";
  • i++ Display target userRe-add after completion of change 1,< a i=4>++i DisplayPre-addition 1 Re-exploitation flow change amount;
  • "&&" and "||" are short-circuiting operators, while "&" and "|" are non-short-circuiting operators. The difference between them is: short-circuit operator, when using "&&" for "AND" operation, if the value of the first (left) operand is false, the entire expression can be judged; while using The short-circuiting operator "&" requires solving both sides of the expression. Same reason. ,
  • For XOR "^", the expression is true when the two operands are one "true" and one "false", otherwise the expression is false;
  • The compound operator is the unit of "whole", for example, a+=5*++a/5+2 is equivalent to a=a+(5*++a/5+2);
  • Priority of operators: auto-increment and auto-decrement> logical NOT> addition, subtraction, multiplication and division operations> comparison operators> logical and logical OR> assignment operator> compound operator;
  • Forced data type conversion: The syntax is to give the target data type to be converted in parentheses, followed by the data type to be converted.

6. Limit the number of decimal places in the output: (use the third option)

Insert image description here

7. About labeled loop control statements

  • Labeled break can be used in loop structures and labeled statement blocks, while labeled continue can only be used in loop structures;
  • Labeled break and continue statements cannot jump to unrelated blocks of statements.

Chapter 2. Definition of classes, objects and arrays

1. Method design

  • Preliminary understanding (first impression)

Insert image description here
Insert image description here

  • Standard definition format for methods

Insert image description here

  • Three formats for method calls

Insert image description here

  • Flow diagram of method calling

1. Find the method
2. Pass parameters
3. Execute the method body
4. Transfer the method The return value is brought back to the calling location of the method

Insert image description here

  • Compare methods with parameters and without parameters

Insert image description here

  • Method overloading

Insert image description here

2. Facing the object - preliminary understanding

  • Facing the object and facing the process

Insert image description here

  • classes and objects
    Insert image description here

2. Class definition

Insert image description here

成员变量一般不赋值,在创建对象后再赋值

3. Create objects and their use

Insert image description here

4. Heap and stack memory

  • The memory map of an object
  1. Before running the method, the method area must first have data, and what is saved is ".class";
  2. The main method is executed first, and entering the stack space is called pushing or pushing;
    After the method is executed, it is called popping, following the principle of "first in, last out"; a>
  3. The left side of the equal sign is like a local variable, and the object name is actually a variable name;
  4. All new things are in the heap. The contents of new things can be found in the method area;
  5. For an object, its member method saves an address value, pointing from the address in the heap to the address in the method area;
  6. Creating an object involves the transfer of an address. The object can be found according to the address, and the object can also find the corresponding content through the address;
  7. Once the main method ends, the entire program will stop and all memory will be gone;

Insert image description here

  • Memory map of two objects using the same method (duplication of ○1)

Insert image description here

  • Two object references point to the memory map of the same object (note the arrow in the stack memory from phone one to phone two)

Insert image description here

5. Use objects as method parameters

Insert image description here
Insert image description here

6. The difference between member variables and local variables

Insert image description here

注意:方法参数可以直接打印(使用),因为调用方法必然会对参数赋值。

7. Three major characteristics of objects - encapsulation

  • method encapsulation
    Insert image description here

  • Encapsulation of private keyword

Insert image description here
Insert image description here

8.Application of this keyword

  • This keyword is mainly used in the following three situations;
  1. Solve the problem of local variables and member variables having the same name;
  2. Solve the problem of method parameters and member variables having the same name;
  3. Used to call another constructor method of this class.
  • Java language stipulates that this can only be used in In non-static methods (instance methods and constructor methods), it cannot be used in static methods. In fact, when an object calls a non-static method, a reference is passed to the method. This reference is the object itself, Use this to represent in the method body;

9.Construction method

Insert image description here

10. Define a standard class

Insert image description here

11. Static keyword static

  • A preliminary understanding of the static keyword static

Insert image description here

  • Static keyword modifies member variables

Insert image description here

  • Static keyword modifies member methods

Insert image description here

  • Static static memory map

Insert image description here

  • static code block

Insert image description here

12. Permission modifiers

  • Permission modifiers in class definitions
  1. Public: If a class is modified with public, it is called a public class, and the public class can be used by classes in any package;
  2. Without the public modifier, the class can only be used by other classes in the same package;
  3. If a class is modified with abstract, the class is called an abstract class;
  4. If the final modifier is used, the class is the final class.
  • Access modifiers for variables
  1. Public: If a member variable is modified with public, the variable is called a public variable, and the public variable can be used by any method;
  2. Without access modifiers, the variable can only be accessed by classes in the same package;
  3. Variables modified with private are called private variables, and private variables can only be accessed by methods in the same class;
  4. Variables modified with protected are called protected variables. Protected variables can be accessed by classes or subclasses in the same package;
  5. Instance variables and static variables: If a variable is modified with static, the variable is called a final variable, also called a class variable. Variables that are not modified with static are called instance variables;
  6. Variables modified with final are called final variables, also known as identifier constants. Constants can be assigned a value when they are declared, or they can be assigned an initial value later. Once assigned a value, it cannot be changed;
  • Method access modifiers
  1. public: Methods modified with public can be called in any class;
  2. private: Private modified methods can only be called in the same class;
  3. protected: protected methods can be called in the same class, classes in the same package and their subclasses;
  4. If a method has the default access modifier, the package is said to be accessible, that is, it can be accessed by methods of the same class and classes in the same package;
  5. Methods that are not modified with static are called instance methods, and methods that are modified with static are called static methods.
    6. final and abstract methods
    The method modified with final is called the final method. The final method cannot be overridden. Method Overriding is related to inheritance. Methods modified with abstract are called abstract methods;
  • Summary

Insert image description here

Chapter 3. Array and character operations

1. Definition of one-dimensional array:

  • declare array;

Declaring an array tells the compiler the array name and array element type. There are two equivalent forms:

   Elementype []arrayName
   Elementype arrayName[]

Note: The array declaration cannot specify the number of array elements, which is different from c/c++
In the Java language, the array is a reference data type, which means that the array is an object , the array name is the object name (or reference name). An array declaration actually declares a reference variable. If the array elements are all reference types, the array is said to be an object array. All arrays inherit the Object class, therefore, all methods of the Object class can be called.

  • Create an array:

Array declaration only declares an array object reference, while creating an array allocates storage space for each element of the array. Create an array using the new statement. The general format is:

ArrayName = new elementType[arraysize]

The declaration and creation of an array can be written in one statement, such as:

Double []marks = new double[5]; 
  • Initialization of array elements:

When declaring an array, you can use an initializer to initialize the array elements, giving the value of each element of the array in a pair of curly braces. This method is suitable for situations where there are few array elements. This kind of initialization is also called static initialization.

Double[] marks = new double[]{
    
    1,2,3,};

The size of the array created by this method cannot be specified. The system determines the array size based on the number of elements. In addition, you can add a comma to the last array element to facilitate supplementation. It can also be written in a simpler form;

Double[] marks = {
    
    1,2,3,};

2. Enhanced for loop

If your program only needs to access each element in the array sequentially, you can use an enhanced for loop. Enhanced for loops can be used to iterate over each element of arrays and separate collections. Its general format is:

For(type inedntifier: expression) 

(array element data type) (each element of the array) (array or collection object)

3. Array parameters and return values

Arrays can be used as parameters and return values ​​of method parameters. Array objects can be passed as parameters to methods.

Insert image description here
Insert image description here

4. Definition of two-dimensional array

  • Declaration of two-dimensional array
Elementype[][] arrayName;
Elementype arrayName; [][]
Elementype[] arrayName[];
  • Creation of a two-dimensional array
    Creating a two-dimensional array is to allocate storage space for each element of the two-dimensional array. The system first allocates reference space for high dimensions, and then allocates space for low dimensions sequentially. There are two ways to allocate space:

Directly allocate space for each dimension, such as:

int[][]matrix = new int[2][3];

Assign values ​​to different dimensions one by one, such as:

int[][] matrix = new int[2][1];//先为一维分配空间
Matrix[0]=new int[3];//再为二维分配空间
Matrix[1]=new int[3];
  • Initialization of two-dimensional array

For two-dimensional arrays, you can also use an initializer to initialize the array elements while declaring the array. For example:

int[][] matrix={
    
    {
    
    15,26,20,89},{
    
    23,80,67,54},{
    
    60,12,23,45}};
  • Irregular two-dimensional array

Java's array is an array of arrays, that is, the array elements are also an array. When declaring a two-dimensional array, you can specify only the size of the first dimension, and each element of the second dimension can specify a different size. like:

String [][]cities = new String[2][];
Cities[0] = new String[3];
Cities[1] = new String[2];  

5. Format output

You can use the System.out.printf() method to display formatted output on the console. The format is as follows:

Public printStream printf(String format,object…args) 

The parameter format is a format control string, in which the format character can be embedded to specify how the parameters are output: args is the output list, and the parameters can be basic data types or reference data types. like:

System.out.println(%7.2d,a);

Chapter 4. Inheritance

1. Inheritance - preliminary understanding

(1) Preliminary understanding of inheritance

Insert image description here

2. Inherited format

Insert image description here

3. Characteristics of member variable access in inheritance

Insert image description here
Insert image description here

Insert image description hereInsert image description here

4. Distinguish three variables with the same name in subclass methods

Insert image description here
Insert image description here

Insert image description here

Insert image description here

5. Access characteristics of member methods in inheritance

Insert image description hereInsert image description here

Insert image description here

6. Overwriting and rewriting of methods

  • basic concepts
    Insert image description here

  • Precautions

    Private method cannot be overridden. Only non-private instance methods can be overridden. If a method defined in the subclass is private in the parent class, the two methods are irrelevant;
    The static method in the parent class can Inherited but cannot be overridden. If the subclass defines a method that is exactly the same as the static method in the parent class, then the method in the parent class is hidden. Hidden static methods in the parent class can still be called using the "class name. method name ()" form.

Insert image description here
Private methods cannot be overridden. Only non-private instance methods can be overridden. If a method defined in a subclass is private in the parent class, the two methods are irrelevant;

Static methods in parent classes can be inherited, but cannot be overridden. If the subclass defines a method that is exactly the same as the static method in the parent class, then the method in the parent class is hidden. Hidden static methods in the parent class can still be called using the "class name. method name ()" form.

  • Application scenarios

Insert image description here

7. Access characteristics of constructor methods in overwriting and rewriting

Insert image description here

8. Comparison of super keyword and this

  • Three uses of the super keyword

Insert image description here

  • Usage of this keyword

Insert image description here
Insert image description here

  • Memory map of super keyword and this keyword

Insert image description here

9.Characteristics of java inheritance

Insert image description here

Subclasses inherit non-private member variables and member methods from the parent class;

If the extends keyword is omitted when defining a class, the defined class is a direct subclass of the java.lang.object class.

10. Abstract methods and abstract classes

  • Initial understanding of abstract concepts
    Insert image description here

  • Definition format of abstract methods and abstract classes

Insert image description here

  • Use of abstract methods

Insert image description here

  • Things to note about abstract classes and abstract methods

Insert image description here

11.final modifier

  • final modified class

If a class is modified with final, the class is the final class, and the final class cannot be inherited;

  • final modified method

If a method is modified with final, the method cannot be overridden by subclasses;

  • final modified variable

Variables modified with final include class member variables, method local variables and method parameters. If a variable is modified with final, the variable is a constant value variable and cannot be changed once it is assigned a value.

Chapter 5. Interface

1. Interface - preliminary understanding

Insert image description here

2. Basic definition format of interface

Insert image description here

3. Definition of abstract methods of interface

Insert image description here

4. Implementation of abstract methods in interfaces (implementation of interfaces)

Insert image description here

5.Default methods in interfaces

  • Preliminary understanding (interface upgrade, AB-like error reporting)

Insert image description hereInsert image description here

  • Implementation of the default method in the interface (class AB remains unchanged, but the default method can be called)

Insert image description hereInsert image description here

Insert image description here
Insert image description here

Insert image description here

(Slightly different from overriding abstract methods)

Summarize

You are welcome to leave messages, exchange comments and make criticisms. If the article is helpful to you or you think the author's writing is good, you can click to follow, like, collect and support it.
(The reference source code of the blog can be found in the resources on my homepage. If you have any questions during the learning process, you are welcome to ask me in the comment area)

Guess you like

Origin blog.csdn.net/HHX_01/article/details/133785745
Recommended