Review of basic knowledge of Java-01

How Java works:

Write source code - > compile by compiler - > output - > JVM (Java virtual machine, that is, Java environment) reads and executes the output content
Circular statement:

data type

typeNumber of byteslength
byte1-128~127
short 2-32768~32767
int 4-2147483648~2147483647
long 8-9223372036854775808L~9223372036854775807L
float4+/-3.4E+38F (6 ~ 7 significant bits)
double 8+/-1.8E+308 (15 significant bits)
char2ISO single character set
boolean2true or false

Data types in Java

Java data type refers to all the above types as eight basic data types, which are divided into numerical type, char type and boolean type The so-called number type is the specific number type, that is, the first six values
Where char represents Unicode characters and is also an unsigned data type It's just a character The boolean type is Boolean, which has only two values: true and false

While statement

The while statement means that it can always be executed when certain conditions are met. Its format is:

while(condition){
solution code
}

If statement, also known as branch statement

The if statement is similar to the former, but the addition of a code executed when the conditions are not met means that multiple conditions can be included, so it can be used as the former:

if(condition){
solution code
}

It can also write a condition and a solution that does not meet the condition:

if(condition){
solution code1
}else{
solution code2
}

It can also write in a variety of situations:

if(condition1){
solution code1
}else if(cndition2){
solution code2
}...
else if(conditionN){
solution codeN
}else{
solution code(N+1)
}

For statement

This is a very common statement. The difference from the former is that it is a real statement that can realize circulation. Just like the first while, if its conditions are not met, the program will be stuck in this statement until the end of the world... Its structure is as follows:

for(num=N;condition;num++ or num--){
Solution code
}

Switch statement

This statement means that we execute a statement under certain circumstances, which means that it is similar to the if statement. Its format is:

switch(expression){

case result1:
solution;
break;

case result2:
solution;
break;

case result3:
solution;
break;
...
case resultN:
solution;
break;
}

On the concept of object

Java is object-oriented, so as for objects, Java is water to water 🐟 Same, very important. Java uses objects to describe the whole world, so we need to understand the concept of objects:
The so-called object is the sum of the descriptions of any thing in the world, so each object includes two parts: the attribute (feature) of the object and the method (action) of the object. For example, for creatures, the attributes that a person can include include name, age, gender, height, weight, appearance, etc., while his actions can include eating, drinking water, sleeping, Singing, writing code and so on. Even for stone, we can describe it in the form of objects: in terms of attributes, we can use size, density, quality, shape and color, while in terms of actions, we can use rolling, falling, collision and so on. We can describe everything in the world.
Now that we have stated the basic understanding of text, let's study its code expression:

class Person{
String name;
String sex;
int age;
void code(){
...
}
}

The above is a class (the first three are attributes, and void code() is our method). In fact, it is the ontology of the object. Its class represents that it can represent a type of thing. Just like the class of human in the example, it can represent the type of human as a whole (after all, other creatures can't program), When we instantiate it, it becomes an object. At this time, it does not represent the whole type, but a separate and specific object. Its instantiation is:

Person p1=new Person();//Created a p1 object
p1.name="Rongxin Yang";
p1.sex="Male";
p1.age=22;
p1.code();

So we created a 22-year-old man named Rongxin Yang. Then we can call its programming method.
However, we found a problem, that is, although it is difficult to create an object p1 first and then assign values to its attributes one by one, the concept of construction method was born. The so-called construction method is a method first, and this method defines a format. Through the specified format of construction method, we can pass the attribute parameter of an object to the creation object. For example:

public Person(String name, String sex, int age) {  
    this.name = name;  
 this.sex = sex;  
 this.age = age;  
}
//At this point, if we want to create this type of object
Person p1=new Person("Rongxin Yang","Male",22);

Therefore, we find that we can achieve the purpose of injecting object information into objects in the process of instantiation. However, it should be noted that the construction method is only for attributes and does not involve methods. In addition, we should also note that when we do not add a constructor for a class, it will add an empty parameter constructor by default, but it is hidden because it is the default. This is why we can start instantiating objects without parameters. Once we create a constructor for a class, the constructor of the null parameter will be removed, so we can't create an object with null parameter at this time. If we want to continue to construct the instantiated object with null parameter, we need to declare the constructor of the null parameter separately.

About Getter and setter methods

The so-called Getter and Setter are to get and set. The same is true of their functions. The reason for these two methods is that if we want to set the properties of an object, the easiest way to think of is:

p1.age=20;

At this time, we find that we first call the attribute of the object, and then assign the value to the attribute, which is a little troublesome. We have a method to simplify the process. At this time, we use setter as:

p1.setAge(20);

At this point, the process is simplified, and the getter is:

System.out.println(p1.getAge());

More importantly, using methods to set and obtain attributes can better reflect the object-oriented process.
Of course, this getter and setter do not appear out of thin air. We must first set them in the class:

public String getName() {  
        return name;  
 }  
  
    public String getSex() {  
        return sex;  
 }  
  
    public int getAge() {  
        return age;  
 }  
  
    public void setName(String name) {  
        this.name = name;  
 }  
  
    public void setSex(String sex) {  
        this.sex = sex;  
 }  
  
    public void setAge(int age) {  
        this.age = age;  
 }  

About inheritance and replication

The so-called inheritance means that there is a parent-child relationship between two classes, which can also be understood as inheritance. The keyword used is extnds. For example:

class Person extends Animal{
    ...
}

This means that our Person class inherits an Animal class. At this time, our Person class has the gene of Animal, which means that it can directly use the properties and methods in the parent class. For example:

<!-- Parent class -->
package review;

public class Animal {
    String wildness;
    public Animal() {

    }

    void sleep(){
        System.out.println("Sleeping!");
    }
}

<!-- Subclass -->
package review;

public class Person extends Animal{
    String name;
    String sex;
    int age;
    public Person(String name, String sex, int age,String wildness) {
        this.wildness=wildness;
        this.name = name;
        this.sex = sex;
        this.age = age;
    }
    void output(){
      super.sleep();
      System.out.println(super.wildness);
    }
}
<!-- Test class -->
package review;

public class Review_1 {
    public static void main(String[] args) {
        Person p1=new Person("Rongxin Yang","Male",22,"lust");
        System.out.println(p1.wildness);
        p1.output();
    }
}

At this time, we found that the output results are:

lust
Sleeping!
lust

First of all, this illustrates a problem: after our subclass inherits the parent class, it can call the methods and properties of the parent class. In addition, we found that we can use the keyword super as a pointer to a parent class. In fact, in addition, our subclasses can not only inherit the methods of the parent class, but also override the methods of the parent class. In other words, when our subclass has a method with the same method name as the parent class, when our subclass calls this method again, it will execute the subclass's own method with the same name.

Upward and downward transformation

The so-called upward transformation means that our subclass instance is passed to the parent object, while the downward transformation is the opposite. There is no need to force type conversion for upward transformation, because the parent class must include subclasses, which means that subclass instances must belong to the objects of the parent class. To put it simply, dogs must belong to animals. On the contrary, we can also deduce that the downward transformation is not necessarily correct, so the downward transformation must be forced type conversion (forcibly referring to deer as horse), and there is another point that we must carry out the upward transformation in advance before the downward transformation, otherwise it will not be implemented normally. This is because if you directly new an object of a parent class, there is no final conclusion about what this object is. At this time, you can't directly judge it as an object belonging to a dog. Then it's completely wrong that a dog's reference is equal to a non dog object. To prove this statement, we can create a cat class and then create an upward transformation of the cat class. At this time, using this object to be equal to the reference of the dog is the same error. Here is our sample code:

<!-- Parent class -->
package review;

public class Animal {
    String wildness;
    String type;

    public Animal(String type) {
        this.type = type;
    }

    public Animal() {
    }
    void intro(){
        System.out.println("This is an animal!");
    }
}

<!-- Subclass -->

<!-- Dogs  -->
package review;

public class Animal {
    String wildness;
    String type;

    public Animal(String type) {
        this.type = type;
    }

    public Animal() {
    }
    void intro(){
        System.out.println("This is an animal!");
    }
}

<!-- Cats -->
package review;

public class Cat extends  Animal{
}

<!-- Implementation class -->
package review;

public class Review_1 {
    public static void main(String[] args) {
        Animal i=new Dog("Shiba Inu");
        Animal c=new Cat();
        Animal a=new Dog("Labrador retriever");
//        Dog d=(Dog) C;   prove
        Dog d=(Dog) i;
        a.intro();
        ((Dog) a).bark();
        d.bark();
        d.intro();
        System.out.println(((Dog) a).type);
        System.out.println(d.type);
    }
}

Keywords: Java

Added by daeken on Tue, 08 Mar 2022 07:24:56 +0200