Java control flow large value array

  • 2021-11-24 01:33:19
  • OfStack

Directory 1. Control Flow 1.1 Block Scope 1.2 Condition Statement 2. Large Numeric Value 3. Array 3.1 Command Line Parameters

1. Control flow

1.1 Block scope

"Error-prone point": You cannot declare a variable with the same name in two nested blocks, such as


public static void main(String[] args){
    int n;
    ...
    {
        int n; // Error--can't redefine n in inner block
    }
}

This code cannot be compiled because variables with the same name appear in two nested blocks

Note:

In C + +, you can redefine 1 variable in a nested block. Variables defined in the inner layer override variables defined in the outer layer.

1.2 Conditional Statements

"Error-prone point": In a loop, checking whether two floating-point numbers are equal requires special attention, such as


for(double x=0; x!=10; x+=0.1);

The above loop will not end, because 0.1 cannot be accurately expressed in binary, so x will skip from 9.9999999999998 to 10.099999999999998.

"Additional points": switch Statement in the case If break is not added at the end of a branch statement, it is actually a more dangerous situation. So use switch Statement can be written as follows when compiling code javac -Xlint:fallthrough File name. java. If break is missing in a certain bit of a branch, the compiler will report a warning. If you just need some branches and don't add them behind them break You can add 1 annotation to its peripheral method @SuppressWarnings ("fallthrough") so that no warning is generated for the method.

"Additional points": Java No goto Statement, but break It can be labeled, which can be used to realize the purpose of jumping out of the inner loop. Such as


int n;
read_data:
while(){
    for(){
        ...
        break read_data;
        ...
    }
}

Note:

This mode can only jump out of the statement block, but not into the statement block

2. Large values

"Supplementary points": If the basic integers and floating-point numbers do not meet the requirements, you can use the java.math Two classes in the package: case 0 And BigDecinmal . These two classes can handle numeric values containing sequences of numbers of arbitrary length. case 0 Class implements integer operations with arbitrary precision, BigDecinmal The floating-point number operation with arbitrary precision is realized

3. Arrays

"Error-prone point": The array length in Java is not required to be a constant, such as new int[n] An array of length n is created. However, after determining the value of n, it cannot be changed.

3.1 Command Line Parameters

"Supplementary point": Every Java application has a String[] args The main method of the parameter. This parameter indicates that main Method will accept an array of 1 strings, that is, command line arguments. Such as


public class Message{
    public static void main(String[] args){
        if(args.length==0||args[0].equals("-h")){
            System.out.print("Hello,");
        }else if(args[0].equlas("-g")){
            System.out.print("Goodbye,");
        }
        for(int i=1;i<args.length;i++){
            System.out.print(" "+args[i]);
        }
    }
}

If we run this program on the command line using the following form: java Message -g cruel world The following information is displayed Goodbye , cruel world !

This shows that switch 0 The contents of the array memory are as follows: switch 1

Note:

In the main method of the Java application, the program name is not stored in the args array


Related articles: