The basic language for getting started with Java

Source: Internet
Author: User

In the previous chapter, the reader has learned how to build the Java development environment and the Java program development process. Start with this article to explain the basic language of Java. The grammar of these basic languages is similar to some other programming languages, but there are many differences, and it is better for readers to spend some time understanding these basic grammars.

The first part of the basic language elements

A Java program is made up of a lot of parts, and any one of these words has a meaning to its existence. These words are the basic language elements of a Java program. This section explains these basic language elements, including identifiers, keywords, separators, and annotations .

First, identifiers

Identifiers are names of classes, methods, or variables that programmers define for themselves.

In the Java language, it is specified that identifiers consist of uppercase and lowercase letters, numbers, underscores (_), and dollar signs ($), but cannot begin with a number. You cannot use keywords in the Java language.

For example, HelloWorld, Hello_world, $HelloWorld are legal identifiers. However, several of the following are not legal identifiers.

    • 555HelloWorld (starts with a number).
    • ¥helloworld (with illegal character ¥).

Identifiers are strictly case-sensitive in Java, and hello and hello are completely different identifiers.

The correct identifier is not necessarily a good identifier. In a large program, it is often possible to define hundreds of identifiers, which can cause confusion if there is no good identifier naming habit. So the name of the identifier to express the meaning, such as defining a student class, you use student to name, and not for the sake of convenience defined as SD. In addition, there should be some habits defined by different identifiers.

    • Package Name: Use lowercase letters.
    • Class name and Interface name: typically defined as a word consisting of words with meaning, capitalized on the first letter of all words.
    • Method Name: Usually also consists of words with meaning, the first letter lowercase, and the first letter of the other words are capitalized.
    • Variable name: member variables and methods are the same, local variables all use lowercase.
    • Constant name: Use all uppercase, preferably using underscores to separate words.

Second, the key word

1. Access modifier keywords

    • Public: The classes, methods, and variables that are decorated are common, and other classes can access the class, method, or variable that the keyword modifies.
    • Protected: Used to modify methods and variables. These methods and variables can be accessed by classes or subclasses in the same package.
    • Private: Also modifies methods and variables. Methods and variables can only be accessed by the class in which they are located.

2. Class, method, and variable modifier keywords

    • Class: A class is defined by telling the word behind the system to be a class name.
    • Interface: Tells the word behind the system to be an interface name, which defines an interface.
    • Implements: Let the class implement the interface.
    • Extends: for inheritance.
    • Abstract: Abstraction modifier.
    • static modifier.
    • NEW: Instantiates the object.

There are also several uncommon classes, methods, and variable modifiers, such as native, STRICTFP, synchronized, transient, and volatile.

3. Process Control Keywords

The process Control statements include the If-else statement, the Switch-case-default statement, the For statement, the Do-while statement, the break statement, the continue statement, and the Ruturn statement, which are the Process Control keywords. There is also a keyword that should also be included in the Process Control keyword, which is the instanceof keyword, which is used to determine whether an object is an instance of a class or interface.

4. Exception Handling Keywords

The basic structure of exception handling is try-catch-finally, these three words are keywords, exception handling also includes throw and throws two keywords. The Assert keyword is used in an assertion operation and is also considered an exception handling keyword.

5. Package Control Keywords

There are only two packet control keywords, import and package, respectively. The InPort keyword is used to import a package or class into a program; the Pachage keyword is used to define the package and define the class to the package.

6. Data type Keywords

There are 8 basic data types in the Java language, each of which requires a keyword to be defined, in addition to Boolean (Boolean), character (char), byte type (byte), and numeric. The numeric type is also divided into short, int, long, flort, and double.

7. Special types and Method keywords

The Super keyword is used to refer to the parent class, and the This keyword is used to apply the current class object. The void keyword is used to define a generic method that does not have any return value. This keyword is available before the main method in the HelloWorld program.

8. Keywords that are not used

There are two other alternatives in the keyword family, which is const and Goto. The keyword is already known to be the word used by the system, but the two alternatives are keywords, but the system does not use them. This is where beginners should pay special attention, which is often asked in some exams or corporate interviews. Finally, it is obvious, but a lot of people do not notice the problem, that is all the keywords are lowercase, if capitalized, it is certainly not a keyword.

Third, comments

Comments are added to the code and are presented to the programmer, and when the system runs the program, the comments are read over without execution. With the development of technology, now has millions of lines of code of the program is very common, in such a large code, if there is no comment, it can be imagined that the subsequent modification and maintenance will have a lot of trouble. Provides a complete annotation mechanism in the Java language, with three annotation methods, namely single-line comments (//), Multiline Comments (/* */), and document annotations (/** */). Having good annotation habits is an indispensable professional quality for a good programmer. In this book, a single-line comment is used primarily to annotate the developed program. This may not be in accordance with some of the company's development specifications, here is mainly to explain knowledge. In the reader's work, according to their own development needs, to learn the relevant development norms.

Part two basic data types

Java is a strong data type language, and all data defined in a Java program has a fixed data type. Data types in Java can be divided into two basic categories: basic data Types (also known as raw data types) and composite data types. In this section, which focuses on basic data types, the focus of the learning data type is to understand the range of values for each data type.

First, constants and variables

References: Java from getting started to mastering

The basic language for getting started with Java

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.