Learning Diary (iv) basic composition of Java programs

Source: Internet
Author: User

A basic Java program typically consists of several parts, namely the package name of the program, the path to the other packages used in the program, the class of the program, the methods in the class, the variables, and the literal.

Package demo;

Import Java.util.Date;

public class Demo {

public static void Main (string[] args) {

int num;

Num=1;

String Str=new string ();

Date Date=new date ();

System.out.println (str);

SYSTEM.OUT.PRINTLN (num);

SYSTEM.OUT.PRINTLN (date);

}

}

The above is a simple program that outputs an integer and the current system date. Where the demo is the package name, Java.util.Date is the package of the imported date class, the demo is the class name, main is the method name in the class, NUM is the variable, and 1 is the literal.

Package is the key word for defining packages,

Import is the keyword of the guide package,

Public is one of the rights modifiers, which is publicly owned, public, and three others are private, protected protect, and default.

Class is the keyword that defines the classes,

Static is also a modifier that stands for static. Methods with static, variables, and so on belong to the class all do not belong to the object. A class is a type, a somewhat abstract thing, an instantiation of a class is an object, such as a human being, a person, a class, not a specific one, and to any person, such as a person, is an object.

The package name, class name, method name, and variable name are usually taken by themselves, but it is best to see the name, use English and follow the hump naming method. The so-called hump nomenclature is when several words are grouped together as a single word, the first letter is lowercase, the other initials are capitalized, and all the letters except the first letter are lowercase. Beginners easy to make a mistake is, like to use pseudo-English, in fact, is the Chinese pinyin, such as the date, not date but written Riqi, at first glance seems to be English, actually not so.

In the above example, the method name is special, the main method in Java is the entry of the program, that is, the general program will go from here to start running, and this method is a fixed line of writing, any word and symbols including parentheses inside the parameters can not be changed, once changed, then he is not the main function, Can not play the role of the entrance to the program.

Naming there is also a need to note that you can not use the Java keyword, nor the use of reserved words in Java, so-called reserved words, in fact, the Java definition may become a keyword, but is not a keyword word.

In the above example, there are three variables, num, str, and date, the three are different, int and STR are not guided, and the date is a guide package. In Java, the eight basic data types are not guided, so int is not, and the type of STR is string, he does not belong to the eight basic types, but because it is too often used, so it has the privilege of the same as the eight basic types do not have to guide the package; As for date, although he is often used, But it's not as common as a string, so he wants to be honest with the package, of course, except for all reference types except string.

Variables are actually equivalent to a box of things, that is, something that can change the content, and what we see in it is the so-called literal. For example, int num=1,num is a variable box, and 1 is the literal, that is, the thing in the box.

In the end, the program output the contents of these three variables, in the console printout of the common statement is System.out.println (), the parentheses inside is to output the content, here is used in the above variable. When you use variables, you need to be aware that they must be declared and initialized before they are used.

The simple point of the declaration is the type plus variable name, and the simple point of initialization is to assign a value to the box, add something to it, such as 1 for the nun, and a date loaded with the new date ().

The Declaration and initialization of a variable is usually in two forms, with the first declaration and initialization of the Declaration. In the example above, Num declares the reinitialization first, while the other two are initialized at the same time as the declaration.

When using variables, it is also important to note that it has scope, that is, even if the declaration and initialization are not necessarily available, the scope of the variable is generally from the beginning of the closest curly brace that contains him to the end of the range, and the out-of-scope will be invalidated.


Learning Diary (iv) basic composition of Java programs

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.