Introduction to Java Syntax (Part 1): The Basic Components of the Java Language

The original poster: The disappearance of Suzumiya Views: 558 Replies: 3 Posted in: 2012-04-22 19:38:33
---------- The Java language is mainly composed of the following five elements: identifiers, keywords, literals, operators, and separators. These five elements have different syntactic meanings and composition rules, and they work together to complete the semantic expression of the Java language. The following is an explanation of each.

1: Identifiers
Variables, classes, and methods all require a certain name, which we call an identifier. Java has certain restrictions on identifiers. First, the first character of all identifiers must be a letter (uppercase or lowercase), an underscore _, or a dollar sign $; second, identifiers are composed of digits (0-9), all uppercase letters A-Z, lowercase letters a-z, the underscore _, the dollar sign $, and all ASCII codes before 0xc0 in hexadecimal; third, note that identifiers cannot use system-reserved keywords as identifiers.

These are the basic rules for naming identifiers. Below is a table of correct and incorrect examples, which provides a better understanding of identifier naming rules:

Valid Identifier | Invalid Identifier
-----------------|----------------
trytry | (Note: cannot be used as an identifier)
group_77 | group (Note: cannot start with a numeric symbol)
opendoor | open-door (Note: cannot use hyphen - as part of an identifier)
boolean_1 | boolean (Note: boolean is a keyword and cannot be used as an identifier)

2: Keywords
Keywords are identifiers used by the Java language itself and have specific syntactical meanings. All Java keywords cannot be used as identifiers. Java keywords include: abstract, continue, for, new, switch, boolean, default, goto, null, synchronized, break, do, if, package, this, byte, double, implements, private, threadsafe, byvalue, else, import, protected, throw, case, extends, instanceof, public, transient, catch, false, int, return, true, char, final, interface, short, try, class, finally, long, static, void, const, float, native, super, while, etc.

3: Data Types
Java has various data types, including: integer, floating-point, boolean, character, and string types.

Integer data is the most common data type and can be represented in decimal, hexadecimal, and octal formats. Hexadecimal integers must start with 0X.

Each integer occupies 32 bits of storage space, that is, four bytes.This means that the range represented by integer data is between -2,147,483,648 and 2,147,483,648. If, for some reason, you need to represent a larger number, a 64-bit long integer should be sufficient. If you want to force an integer to be stored as a long, you can add the letter 'l' after the number. Floating-point data is used to represent a decimal number with a fractional part, such as 1.35 or 23.6. This is the standard form of a floating-point number, and it can also be expressed in scientific notation. Here are some examples: 3.14159260, 34.86, 0.012349, 9.99E8. The standard floating-point number is called a single-precision floating-point number, which has a storage space of 32 bits, or four bytes. There are also 64-bit double-precision floating-point numbers. You can use the 'D' suffix to specify that you want to use this double-precision floating-point number. The Boolean type is the simplest data type. Boolean data has only two states: true and false, usually represented by the keywords 'true' and 'false'. Character data is a single character enclosed in a pair of single quotes. It can be any character from the character set, for example: 'a', 'b'. The string data type is a sequence of characters enclosed in a pair of double quotes. String data is actually implemented by the String class (the concept of class will be explained in detail in later chapters), rather than by character arrays as used in C. Each string data will generate a new instance of the String class. Readers do not need to worry about the relationship between strings and classes. Due to the characteristics of classes, you do not need to worry about how to implement them; they take care of themselves. It should be noted that strings are only implemented as classes in Java for security reasons. Here are a few examples of strings: "Howareyour" "IamStudent". 4: Operators Every language has its own operators, and Java is no exception. Operators such as +, -, *, / are operators. The function of an operator is to combine with certain operands to form an expression to perform the corresponding operation. Different data types have different operators. 5: Separators Separators are used to let the compiler know where code is separated. ';' and ':' are separators in the Java language.
reply 🤍 0 📤 share collect
If you want to understand Java, you first need to understand J2ME! To be precise, J2ME is actually a branch of JAVA...
I feel like when I localized Missile Invasion before, I might have changed the keywords of the Java language, no wonder it caused errors.
图片\Demacia!!! Awesome!
— All replies loaded —

Leave a Reply