Java learning notes


1. Comment statement

(1) Paragraph comment: "/ * * /" (2) statement comment "/ /"

2. Keywords

Words with special meanings: 1. All lowercase; 2. Special color mark

3. Data type

Numerical typeSymbol / byteNon numerical typeSymbol
Floating point numberfloat(4),double(8)classclass
characterchar(2)numerical value[ ]

4. Precautions for variable use

Variables cannot be defined repeatedly; Assignment is required before direct use; Add L after defining long type variable; Floating point number is double by default. To define float, f must be added after the variable;

5. Identifier

It consists of numbers, letters, underscores and dollar characters;

Cannot start with a number, cannot be a keyword, and is case sensitive;

Small hump nomenclature: one word: the first letter is lowercase, multiple words: the first lowercase, other initials are uppercase

Name of the big hump: one word: the first letter is capitalized, multiple words: the first letter is capitalized, and other initials are lowercase

6. Type conversion

Automatic conversion: types with small data range - > types with large data range;

Cast type: target cast data type = (variable symbol) data

7. Addition of characters

Rank order: byte, short, char - > int - > long - > float - > double

8. Addition of string

Concatenate the strings and the string connector;

9. Operator

+=Assignment after addition
-=Assignment after subtraction
*=Assignment after multiplication
/=Assignment after division
%=Post modular assignment
++Self addition
Self subtraction
==,!=,>,>=,<,<=Relational operation
&&Short circuit and (the first one is true, and the subsequent logic judgment is not executed)
||Short circuit or (the first one is true, and the subsequent logical judgment will not be executed)
a>b?a:bture:a; false:b

10. Data input

Scanner steps:

1. Import package

import java.util.Scanner

2. Create object

Scanner sc=new Scanner( //sc is the variable name, which can be changed, and others are not allowed to be changed

3. Accept data

int i=sc.nextInt()		//Only variable names are variable

11. Process control statement

ifIf (relational expression) {statement body};
if...elseIf (relational expression) {statement body} else {statement body 2}
if...else if...else
switchSwitch (expression) {case 1: statement body; break;}
forFor (initialization statement, conditional statement, conditional control statement)
whileWhile (conditional judgment statement) {loop body statement:}
RandomGenerate random number

random usage steps:

1. Guide Package

import java.util.Random;

2. Create object

Random r=new Random();

3. Get random number

int number=r.nextInt(10);//[0,10) integer

12. Array definition

int[] arr  //Data type [] variable name

13. Array dynamic initialization

Format: data type [] variable name = new data type []

14. Method definition and call

public static void Method name(){Method body}
public static void isEvenNumber(){Method body}
public class fdemo {
    public static void main(String[] args){                 //main method 

    public static void isEvenNumber(int num){



It is used to observe the program execution process, also known as breakpoint debugging

16. Classes and objects (encapsulation, inheritance, polymorphism)

(1) Class definition:

public class Class name{
    //Member variable
    //Member method
public class phone {
    //Member variable
    String brand;
    int price;
    //Member method
    public void call(){
    public void message(){
        System.out.println("send message");

(2) Member variables and local variables

Local variables: variables within methods in a class; stack memory; generated and disappeared with method calls and disappearances

Member variables: variables outside the method in the class; heap memory; generated and disappeared with object calls and disappearances

(3) private: it cannot be called directly from outside, but can be called indirectly by methods

(4) this: used to point to object variables and refer to member variables

17. Construction method

//Modifier class name (parameter). When the object is called, the constructor is called
public student(){

18.API overview

API (Application Programming Interface): application programming interface


String construction method

Method nameexplain
public String()Create a blank string object that contains nothing
public String(char[] chs)Creates an object based on the contents of a character array
public String(byte[ ] bys)Creates an object based on the contents of a byte array
String s ="abc"Create a string object by direct assignment
public class Stringdemo {
    public static void main(String[] args){
        //public String() creates a blank string object without any content
        String s1=new String();
        //public String(char[] chs) creates an object based on the contents of the character array
        char[] chs={'a','b','c'};
        String s2=new String(chs);
        //Public string (byte [] bytes) creates an object based on the contents of the byte array
        byte[] bys={97,98,99};
        String s3=new String(bys);
        //String s ="abc" creates a string object by direct assignment
        String s4="abc";

Features: assign values in mode 4, the calling class objects are regarded as the same, and new is unique;

String comparison

==Compare (compare addresses) with the equals method;

Traversal string

1. Enter string / scanner;2. Return char value at index; 3. Return length();


Variable string class, splicing strings, saving space,

StringBuilder sb=new StringBuilder();
Method nameexplain
Public StringBuilder append (any type)Add data and return the object itself
public StringBuilder reverse()Returns the opposite sequence of characters

21. Conversion between StringBuilder and String

StringBuilder->String: to String( )

String->StringBuilder: public StringBuilder(String s)

22. Collection foundation ArrayList

Features: it provides a storage type with variable storage space, and the storage data capacity can be changed


Construction method and addition method:

Method nameexplain
public ArrayList( )Create an empty collection object
public boolean add(E e)Appends the specified element to the end of this collection
public void add(int index, E element)Inserts the specified element at the specified location in this collection
 public static void main(String[] args){
        ArrayList<String> array=new ArrayList<>();

Common methods of ArrayList collection:

Method nameexplain
public boolean remove(object o)Delete the specified element and return whether it is successful
pubic E remove(int index)Deletes the element at the specified index and returns the deleted element
public E set(int index,E element)Modify the element at the specified index and return the modified element
public E get(int index)Returns the element at the specified index
public int size()Returns the number of elements in the collection
import java.util.ArrayList;

import javax.lang.model.element.Element;

public class ArrayListdemo {
    public static void main(String[] args){
        ArrayList<String> array=new ArrayList<>();
        array.remove("hello");//Deletes the specified element
        System.out.println(array);//Deletes the specified index element
        System.out.println(array);//Modify the specified element

23. Succession

Make the subclass have the attributes and methods of the parent class, and improve the reusability and maintainability of the code. Format:

public class Subclass name extends Parent class name()

Variable access features: method internal - > this class member - > parent class, access order;

Cannot inherit multiple classes at the same time; supports multi-layer inheritance;

​ super

When accessing, skip method internal variables and access parent class member variables;

super.(Parent class member variable)
keywordAccessing member variablesAccess construction methodAccess member method
thisthis. Member variablethis(...) accesses the constructor of this classthis. this class member method (...)
supersuper. Parent class member variablesuper(...) accessing the parent class constructorsuper. Parent class member method (...)

All constructors in the subclass will access the parameterless constructors in the parent class by default;

When there are no parameterless constructors in the parent class and only parameterless constructors:

1. Use the super keyword to display the construction method with parameters calling the parent class;

2. Provide a parameterless construction method in the parent class;

24. Method rewriting

A subclass declares a method with the same name as the parent method. On this basis, you can call the parent method with super.

Subclasses cannot override private methods of parent classes;

Subclass method access permission cannot be lower (public > Default > private) modification

1. The final modifier means that the data value of the basic type cannot be changed

2. Modify the reference type, its address value cannot be changed, and the content in the address can be changed;


Static variable

It is suggested to use class modification directly;

Non static member methods can access static and non static;

Static member methods can only access static variables and methods;

27. Polymorphism

Different forms of the same object at different times;

animal a=new cat();

The parent class reference points to the child class object;


(1) Accessing member variables in polymorphic form is actually accessing parent class member variables

(2) The member method views the contents of the class object on the left side of the compilation, and executes the observation code on the right side

28. Polymorphic transformation

Upward and downward transformation;

Down (parent to child):

Animal a=new Cat();
Cat c=cat(a);//Force conversion to subclass object

29. Abstract classes

A method without a method body should be defined as an abstract method, and if there is an abstract method in a class, the class must be defined as an abstract class;

public abstract class Animal{//Abstract class declaration
    public abstract void eat();//Abstract method
public abstract class  Animal {
    public abstract void eat();//Abstract classes do not necessarily have abstract methods, but abstract methods must be in abstract classes


public class Cat extends Animal {

    public void eat() {
        System.out.println("Cats eat fish");

public class animaldemo {
    public static void main(String[] args){
        Cat a=new Cat();;


1. Member variables:

(1) can be variable

(2) it can also be a constant

2. Construction method

(1) there is a construction method, but it cannot be instantiated

(2) initialization for subclasses to access parent class data

3. Membership method:

(1) there can be abstract methods or non abstract methods

30. Interface

Overview: interface is a public standard. As long as it meets the standard, everyone can use it

The interface in java is more embodied in the abstraction of behavior;

Tags: Java Back-end

Posted on Sun, 05 Dec 2021 23:31:34 -0500 by blurredvision