The 4th chapter of Java syntax based on Java from small white to Daniel Lite

Source: Internet
Author: User
Tags goto variable scope java keywords

This chapter mainly introduces some basic Java syntax, including identifiers, keywords, reserved words, constants, variables, expressions and so on.

4.1 identifiers, keywords, and reserved words

Any computer language can be separated from identifiers and keywords, so the following details the Java identifiers, keywords, and reserved words.

4.1.1 Identifier

Identifiers are names specified by the programmer, such as variables, constants, methods, enumerations, classes, interfaces, and so on. There are certain specifications for the letters that make up identifiers, and the naming rules for identifiers in the Java language are as follows:

    1. Case-sensitive: MyName and myname are two different identifiers.

    2. The first character, which can be an underscore (_) or a dollar symbol or letter, but not a number;

    3. Other than the first character, it can be an underscore (_), dollar symbol, letter, and number.

    4. The keyword cannot be used as an identifier.

For example, height, identifier, userName, user_name, $Name, _sys_val, etc. are legal identifiers, note that the Chinese "height" variable is legal, while 2mail, room#, and class are illegal identifiers, note # is an illegal character, and class is a keyword.
**
Note that the letters in the Java language use a double-byte Unicode encoding. Unicode is called a unified coding system, which contains Asian text encodings, such as Chinese, Japanese, and Korean characters. **

4.1.2 Keywords

The keyword is a sequence of reserved characters similar to an identifier, which is defined by the language itself and cannot be used as a 50 keyword in the Java language, as shown in table 4-1.

Table 4-1 Java Keywords

Break
Abstract assert Boolean byte
Case Catch Char Class Const
Continue Default Do Double Else
Enum Extends Final Finally Float
For Goto If Implements Import
instanceof Int Interface Long Native
New Package Private Protected Public
Return Strictfp Short Static Super
Switch Synchronized This Throw Throws
Transient Try void Volatile While

Many of the keywords are not introduced here, but what readers need to remember is that the keywords in Java are all lowercase letters.

4.1.2 Reserved words

There are a number of character sequences in Java that cannot be used as identifiers or keywords or used in programs, which are called reserved words. Reserved words in the Java language have only two Goto and const:

    1. Goto: Called an infinite jump statement in other languages, the Goto statement is no longer used in the Java language because the infinite jump statement destroys the program structure. The substitution statements for Goto in the Java language can implement a "finite jump" through break, continue, and return.

    2. Const: In other languages, the constant keyword is declared, and the declaration of constants in the Java language is declared using the public static final method.

4.2 Java delimiter

In the Java source code, some characters are used as separators, called delimiters. Separators are mainly: semicolons (;), left and right braces ({}), and white space.

1. Semicolon

A semicolon is the most commonly used delimiter in the Java language, which represents the end of a statement. The following code:

int totals = 1 + 2 + 3 + 4;12

Equivalent to

int totals = 1 + 3 + 4;1234
2. Curly Braces

In the Java language, a collection of statements, surrounded by left and right curly braces ({}), is called a statement block (block) or compound statement, and a statement block can have a 0~n statement. When a class or method is defined, the statement block is also used to delimit the class body or method body. Statement blocks can also be nested, and there is no limit to the nesting hierarchy. The sample code is as follows:

public class HelloWorld {public static void main (String args[]) {int m = 5;        if (M <) {System.out.println ("<10"); }}}123456789101112
3. Blank

Whitespace is allowed between elements in the Java source code, and there is no limit to the number of blanks. Whitespace includes spaces, tabs (tab input) and newline characters (enter key input), and appropriate whitespace can improve readability of the source code. The following pieces of code are equivalent.

if (M <) {System.out.println ("<10");} 12

Equivalent to

if (M <) {System.out.println ("<10");} 1234

Equivalent to

if (M <) {System.out.println ("<10");} 123
4.3 variables

Variables and constants are important parts of an expression, and the inside of a variable can be modified. Variables include variable names and variable values, and the declared format of variables is:

Data type variable name [= initial value];

Variable names are subject to the naming conventions with identifiers, but cannot have duplicate variable names in the relevant scopes. Variable scope is the scope of use of variables, in which variables can be used, beyond the scope, the contents of variables are released, according to the scope of the different divided into: member variables and local variables, the sample code is as follows:

public class helloworld {    //  declares a member variable of type int     int  y; ①    public static void main (String[] args)  {         //  declaration int type local variable          int x; ②        //  declaring float type variable and assigning value          float f = 4.5f; ③         // x = 10;        system.out.println ("x =   " + x";//  compilation error, local variable  x not initialized  ④         System.out.println ("f = "  + f);        if  (f &NBSP;&LT;&NBSP;10)  {            //  Declarative local Variables             int m = 5; ⑤         }        system.out.println (m); //  compilation error  ⑥    }}12345678910111213141516171819202122232425

The code in the code in the ① line is declared member variable y, the member variable is in the class body, and outside the method, the scope is the entire class, if there is no initial assignment, the system assigns it a default value, each data type has a default value, the int type default value is 0.

The code ②, ③, ⑤ lines declare local variables, which are variables declared in a code block such as a method or if, for, and while, and the ② and ③ lines declare the scope of the local variable as the entire method, and the M variable scope declared by the ⑤ line is the current if statement.

In addition, the code ④ and ⑥ rows have a compile error method because the ④ line is because X is not initialized before use, and unlike member variables, local variables must be initialized before they are used. The code ③ line is initialized at the same time as the declaration. The error in line ⑥ of the code is because the m variable exceeds the scope.

4.4 Constants

Constants are actually variables whose contents cannot be modified, and constants similar to variables also need to be initialized, which is to give an initial value while declaring a constant. Constants cannot be modified once they are initialized. Its declaration format is:

Final data type variable name = initial value;

The final keyword represents the ultimate, it can modify many elements, and the modifier variable becomes a constant. The sample code is as follows:

public class helloworld {    //  static constants, replacing const     public static final double pi = 3.14; ①    //  Declaring member Constants     final int y = 10;   ②     Public static void main (String[] args)  {         //  declaring local constants         final double x = 3.3;  ③    } }1234567891011121314 

In fact, there are three types of constants: Static constants, member constants, and local constants. The code, ①, declares a static constant, using the public static modifier before final to preserve the word const. The constant scope of the public static modifier is global, and you do not need to create an object to access it in the form outside the class: HelloWorld. PI, this constant is used in programming a lot.

The Code section ② declares member constants, which are similar to member variables but cannot be modified. The Code section ③ declares local constants, which are similar to local variables but cannot be modified.

Summary of this chapter

This chapter introduces the most basic syntax in the Java language, first of all the identifiers, keywords and reserved words, the reader needs to master the identifier composition, understand the Java keyword and reserved words. Then we introduce the delimiters in Java, and finally introduce the variables and constants, the readers need to know the types and scopes of variables, and the declarations of constants.

Companion video

Http://edu.51cto.com/topic/1246.html

Supporting source code

http://www.51work6.com/book/java1.php

and the free version of this book corresponds to a fee version:
    1. Enter Baidu to read ebook

    2. Access to Turing Community ebook

This article is from "The Wisdom Jie Classroom" Dongsheng blog "blog, reprint please contact the author!"

The 4th chapter of Java syntax based on Java from small white to Daniel Lite

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.