[java] Basic knowledge (1)

JDK, JRE, and JVM

JVM  : The English name (Java Virtual Machine), is the Java virtual machine we are familiar with. It only recognizes files of the type xxx.class, and it can recognize the bytecode instructions in the class file and call the upward API of the operating system to complete the action. Therefore, jvm is the core of Java's ability to cross platforms, which will be explained in detail below.

JRE  : English name (Java Runtime Environment), we call it: Java Runtime Environment. It mainly includes two parts, the standard implementation of jvm and some basic class libraries of Java. Compared with the jvm, it has a part of the Java class library.

JDK  : English name (Java Development Kit), Java Development Kit. jdk is the core of the entire Java development, it integrates jre and some useful gadgets. For example: javac.exe, java.exe, jar.exe, etc.

How does banking represent money (java) 

First of all, floating point numbers cannot be used for representation;

Reason: Floating-point numbers are finite, discrete, round-off errors, mean approximately but not equal.

BigDecimal can represent decimals of arbitrary precision, no matter how large its range is. BigDecimal uses array to store numbers, and each input represents a number. Therefore, BigDecimal can represent numbers of any size.

In addition, BigDecimal is a class, not a basic type, and methods need to be called to implement number operations. It has four methods: add, subtract, multiply, and divide. Here m1 and m2 are multiplied using multiply.

bit operation 

<< is equivalent to *2

>> Equivalent to /2

string concatenation 

//字符串连接符
        System.out.println(""+a+b);//1020 
        System.out.println(a+b+"");//30

The difference between == and equals 

== compares the (heap) memory address of the object stored in the variable (stack) memory, and is used to judge whether the addresses of the two objects are the same, that is, whether they refer to the same object. The comparison is a pointer operation in the true sense.

equals is used to compare whether the contents of two objects are equal. Since all classes are inherited from the java.lang.Object class, it is applicable to all objects. If the method is not overridden, the call is still Object The method in the class, and the equals method in Object returns the judgment of ==.

Computes odd and even sums between 0 and 100

public class ForDemo01 {
    public static void main(String[] args) {
        //计算0到100之间的奇数和与偶数和
        int oddSum=0; //奇数
        int evenSum=0; //偶数
        for (int i = 0; i < 100; i++) {
            if(i%2!=0){
                oddSum+=i;
            }
            else {
                evenSum+=i;
            }
        }
        System.out.println("奇数和:"+oddSum);
        System.out.println("偶数和:"+evenSum);
    }
}

//Use while or for loop to output numbers divisible by 5 between 1 and 1000, and output 3 numbers per line 

public class ForDemo02 {
    //用while或for循环输出1~1000之间能被5整除的数,并且每行输出3个
    public static void main(String[] args) {
        for (int i = 0; i <= 1000; i++) {
            if(i%5==0){
                System.out.print(i+"\t");
            }
            if(i%(5*3)==0){
                System.out.println();
            }
        }
    }
}

Print 99 multiplication table

public class ForDemo03 {
    public static void main(String[] args) {
        //打印99乘法表
        for (int i = 1; i <= 9; i++) {
            for(int j = 1; j <= i; j++){
                System.out.print(i+"*"+j+"="+(i*j)+"\t");
            }
            System.out.println();
        }
    }
}

Print triangles (5 lines)

public class TestDemo01 {
    //打印5行三角形
    public static void main(String[] args) {
        for (int i = 0; i <= 5; i++) {
            for(int j = 5;j >= i;j--){
                System.out.print(" ");
            }
            for(int j = 1;j <= i;j++){
                System.out.print("*");
            }
            for(int j = 1; j< i;j++){
                System.out.print("*");
            }
            System.out.println();
        }
    }
}

encapsulation, inheritance, polymorphism

encapsulation

  • That is, the properties and methods of the object are wrapped, and can only be accessed through the agreed interface
  • Encapsulation is an information hiding technology, and encapsulation is realized through the keyword private in java. ,

inherit

Inheritance is one of the most notable features of object-oriented. Inheritance is to derive a new class from an existing class. The new class can absorb the data attributes and behaviors of the existing class, and can expand new capabilities.
Why inherit it:

1. Real relationships that reflect reality

2. Reduce code redundancy

3. Extend and rewrite the properties and methods of the parent class

polymorphism

Polymorphism is mainly based on inheritance and rewriting. In the end, the same type can be called the same method, but the results are different.

Guess you like

Origin blog.csdn.net/weixin_46601559/article/details/126394773