Javase (write something that you may not remember clearly in your study. It is probably only suitable for your own use)

JAVA01

public class Demon01{
  static string name;//Adding static can be used directly below
  int age;//This requires new
    public static void main(String [] args){
    Demon01 demon01=new Demon01();
      System.out.println(demon01.age);
      System.out.println(name);
    
  }
}

Constant:

final constant name = value;

final pi =3.14;

static final double PI=3.14;

final static double PI=3.14; Same, the front belongs to modifier, and the front and back are not distinguished

Variable naming rules:

All variables, methods and class names: see the meaning of the name;

Class member variables: initial lowercase and hump principle: monthSalay.

Local variables: ditto

Constants: all uppercase and underscore: MAX_VALUE

Class name: initial capital and hump

Method name: small hump

JAVADoc

/**

*/

@author

@version

@since indicates that the earliest JDK version is required

@param parameter

@return

@Exceptions thrown by throws

Scanner

new Scanner(System.in)

Scanner scanner =new Scanner(System.in);

System.out.println("receive by next.line:");

String str =scanner.nextLine()

scanner.close();

JAVA method

heavy load

Overloading is a function with the same function name but different formal parameters in a class

Overloaded rules:

  • Method names must be the same.
  • The parameter list must be different (different number or type, different order of parameters, etc.)
  • The return types of methods can be the same or different
  • Just different return types are not enough to overload methods

Implementation theory: if the method names are the same, the compiler will match one by one according to the number of parameters and parameter types of the calling method to select the corresponding method. If the matching fails, the compiler will report an error

Variable parameter

JDK1. Starting from 5, JAVA supports passing variable parameters of the same type to a method.

In the method declaration, add an ellipsis (...) after specifying the parameter type

Only one variable parameter can be specified in a method. It must be the last parameter of the method, and any ordinary parameter must precede its declaration.

public static void printMax(double...numbers){
  if (numbers.length == 0){
    System.out.println("NO argument passed");
    return;
  }
  double result = numbers[0];
  for (int i = 1;i < numbers.length;i++){
    if (numbers[i] > result){
      result =numbers[i];
    }
  }
  System.out.println("The max Vaule is" + result);
}

recursion

Recursion consists of two parts:

  • Recursive header: when not to call its own method. If there is no header, the recursion will fall into an endless loop
  • Recursive body: when calling itself

array

Declaration and creation of array;

dataType [] arrayRefVar;
dataType arrayRefVar[];
//The Java language uses the new operator to create arrays:
dataType[] arrayRefVar = new dataType[arraySize];

Java memory – 1 heap: * store new objects and arrays

* can be shared by all threads without storing other object references

-- 2 stack: * store the basic variable type (the specific value of this basic type will be included)

* variable of the reference object (the specific location of the reference in the heap will be stored)

-- 3 method area: * can be shared by all threads

* contains all class and static variables

System.out.println(Arrays.toString(a));//Output a all the values of this array, enclosed in square brackets.

JAVA object oriented

Object oriented programming (oop)

The essence of object-oriented programming is to organize code in the way of class and data in the way of object

Three characteristics: encapsulation, inheritance and polymorphism..

static

static can be called directly

If there is no static in the modification, you need to instantiate this class with new: for example:

public class Demon02{
  public static void main(String[] args){
    Student student = new Student();
    student.say();
  }
}
public class Student{
  public void say(){
		System.out.println("speak");
  }
}

constructor

A class will have a method even if it doesn't write anything. Construction method.

Constructors are also called construction methods; It must be called when creating objects, and the constructor has the following two characteristics:

  • Must be the same as the name of the class
  • There must be no return type and void cannot be written
public class Person{
	String name;
	//Nonparametric structure
	public Person(){
		this.name="cqi";//initialize value
	//Tragic structure
	public Person(String name){
		this.name = name;
	}
	}
}
public class Application{
  public static void main(String[] args){
    Person person =new Person();
    Person person = new Person("cqi");//Have reference
  }
}

command+n

Object oriented (encapsulation)

public class Student{
  //Property private
  private String name;
  private int id;
  private char sex;
  public String getName(){
		return this.name;
  }
  
  public String setName(String name){
    this.name=name;
  }
}
public class Application{
  public static void main(String[] args){
    Student s1 =new Student();
    s1.name=.     //This is wrong and cannot be called
      
    s1.setName("cqi");
    System.out.println(s1.getNmae());
  }
}

inherit

Classes in JAVA only have single inheritance, not multiple inheritance.

The essence of inheritance is to abstract a group of classes, so as to realize better modeling of the real world

extends means "expand". A subclass is an extension of the parent class.

Inheritance is a kind of relationship between classes. In addition, the relationship between classes includes dependency, composition, aggregation and so on.

Two classes of inheritance relationship, one is a child class (derived class) and the other is a parent class (base class). The subclass inherits the parent class and is represented by the keyword extends

object class

super class

Method rewrite

public class Person{
  private age = 10;
  
  public int money=10000000; 
  public void say(){
    System.out.println("Shuo Hua");
  }
  public int getAge(){
    return age;
  }
  public void setAge(int age){
    this.age=age;
  }
  
}
//Student is person
public class Student extends Person{
  
}

public class Teacher extends Person{
  
}

public class Application{
  public static void main(String[] args){
		Student student =new Student();
    student.say();
    System.out.println(student.money);
    student.age;//This is wrong
    student.getAge();
  }
}

Cannot inherit properties private to a parent class.

super

public class Person{
  protect String name = "zhyi";
  
}
public class Student extends Person{
  private String name ="cqi";
  public void test(String name){
		System.out.println(name);//Refers to the name passed into the test method
    System.out.println(this.name);//Refers to the name in the student class
    System.out.println(super.name);//Refers to the name in the Person class
    
  }
}
public class Application{
  public static void main(String[] args){
    Student student = new Student();
    student.test("hu");
		
  }
}
The output result is:
  hu
  cqi
  zhyi
  
  

The parent constructor is above the child class

super note:

1.super calls the parent class construction method, which must be in the first instance of the constructor

2.super can only appear in subclass methods or constructor methods

3.super and this cannot call construction methods at the same time

this: the object of the caller itself

super can only be used under inheritance conditions

Method rewrite

Rewriting is the rewriting of methods, which has nothing to do with properties

public class Application{
  public static void main(String[] args){
    A a =new A();
    a.test;
    B b =new A();
    b.test
  }
}
public class A extends B{
  public static void test(){
    System.out.println("A->text");
  }
}
public class B {
  public static void test(){
    System.out.println("B->text");
  }
}

Output as

A-〉text

B->text

public class A extends B{
  public void test(){
    System.out.println("A->text");
  }
}
public class B {
  public void test(){
    System.out.println("B->text");//The subclass overrides the method of the parent class
  }
}

Output is:

A->text

A->text

This is rewritten

Rewriting is only about non static

Static methods are very different from non static methods

The invocation of static methods is only related to the definition on the left

Subclasses that need to be overridden: subclasses that need to be overridden, methods that need to be overridden

1. The method name must be the same

2. The parameter list must be the same

3 modifier: the scope can be expanded: public > protected > Default > private

4 exception thrown, scope: can be reduced, but not expanded.

When overridden, the methods of the subclass and the parent class must be consistent, and the method bodies are different

Why rewrite:

1. The function of the parent class and the subclass are not necessarily required or satisfied

polymorphic

public class Student extends Person{
  public void run(){
    System.out.println("son");
      
      
  }
  public void eat(){
     System.out.println("eat");
  }
  
}
public class Person{
  public void run(){
    System.out.println("run")
  }
}
public class Appliction{
  public static void main(String[] args){
    //The actual type of an object is determined
    //new Student();
    //new Person();
    //The reference type pointed to is uncertain, and the reference of the parent class points to the child class
    Student s1 =new Student();
    Person s2 =new Student();
    s2.run();
    s1.run();
    s2.eat();//Error, cannot call
    s1.eat();
  }
}

Results: son

​ son

The methods that can be executed by an object mainly depend on the type on the left of the object, which has little to do with the right

//The methods that students can call are their own and inherit the parent class

//Person can point to subclasses, but cannot call methods private to subclasses

((Student)s2).eat(); Cast type conversion can be called

Precautions for polymorphism:

1. Polymorphism is the polymorphism of methods, and there is no polymorphism of attributes

2. There is a relationship between parent and child classes. Type conversion exception: ClassCastException

3 existence conditions: inheritance relationship, method needs to be rewritten, and parent class reference points to child class object!

1.static method belongs to class, which does not belong to instance

2final constant

3private

- these three cannot be overridden

instanceof

public class Person{
  
}
public class Student extends Person{
  
}
public class Teacher extends Person{
  
}
public class Application{
  public static void main(String[] args){
    //Object>Person>Student
    Object object = new Student();
    System.out.println(object instanceof Student);
    System.out.println(object instanceof Object);
    System.out.println(object instanceof Person);
    System.out.println(object instanceof Teacher);
    System.out.println(object instanceof String);
    
    Person person =new Student();
    System.out.println(person instanceof Student);
    System.out.println(person instanceof Object);
    System.out.println(person instanceof Person);
    System.out.println(person instanceof Teacher);
    //System.out.println(person instanceof String);// Compilation error
    
    
    
   Student student =new student();
    System.out.println(student instanceof Student);
    System.out.println(student instanceof Object);
    System.out.println(student instanceof Person);
   // System.out.println(student instanceof Teacher); Compilation error
    //System.out.println(student instanceof String);// Compilation error
    
  }
}

Result: True

​ True

​ True

​ False

​ False

true

true

true

false

true

true

true

High and low

Person student= new Student()

Low to high does not require forced conversion

However, this student cannot use the methods in the student class and needs to be cast into student type.

High to low:

Student student1=(Student) student;

student1 can use the methods in the student class

Converting a subclass to a parent class may lose some methods

static

public class Person{
  {
    System.out.println("Anonymous code block");//Can be used to assign initial values
  }
  static{
    System.out.println("Static code block");//Execute only once
  }
  public Person(){
    System.out.println("Construction method");
  }
  public static void main(String[] args){
    Person person1 = new Person();
    Person person2 = new Person();
  }
}

Output: static code blocks

Anonymous code block

Construction method

Anonymous code block

Construction method

abstract class

//Abstract abstract class
public abstract class Action{
	//Constraints. Someone helped us achieve it
  //Abstract, abstract method, only the name of the method, no method implementation
  
  public abstract void doSomething();
}
//Subclasses inherit abstract classes, and subclasses need to complete the implementation of methods
public class A extends Action{
  public void doSomething(){
    
  }
  
}

Abstract classes cannot be new. They can only be implemented by subclasses: constraints!

Abstract classes can write ordinary methods.

Interface

Interface: only specification! You can't write your own method, professional constraints!: Separation of constraints and Implementation: interface oriented programming

General category; Only concrete implementation

Abstract classes: both concrete implementations and specifications (abstract methods) exist

The keyword of declaring a class is class, and the keyword of declaring an interface is interface

All definitions in the interface are actually Abstract public

All interfaces need implementation classes

Multiple inheritance can be realized by using interfaces

public interface TimeService{
  void timer();
}
public interface UserService{
  void add(String name);
  void delete(String name);
}
public class UserServiceImp1 implements UserService,TimeService{
  //In idea, command+n. select override to override the method
  public void add(String name){
    
  }
  public void delete(String name){
    
  }
  public void timer(){
    
  }
}

effect:

1 constraint

2 define some methods for different people to implement

3public abstract

4public static final

5. The interface cannot be instantiated and has no construction method

6implements can implement multiple interfaces

abnormal

There is an important subclass runtimeException (runtime Exception) in the Exception branch

  • ArrayIndexOutOfBoundsException (array subscript out of bounds)
  • NullPointerException (null pointer exception)
  • Arithmeticexception (arithmetic exception)
  • Missingresourceexception (missing resource)
  • Exceptions such as classnotfoundexception (class not found), which are not checked exceptions

Five keywords of exception handling:

try catch finally throw throws

int a=1;
int b=0;
try{//try monitoring area
  System.out.println(a/b);
}catch(ArithmeticException e){//Catch catch exception
  	System.out.println("The program is abnormal, b Cannot be 0");
}catch(Exception e){
  
}catch(Throwable t){
  
}finally{//Deal with the aftermath
  System.out.println("finally");
}
//You can not use fially, assuming that IO and resources are used to close

You can also actively throw exceptions

if(b==0){

​ throw new ArithmeticException();

}

Keywords: Java

Added by Ne.OnZ on Tue, 08 Mar 2022 17:03:09 +0200