Java basics-common classes!

Source: Internet
Author: User

1. system class:

All static methods are used in the system class. If no constructor exists, system cannot be used to instantiate objects.

1) objects in system:

Out: standard output: Default console.

In: standard input. default keyboard.

2) properties:

System. getproperties.

Of course, the preperties here is a subclass of hashtable. That is, map set and subclass. In addition, all input from this collection village are strings without generic definitions.

You can also use setproperty () to customize system information.

When the JVM is started, it checks the system information and puts it in system. properties. In this way, it is cross-platform.

For example:

String osName = System.getProperty("os.name");System.out.println("osName = " + osName);

The results on different computers will be different. Windows XP system will be the result of winowxp, while Windows 7 will be the result of win7.

This can be judged at the early stage of operation. Such as operating system judgment.

Some attributes can be dynamically loaded at JVM startup.

Of course, you need to add some parameters at runtime: Java-dkey = value Class Name

In this way, the JVM dynamically adds an attribute with the key value as key and the value as value.

2. runtime class:

1) The runtime class is the same as the system class, and there is no constructor. Objects cannot be directly created.

2) if there are non-static methods in this class, it indicates that this class certainly provides methods to obtain this class, and this method is not static. And the return value type is the type of this class.

3) This approach is also a singleton mode.

Runtime runtime = Runtime.getRuntime();try {runtime.exec("notepad.exe C:\\1.txt");} catch (Exception e) {e.printStackTrace();}

3. Date class:

Simpledateformat df = new simpledateformat ("mm minute SS seconds at HH on mm dd, YYYY"); Date = new date (); system. out. println (DF. format (date ));

4. calendar:

Abstract calendar class. It cannot be instantiated directly. Getinstance obtains the object through the static method. The obtained object is actually its subclass.

Calendar c = Calendar.getInstance();System.out.println(c.get(Calendar.YEAR));System.out.println(c.get(Calendar.DAY_OF_YEAR));System.out.println(c.get(Calendar.WEEK_OF_YEAR));

For the Set Method in the calendar, you can set the calendar field to a given value.

The add method can be modified on the given value.

/*** Obtain the number of days in the month of any year. * **/Integer year = 2012; C. Set (year,); // The set method can set the calendar field to a given value. C. add (calendar. day_of_month,-1); // get the given value, and then add a-1; system. out. println (C. get (calendar. day_of_month); // output/*** get the current time of yesterday ***/system. out. println ("--------------------------------"); Calendar C1 = calendar. getinstance (); c1.add (calendar. day_of_month,-1); system. out. println (c1.get (calendar. day_of_month) + "" + c1.get (calendar. hour_of_day ));

5. Math class:

MathClass contains methods used to perform basic mathematical operations, such as Elementary Indexes, logarithm, square root, and trigonometric functions.
It is also a class without constructor. The methods are static and called directly.

The random () function generates a random number greater than or equal to 0.0 and less than 1.0.

for(int i = 0 ; i < 10 ; i ++){System.out.println(Math.random());}

This math class is a function and also an independent class. Random can also be used to generate random numbers.

Random random = new Random();System.out.println(random.nextInt(10));

The nextint in the random class is used to generate a random number greater than or equal to 0 and less than 10.

----------------------
Android training and Java training. We look forward to communicating with you! ----------------------

Related Article

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.