Our first Java program

Source: Internet
Author: User
Tags command line documentation memory usage numeric value

Finally, let's make a formal program (annotation ⑤). It can print out information about the currently running system and take advantage of a variety of methods from the systems object from the Java standard library. Note that an additional annotation style is introduced here: "//". It indicates that all content before the end of the line is commented:

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 ());
  }
}


⑤: In some programming environments, the program will be on the screen all the time, and even have no chance to see the results. You can put the following code at the end of Main () to suspend the output:
try {
Thread.CurrentThread (). Sleep (5 * 1000);
catch (Interruptedexception e) {}
}
Its function is to suspend the output for 5 seconds. Some of the concepts involved in this code will not be mentioned until later in this book. So there's no need to delve into it right now, just knowing that it's a trick to suspend a program.


At the beginning of each program file, you must place an import statement and import all the extra classes that you want to use in the code of that file. Note that we say they are "extra" because a special class library will automatically import each Java file: Java.lang. Start your Web browser and 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 document). 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. Because Java.lang enters each Java code file by default, these classes can be used directly at any time. In this list, we can find system and runtime, which we have used in Property.java. The date class is not listed in Java.lang, so you must import another class library to use it. If you are not sure which class library a particular class is in, or want to view all classes, select "Class Hierarchy" (class hierarchy) in the Java user documentation. In a Web browser, although it takes a while to build this structure, you can clearly find each of the classes provided with the Java Companion. You can then search for the keyword "Date" by using the Find feature in the browser. After such treatment, we can find that our search target is listed in java.util.Date form. We finally know that it is located in the Util library, so you must import java.util.*, otherwise you cannot use date.
To see the beginning of the packages.html document (which I have set to its own default start page), select Java.lang and choose System. You can see that the System class has several fields. If you choose out, you know that it is a static PrintStream object. Because it's "static", we don't 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 is handy for listing in descriptive text as a hyperlink. So if you click that link, you can see all the methods that can be invoked for PrintStream. There are a number of methods, which are described in detail later in this book. For now, we are interested in only println (). It means "to print the things I give you to the console and end with a new line." So in any Java program, once you want to print something to the console, you can have a knee-jerk System.out.println ("content").
The class name is the same as the file. If you create a separate program like this, a class in the file must have the same name as the file (if not, the compiler will respond in a timely manner). Class must contain a method named Main (), in the form as follows:
public static void Main (string[] args) {
Where the keyword "public" means that the method can be invoked by the outside world (the 5th chapter will explain in detail). The argument for main () is an array that contains a string object. Args are not used in this program, but need to be listed here because they hold the arguments that are invoked at the command line.
The first line of the program is very interesting:
System.out.println (New Date ());
See its arguments: The only purpose of creating a Date object is to send its value to println (). Once this statement has been executed, date is no longer needed. The "garbage collector" that comes with it will discover this and recycle it whenever possible. In fact, we do not have much need to care about the details of "clearance".
The second line calls System.getproperties (). If you use a Web browser to view the 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 can be used at any time, regardless of whether or not an object exists for the class. When GetProperties () is invoked, it generates the system properties as an object of the properties class (note that properties is the meaning of "property"). The subsequent handle is stored in a properties handle named P. In the third line, 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.
Lines fourth and sixth of Main () are typical print statements. Note To print multiple string values, separate them with a plus sign (+). However, here are some strange things to watch out for. When used in a string object, the plus sign does not represent the true "add". When dealing with strings, we usually do not have to consider any special meaning of "+". However, the Java string class is constrained by a mechanism called "operator overload." That is, the plus sign produces a different representation from anywhere else, only when used with a string object. For a string, it means "connect these two strings".
But this is not the end of the story. Please observe the following statement:
System.out.println ("Total Memory ="
+ rt.totalmemory ()
+ "Free Memory ="
+ rt.freememory ());
where TotalMemory () and Freememory () return a numeric value, not a string object. What happens if a number is "added" to a string? Like us, the compiler is aware of this problem and magically invokes a method that converts that value (int,float, and so on) to a string. After such treatment, they can of course use the plus sign "plus" together. This "automatic type conversion" is also included in the category of "operator overload" processing.
Many Java writings are hotly debating whether "operator overload" (an attribute of C + +) is useful. Now is a good example of opposing it! However, this can only count as a compiler (program) problem, and only for a string object. It is not possible to "overload" an operator for any source code you write.
By calling the GetRuntime () method for the runtime class, the fifth row of main () creates a runtime object. Returns a handle that points to a runtime object. And, we don't have to care whether it's a static object or an object created by the new command. This is because we do not have to be responsible for cleanup work, we can use objects swept. As shown, runtime can tell us the information about memory usage.

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.