Crazy Java Notes (v)-system interaction, systems, Runtime, date class

Source: Internet
Author: User

First, the program and user interaction (Java's Entry method-main method):

When you run a Java program, you must provide a Main method entry: publicstatic void Main (string[] args) {}

  Public : because the Main method may contain methods within the same package or other classes, in order to ensure the normal execution of the method, the method can only be used;

  Static: Call the Main method without first creating the object of the main class, but directly through the class to invoke the main method, so use static decoration;

  string[]: Who calls the method assigns a value to this parameter, the default length is 0 array

Runtime: Java Main class class name the first array element the second array element ...

 1  class       testargs{ 2  public  static  void   Main (string[] args) { 3   System.out.println (args.length);  4   ( String Arg:args { 5   System.out.prin TLN (ARG);  6   7
        8 } 

When the above program compiles, the direct run output is 0, which indicates that the default length of the args array is 0.

Instead, if you use the command line Java Testargs one AA Cc/java testargs "one AA" to run the Change program will output:

  

When you run a Java program, the class name immediately follows one or more strings, and multiple strings are separated by a space , which should be enclosed in double quotation marks if the parameter itself contains spaces.

Second, use scanner to get keyboard input:

System, System.in represents the keyboard-directly with the system.in is troublesome, usually will be packaged:

1, the traditional will be packaged into BufferedReader-this is more secure, and there is a perfect anomaly mechanism, BufferedReader is a character in the Java IO Stream, packaging flow it must resume on the basis of another character stream, the standard input system.in is a byte stream, The program needs to wrap it into a character stream using InputStreamReader:

BufferedReader br = new BufferedReader (new InputStreamReader (system.in));

2, JDK1.5 add a Scanner -More simple:

①, Hasnextxxx (): whether there is a next entry, where XXX can be an int, a long, and so on, representing the base data type of the string. If you need to decide whether to include the next string, you can omit xxx.

②, Nextxxx (): Gets the next entry. The meaning of XXX is the same as that of XXX in the previous method.

③, scanner provides two simple ways to read:boolean hasnextline (): Returns whether there is another line in the input source; String nextline (): Returns the string of the next line in the input source.  

1 ImportJava.util.*;2 3 classtestscanner{4      Public Static voidMain (string[] args) {5Scanner sc =NewScanner (system.in);6System.out.println ("Please Enter:");7         8         /*determine if there is a next string and output an entire line of content without a partition9 While (Sc.hasnext ()) {Ten System.out.println ("The Next line is:" + sc.nextline ()); One         } A          -  - determines if there is a next string and outputs the next value, with each value separated by a space the While (Sc.hasnext ()) { - System.out.println ("Next content is:" + sc.next ()); -         } -         */ +  -         //outputs the next integer, exits the current program when a non-integer is encountered, and a space partition when multiple characters are in a row +          while(Sc.hasnextint ()) { ASystem.out.println ("Next integer is:" +sc.nextint ()); at         } -     } -}

Third, the System class:

In layman's words: If you want to get data on the system where the operating platform is related to/JVM , use the systems class.

In-standard input, usually the standard input is the keyboard

Out-standard output is usually the screen

Common methods:

Exit (in status): Exits the virtual machine, regardless of whether you have any methods, threads will end

Getenv ()-Get all the environment variables

  getenv (String name)-Gets the value of the specified environment variable

Static Properties GetProperties ()-Get all system Properties

static string GetProperty (String key)-Gets the specified system property

1 ImportJava.util.*;2 3 classtestsystem{4      Public Static voidMain (string[] args) {5         //system.getenv (String name) Gets the value of the specified environment variable6System.out.println (system.getenv ("Java_home"));7         //system.getenv () Get all the environment variables of the system8         //System.out.println (System.getenv ());9 Ten         //GetProperty (String key) Gets the specified system property OneSystem.out.println (System.getproperty ("Os.name")); A  -         //get all System Properties -         //System.out.println (System.getproperties ()); the     } -}

Four, runtime class:

In layman's words: If you want to get JVM-related data , you need to use the runtime class

The runtime class is a typical "singleton class" whose constructor is hidden and therefore can only be obtained through the GetRuntime () method.

  Rt.maxmemory ()-

Rt.totalmemory ()-

rt.freemomory () -

You can also run programs that are already in the system:

  Rt.exec ("F:/application/sina_live.exe");

1  Public classruntimetest{2      Public Static voidMain (string[] args)throwsexception{3         //Singleton classes need to create an instance by means of a method4Runtime RT =runtime.getruntime ();5         //method Availableprocessors () Gets the number of processors and returns6System.out.println ("Number of processors:" +rt.availableprocessors ());7         8         /*memory Consumption9 int[] arr = new int[20000];Ten for (int i = 0;i < arr.length;i++) { One Arr[i] = i * 2; A         } -         */ -  the         //get the maximum memory for the JRE -System.out.println ("Max Memory:" +rt.maxmemory ());  -         //get JVM Total memory java-xms512m-xmx1024m -System.out.println ("Max Memory:" +rt.totalmemory ());  +  -         //commands to run the system +         //if no absolute path is used, it is searched in the system path specified by the PATH environment variable ARt.exec ("F:/application/sina_live.exe"); at     } -}

Five, date and calendar classes:

1, Date class: Represents a date, time;

There are no obsolete constructors:

New Date (long)-long indicates how many milliseconds have elapsed since the beginning of a certain time (gmt1970-1-1 00:00:00);

New Date ()

Date←→long (Gmt1970-1-1 00:00:00 to now can be converted to each other)

2. Calendar Class-Represents a date, time (instead of date)

The getinstance () method is called to get the instance, or it can be converted to date, but date cannot be converted to calendar;

Set (year, month, day, time, minute, second), such as: Set (calendar.month,9) directly sets the value of a field

Add (int field,int amount) to a field with roll (int field,int amount)

   The add method has the following rule: When the Modified field exceeds its allowable range , it will take place"carry";

1 ImportJava.util.*;2 3  Public classdatetest{4      Public Static voidMain (string[] args) {5         //creates a date of the current time6Date Date =NewDate ();7 System.out.println (date);8         //outdated commands, compiled with hints, but can be run. In Java, the sun and Moon are starting from 0.9         //System.out.println (Date.getday ());TenSystem.out.println ("How many milliseconds to get from 1970-1-1 to now" +System.currenttimemillis ()); One  A         //turn the number of milliseconds into a date -System.out.println (NewDate (System.currenttimemillis ()-3 * 20 * 60 * 60 * 1000)); -  the         //If you want to calculate the number of days (hours) between two date -Date D1 =NewDate (); -Date D2 =NewDate (114,1,19,22,41,30); -  +System.out.println ("The difference between two date hours:" + (D1.gettime ()-d2.gettime ())/3600/1000); -     } +}
1 ImportJava.util.*;2 3  Public classcalendartest{4      Public Static voidMain (string[] args) {5Calendar cal =calendar.getinstance ();6         //set to 2015-4-12 22:45:307Cal.set (2015,4,12,22,45,30);8 System.out.println (CAL);9 System.out.println (Cal.gettime ());Ten          One         //set to October ACal.set (calendar.month,9); - System.out.println (Cal.gettime ()); -  the         //the difference between add and roll -Cal.add (calendar.date,20); - System.out.println (Cal.gettime ()); -  +Cal.roll (calendar.date,30); - System.out.println (Cal.gettime ()); +     } A}

Crazy Java Notes (v)-system interaction, systems, Runtime, date class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.