Java learning notes

Posted by laanes on Tue, 01 Feb 2022 10:47:51 +0100

Overloaded function:

The function name must be the same, but the type / number of parameters of the function must be different
Cannot be distinguished by return value type

public static int add(int a, int b)	{	}
public static int add(double a, double b) {	}	// Can be overloaded with the function on the first line
public static double add(int a, int b) {	}	// Cannot be overloaded

Object and basic type:

Creating a new object is equivalent to a pointer in C language, which is called Reference
Object assignment is Reference assignment
The basic type assignment is a copy of the value

new displays the default value of an object:

After a new object is generated, if there is no assignment, there will be an internal default attribute value:

typeDefault value
short0
int0
long0
booleanfalse
char'\u0000'
byte0
float0.0f
double0.0d

Constructor:

The constructor name must be the same as the class name and has no return value
Every Java class must have a constructor, otherwise the compiler automatically adds an empty constructor
You can have multiple constructors as long as the formal parameter list is different

Information hiding:

The member property of the class is private
Class methods are common. Modify the attribute value public through the method

this pointer:

this pointer is responsible for pointing to:

  1. Member variables in this class
  2. Member methods in this class

You can also replace the constructor in this class

super():

super() is the constructor of the parent class.

For subclasses, if the first sentence of the constructor is not super, the compiler will automatically add a super();
If the first sentence of the constructor is super written by the programmer, it will not be added automatically.
And there can only be one super in a constructor.

as

public class A{
	public A(){
		System.out.println("dadadadadadad");
	}
	
	public A(int a){
		System.out.println("I am dadadadadadad");
	}
}

public class B extend{
	public B(){
		System.out.println("son son son son");
	}
	
	public B(int a){
		System.out.println("I am son son son son");
	}
	
	public static void main(String[] a){
		B obj1 = new B;
		System.out.println(">>>>>>>>>>>>>>>>>>>");
		B obj2 = new B(10);
	}
}

Output is:

dadadadadadad
son son son son
>>>>>>>>>>>>>>>>>>>
dadadadadadad
I am son son son son

If the class B definition is modified to:

public class B extend{
	public B(){
		System.out.println("son son son son");
	}
	
	public B(int a){
		super(a)
		System.out.println("I am son son son son");
	}
	
	public static void main(String[] a){
		B obj1 = new B;
		System.out.println(">>>>>>>>>>>>>>>>>>>");
		B obj2 = new B(10);
	}
}

Then the output becomes:

dadadadadadad
son son son son
>>>>>>>>>>>>>>>>>>>
I am dadadadadadad
I am son son son son

abstract class

Class: attribute (0 or more) + method (0 or more)

Complete class: all methods have implementation (method body)
Classes can have no methods, but if there are methods, experiments are needed. Such a class is a complete class, and a complete class can be instantiated: it is created by new

Abstract class: there are methods that are not implemented
Abstract classes are declared using the keyword abstract
Composition of abstract classes: member variables, concrete methods and abstract methods (declared with the keyword Abstract)

Subclasses can inherit abstract classes, but they must implement all abstract methods of the parent class. Otherwise, subclasses should also be defined as abstract classes

Interface

If all the methods of a class are not implemented, the class can be considered as an interface

public interface People {
	public void eat();
	public void sleep();
}

public interface Climb {
	public void climbTree();
}

Interfaces can inherit multiple interfaces, and methods that are not implemented will be superimposed.

Class implements (not inherits) an interface and must implement all methods of the interface. Otherwise, the class is defined as an abstract class

public class Man implements People {
	
	public void eat(){
		System.out.println("I can eat");
	}
	
	public void sleep(){
		System.out.printlb("I can sleep");
	}
	
}

public abstract class baby implements Climb {
	
	public void eat(){
		System.out.println("I can eat");
	}
	
	public void sleep(){
		System.out.printlb("I can sleep");
	}

	public abstract void climbTree();
}

Tips:

  1. double is a floating-point number, which is imprecise and cannot be used in switch statements
  2. The main method can be overloaded, called, inherited, and hidden
  3. The formal parameter name of the main method can be changed, but the type cannot be changed
  4. JDK contains JRE. JDK is the development kit and JRE is the running environment

Topics: Java