"Crazy Java Lectures" 8

table of Contents

 

Interact with users

1. Parameters for running Java programs

2. Use Scanner to get keyboard input

System related

1.System class

2.RunTime class

BigDecimal class


Interact with users

1. Parameters for running Java programs

 The entrance to the Java program-the method signature of the main() method: 

public static void main(String[] args)    { }

The following explains in detail why this method signature is used:

  • Public modifier: Because the Java class is called by the JVM (java virtual machine), in order to allow the JVM to freely call the main() method, it is decorated with public.

  • Static modifier: When the JVM calls this method, it will not first create the object of the main class, and then call the main method through the object. The JVM directly calls the main method by calling this class (As mentioned before, the static modified method is the class Method, which can be called by class or object).

  • void: The JVM calls the main method, and the return value of the main method must be returned to the JVM, but this has no meaning, so just don't return the value.

  • String[] args: character array parameter. According to the method call principle, whoever calls the method provides the value of the formal parameter, that is, the JVM calls the main method, so it assigns the value of the formal parameter.

    But how does the JVM know how to assign a value to this array?

    Look at the following code:

     

    image

    The results are as follows:

     

    image

    This is the expected result, because no value is assigned to the array, of course the length is 0;

    However, if you run a Java program (console operation) followed by one or more strings (separated by spaces between multiple characters) immediately after the class name, the JVM will assign these strings to the args array elements in turn.

    If a parameter itself contains spaces, you should enclose the parameter in double quotation marks (""), otherwise Java will single-seat the space as a parameter separator instead of the parameter itself.

     Isn’t it amazing? I didn’t expect that something that I thought was a routine has such a profound meaning.

2. Use Scanner to get keyboard input

(1) Scanner mainly provides two methods to scan input:

    1) hasNextXXX(): Whether there is another input item, where XXX can be a string representing basic data types such as int, long, etc.; if you just judge whether it contains the next string, use hasNext() directly.

    2) nextXXX(): Get the next input item.

 

code show as below:

image

The results are as follows:

image

    Here you need to understand that Scanner uses blanks (spaces, tab blanks, carriage returns) as separators by default. If you do not want to use blanks as separators, for example, if you want to enter a row of data, whether there is a space or not, it is regarded as an input item. If this is the requirement, you can set the separator of the Scanner to enter only.  Use the useDelimiter (String pattern) method to set the separator for the Scanner . The parameter of this method should be a regular expression. (Introduced later)

Take a look at chestnuts:

image

The result is:

image

    You can see that this time you enter three data in one line, and the blank has no delimiter.

    In fact, Scanner provides two simple methods to read line by line:

1) boolean hasNextLine(): returns whether there is a next line in the input source;

2) String nextLine(): Returns the string of the next line in the input source.

The following demonstrates using Scanner to obtain integer input items in the input source:

image

The result is:

image

    It can be seen that it turns numbers (integer or floating-point numbers into floating-point numbers for output), but the second input data, because characters are input, not numbers, so there is no output result. At this time, Scanner reads The fetch operation is blocked. This is where it is not as good as the previous program, and it is not very adaptable.

    Scanner can not only read the user's keyboard input, but also read the file.

Give a chestnut:

 

image

The writing method is similar to the above. Two points should be paid attention to. One is the writing method of file loading, and the other is the above exception handling (this program will automatically prompt for generation).

 

System related

    When a Java program runs on different operating systems, it may be necessary to obtain platform-related attributes or call platform commands to complete specific functions. Java provides the System class and Runtime class to interact with the running platform of the program.

1.System class

    When the System class is used as the current running platform, the program cannot create objects of the System class. The System class provides some class variables and class methods, allowing these class variables and class methods to be called directly through the System class.

    The System class provides class variables representing standard input, standard output and error output, and provides some static methods for accessing environment variables and system properties. It also provides methods for loading files and dynamic link libraries (I don’t know this , Just check it out). The System class also has two methods to obtain the current system time, currentTimeMillis() and nanoTime(), both of which return a long integer. It must be pointed out here that the time granularity returned by these two methods depends on the underlying operating system. The operating system does not support milliseconds and nanoseconds as timing units at all.

2.RunTime class

    When the Runtime class represents the runtime environment of a Java program, each Java program has a corresponding Runtime instance, and the application program is connected to its runtime environment through this object. The application cannot create its own Runtime instance, but it can be obtained through the getRuntime() method

When the associated Runtime object Runtime is used as the running environment, you can access the relevant information of the JVM,

Such as the number of processors, memory information, etc.

Give a chestnut:

 

image

The results are as follows:

image

    In addition, the Runtime class has a function: directly start a separate process to run the operating system commands:

image


    It feels so amazing!

BigDecimal class

1.  In order to accurately represent and calculate floating-point numbers, Java provides the BigDecimal class, which provides a large number of constructors for creating BigDecimal objects, including converting all basic numeric variables into a BigDecimal object, as well as using numeric strings , Numeric character array to create BigDecimal object.

2.  In the BigDecimal class constructor, the BigDecimal (double val) constructor has certain unpredictability . When the program uses new BigDecimal (0.1) to create a BigDecimal object, its value is not 0.1, it is actually equal to A number close to 0.1, because 0.1 cannot be accurately represented as a double floating point number. But the result of using the BigDecimal (String val) constructor is predictable, so String-based constructors are usually preferred. If you must use a double floating point number as the parameter of the BigDecimal constructor, use the BigDecimal.valueOf(double value) static method to create a BigDecimal object.

END

image

Guess you like

Origin blog.csdn.net/allein_STR/article/details/113986272