Everything is an object note-the third day, the first Java program

Source: Internet
Author: User

2.7 your first Java program

As described above, let's write a complete program.

Java code
//: HelloDate. java

Import java. util. Date;

Public class HelloDate {
Public static void main (String [] args ){
System. out. println ("Hello! It's :");
System. out. println (new Date ());
}
}///:~
 
Import is used to introduce additional classes required in file code.
Java. lang this class library package each Java program will be automatically introduced
The class name must be the same as the file name
When the Date () class is introduced through import, a new Date () object is created and its value is automatically converted to String and sent to println (). After the execution is complete, the Date object will no longer be used, and the spam will find that the object does not have any reference associated with it and will be automatically recycled.
Java. lang. System has many interesting usage:

 

Java code
//: ShowProperties. java
Public class ShowProperties {
Public static void main (String [] args ){
System. getProperties (). list (System. out );
System. out. println (System. getProperty ("user. name "));
System. out. println (System. getProterty ("java. library. path ));
}
}///:~
 
 
2.7.1 compile and run
Bash code
Royakon @ royakon-track2web :~ /Temp $ vim HelloDate. java
Royakon @ royakon-track2web :~ /Temp $ ls
HelloDate. java
Royakon @ royakon-track2web :~ /Temp $ javac HelloDate. java
Royakon @ royakon-track2web :~ /Temp $ ls
HelloDate. class HelloDate. java
Royakon @ royakon-track2web :~ /Temp $ java HelloDate
Hello! It's:
Wed Aug 24 22:20:57 CST 2011
 
2.8 comments and documents

 

Java code
/* Java has two annotation styles */
// There are two annotation styles in Java
 
The biggest problem in writing code documents is probably the maintenance of the documents. The solution is also simple: link the Code with the document. To achieve this, you must also use special annotation syntax to mark documents. In addition, you need a tool to extract those annotations and convert them into useful forms. This is exactly what Java has done. Java provides javadoc to complete this job.

All javadoc commands can only appear in the "/**" comment. Normally, the Comment ends with "*/". There are three types of annotation documents, which correspond to the three elements after the annotation position: Class, field, and method.

Java code
//: Documentation1.java
/** A class comment */
Public class Documentation1 {
/** A field comment */
Public int I;
/** A method comment */
Public void f (){}
}///:~
 
 

Javadoc sends Html commands through generated html documents, which allows you to make full use of Html. Format the code.

Java code
//: Documentation2.java
...
/**
* You can <em> even </em> insert a list:
*
<Ol>
*
<Li> Item one </li>
 
*
<Li> Item two </li>
 
* </Ol>
 
*/
...
///:~
 
 

Examples of tag usage:

Java code
//: HelloDate. java

Import java. util. Date;

/**
* The frist Thinking in Java example program.
* Displays a string and today's date.
* @ Author royakon
* @ Author www.track2web.com
* @ Version 1.0
*/
Public class HelloDate {
/**
* Entry point to class & application.
* @ Param args array of string arguments
* @ Throws exceptions No excetptions thrown
*/
Public static void main (String [] args ){
System. out. println ("Hello! It's :");
System. out. println (new Date ());
}
}
/* Output: (55% match)
* Hello! It's:
* Wed Aug 24 22:20:57 CST 2011
*///:~
 
2.9 encoding Style

In the "Java programming language coding Convention", the Code style stipulates that the first letter of the class name should be capitalized. If the class name consists of several words, put them together, the first letter of each internal word is in uppercase.

This style is called "camper style ". Almost all other content: Methods, fields, Object Reference names, etc. The accepted style is the same as the class style, but the first letter of the identifier is in lower case.

 

Author: "RoyAkon's FreeStyle"
 

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.