Java programming thought note--The first Java program

Source: Internet
Author: User

Property.java
Import java.util.*;
Public Class Property {
public static void Main (string[] args) {
System.out.println (New Date ());
Properties p = system.getproperties ();
P.list (System.out);
SYSTEM.OUT.PRINTLN ("---Memory Usage:");
Runtime RT = Runtime.getruntime ();
System.out.println ("Total Memory ="
+ rt.totalmemory ()
+ "Free Memory ="
+ rt.freememory ());
}
}

1: in some programming environment, the program will be on the screen all over, and even no chance to see the results. You can put the following code at the end of Main () and use it to pause the output:

try {thread.currentthread (). Sleep (5 * 1000);

}catch (Interruptedexception e) {}

}

Its function is to pause the output for 5 seconds. Some of the concepts involved in this code will be discussed later in this book. So there is no need to delve into it, just know that it is a trick to get the program to pause.

2: A special class library is automatically imported into each Java file: Java.lang. Launch your Web browser to view the user documentation provided by Sun (download now if you have not already downloaded from http://www.java.sun.com or otherwise installed the Java documentation). In the packages.html file, you can find all the class library names provided by the Java companion. Please select the Java.lang. Under "Class Index", you can find a list of all the classes that belong to that library. Since Java.lang enters each Java code file by default, these classes can be used directly at any time.

3:. If you do not know which class library a particular class is in, or if you want to view all the classes, you can select "Class Hierarchy" (class hierarchy) in the Java user documentation. In a Web browser, although it takes a short time to build this structure, it is clear that each class provided with the Java companion can be found. You can then search for the keyword "Date" using your browser's Find feature. Once this has been done, we can find that our search targets are listed in the form of java.util.Date. We finally know that it is located in the Util library, so you must import java.util.*, otherwise you will not be able to use Date.

4: Observe the beginning of the packages.html document (I have set it as my default start page), select Java.lang, and then click System. You can see that the System class has several fields. If you choose out, you know that it is a static PrintStream object. Since it is "static", we do not need to create anything. The Out object must be 3, so just use it directly. What we can do with this out object is determined by its type: PrintStream. PrintStream are listed as a hyperlink in the description text, which is very handy. So if you click that link, you'll see all the methods that can be called for PrintStream. There are a number of methods, which are described in detail later in this book. For the moment, we are only interested in println (). It means "Print the things I gave you to the console and end with a new line". So in any Java program, once you print some content to the console, you can write System.out.println ("content") in a conditioned manner.

5:

The class name is the same as the file. If you create a standalone program like this, a class in the file must have the same name as the file (the compiler responds in a timely manner if it does not). The class must contain a method called Main (), in the form of the following: public static void Main (string[] args) {
13
Where the keyword "public" means that the method can be called by the outside world (chapter 5th explains it in detail). The argument to main () is an array containing the String object. Args are not used in this program, but need to be listed in this place because they hold arguments that are called at the command line.

6: The first line of the program is very interesting: System.out.println (new Date ()); Observe its arguments: the only purpose of creating a Date object is to send its value to println (). Once this statement is executed, Date is no longer needed. The resulting "garbage collector" will find this out and recycle it whenever possible. In fact, we don't have much need to care about the details of "cleanup".

7: The second line calls System.getproperties (). If you use a Web browser to view online user documentation, you know that getProperties () is a static method of the System class. Because it is "static", you do not have to create any objects to invoke the method. The static method is ready to use regardless of whether or not an object of the class exists. When GetProperties () is called, it generates system properties as an object of the properties class (note that the property is meant for "attributes"). The subsequent handle is stored in a Properties handle called P . In the third row, you can see that the Properties object has a method named list () that sends its entire contents to a PrintStream object that we pass as an argument.

Java programming thought note--The first Java program

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.