Interacting with users
Parameters for running Java programs
// Java Program entry: Main () method Public Static void Main (string[] args)
Public modifier: Java class has a JVM call, in order for the JVM to be free to invoke the main () method, all using the public modifier exposes this method.
Static modifier: When the JVM calls this main method, it does not invoke this method by creating an instance of the class, but instead calls the method directly through the class, so it is decorated with static.
void return Value: The Main method is called by the JVM, and the return value of the method is returned to the JVM, which makes no sense, so the main () method has no meaning.
The principle of method invocation: Who invokes the method, who is responsible for assigning the parameter. So. The main () method parameter args should be assigned by the JVM.
Public class argstest{ publicstaticvoid main (string[] args) { // returns 0 System.out.println (args.length); for (String Arg:args) { System.out.println (ARG); }}}
The above program runs the command
Java argstest Java Spring
When you run a Java program with one or more characters following the class name (multiple characters separated by a space), the JVM assigns the characters to the args array at once. If a parameter itself contains a space, the argument is expanded with double quotation marks ("").
Java argstest "Java Spring"
Get keyboard input using scanner
Scanner class under the Java.util package
Scanner is a regular expression-based text scanner that resolves basic type values and string values from files, input streams, and strings.
Scanner provides multiple constructors, which can receive files, input streams, and strings as data sources for parsing data from files, input streams, and strings.
The scanner mainly provides two methods for scanning input:
Hasnextxxx (): whether there is a next entry, where xxx can make int, long, and so on represent a string of the base data type. If you just decide whether to include the next string, use Hasnext () directly
Nextxxx (): Gets the next entry, XXX is the same as the previous one
By default, scanner uses whitespace (including spaces, Tab, and carriage returns) as a direct delimiter for multiple entries
import java.util.*; Public class scannertest{ publicstaticvoid main (string[] args) { New Scanner (system.in); while (S.hasnext ()) { System.out.println (S.next ()); }}}
import java.util.*; Public class scannertest{ publicstaticvoid main (string[] args) { New Scanner (system.in); // only the data of the int data type will be scanned, and the non-conforming condition will jump out of operation while (S.hasnextint ()) { System.out.println (S.nextint ()); }}}
You can set the scanner of the payment to other
Scanner read operations may be blocked (the current execution sequence is paused) to wait for information input. If the input stream does not end, scanner cannot read more entries (especially if keyboard input is common), scanner's Hasnext () and Next () methods are likely to block, and the Hasnext () method blocks regardless of whether the next () method associated with it is blocked.
To set the delimiter for scanner using the Usedelimiter (String Pattren) method, the parameter of the method should be a regular expression
import java.util.* public class scannertest{ public static void main (string[] args) {Scanne R S = new Scanner (system.in); // set delimiter // s.usedelimiter ("\ n" while (S.hasnextint ()) {System.out.pri Ntln (S.nextint ()); } } }
Scanner provides two simple ways to read line by row
Boolean hasnextlint (): Returns whether the input source has the next line
String nextline (): Returns the next line of string from the input source
ImportJava.util.*;ImportJava.io.*; Public classscannertest2{ Public Static voidMain (string[] args)throwsexception{//Read File//The file object is used as the scanner constructor parameter, scanner reads the contentsScanner s =NewScanner (NewFile ("Scannertest.java")); while(S.hasnextline ()) {System.out.println (S.nextline ()); } } }
System-related
System class
The System class represents the running platform of the current Java program, and the program cannot create objects of the system class, and the system class provides class variables and class methods that allow these class variables and methods to be called through the system class.
The system class provides class variables that represent standard input, standard output, and error output, and provides methods for accessing environment variables, System Properties, and methods for loading files and dynamic-link libraries.
The System class also provides a Identityhashcode (Object x) method. The method returns the exact hashcode value of the specified object, that is, the hashcode value is computed based on the address of the object. When a class Hashcode () method overrides, the Hashcode () method of the instance cannot uniquely identify the object, but the Identityhashcode () method can return the Hashcode value.
Runtime class
The runtime class represents the runtime environment of a Java program, and each Java program has a runtime instance that is connected to its runtime environment by the application. The application cannot create the runtime instance itself, but the Rumtime object associated with it can be obtained through the Gerruntime () method. The runtime also provides the GC () and Runfinalizatiom () methods to notify the system of garbage collection, clean up system resources, The load (String filename) and LoadLibrary (Stringlibname) methods are provided to load the file and the dynamic-link library.
Common classes
Java Base Class Library