Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the basic syntax of Java programming

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article focuses on "what are the basic syntax of Java programming". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn what are the basic syntax of Java programming.

I. the morphology of Java

1. Identifier

Identifiers are the names of classes, variables, methods, and so on, and their purpose is to enable the compiler to recognize them. In the Java language, identifiers can contain numbers, letters, underscores (_), and dollar symbols ($), but cannot start with a number. Unlike the Java Cure + language, the Java language can also include some non-Latin letters in identifiers because the Java language uses Unicode characters, and all Unicode characters above hexadecimal 00C0 can be used in identifiers. But keywords in Java cannot be used as identifiers.

When using identifiers, we should try our best to express the meaning of classes, variables, methods, etc. In addition, underscores can be used in identifiers, which we should make full use of to improve the readability of the program.

2. Keywords

Here are all the keywords for Java:

Abstract double int strictfp** boolean else interface super break extends long switch byte final native synchronized case finally new this catch float package throw char for private throws class goto* protected transient const* if public try continue implements return void default import short volatile do instanceof static while * unused keywords * * added keywords for Java 2

3. Notes

Java has three forms of comments:

* is the traditional annotation method of the C language, that is, the text between "/" and "* /" is regarded as a comment, which can span multiple lines.

The second is a C++-style comment, which treats the text after the "/ /" as a comment until the end of the line. This kind of comment can only contain one line.

The third is Java's new way of annotating, that is, text between "/ *" and "* /" is treated as comments, which can also boast of multiple lines. Note that comments generated in this way are saved as Javadoc documents.

II. Operators

1. Arithmetic operator

Operator usage priority associativity +,-- automatically increment, automatically decrement 2 (second only to parentheses) from right to left +,-unary plus, unary minus 2 from right to left *, / multiply, divide 3 from left to right% take remainder (module) 3 from left to right +,-add, subtract 4 from left to right

2. Boolean operations and relational operators

Operator usage priority associativity

>, =, > > move right as unsigned 5 from left to right & bit and 8 from left to right ^ bit XOR 9 from left to right | bit or 10 from left to right

4. Assignment operator

Operator usage priority associativity = assignment 14 from right to left * = Operation simultaneous assignment 14 right to left / = Operation simultaneous assignment 14 right to left% = Operation simultaneous assignment 14 right to left + = Operation simultaneous assignment 14 from right to left > > = Operation simultaneous assignment 14 from right to left > = Operation simultaneous assignment 14 from right to left > = Operation simultaneous assignment 14 from right to left ^ = Operation 14 from right to left Operation simultaneously assigns 14 from right to left & = operation also assigns 14 from right to left

5. Special operator

The cast operator converts one data type to another, and cast should be written to put the name of the type to correct the Operand in parentheses.

For example:

Int i; long l; lump 7876547; I = (int) l

If a high-precision variable is assigned a low precision value, it does not need to be corrected. The type correction operator has a priority of 2 and associativity from right to left.

Summarize the priority of the operators mentioned above as follows:

The precedence operator combines sequential 1 [] array operations. Dot operator () calls method from left to right 2! +-- + (unary plus)-(unary minus) () casts type conversion new from right to left 3 * /% from left to right 4 + (binary addition)-(binary subtraction) from left to right 5 > from left to right 6

< >

= instanceof from left to right 7 degrees =! = from left to right 8 & from left to right 9 ^ from left to right 10 | 11 degrees from left to right & 12 from left to right | | 13 degrees from left to right 14 = + =-= / =% = & = = ^ = > = from right to left

Third, constant

1. Integer constant

In the Java language, there are three forms of integer constants: decimal, octal, and hexadecimal. It is important to note that decimal numbers cannot start with 0 because octal numbers start with 0 and hexadecimal numbers start with 0x or 0X.

2. Floating point constant

Floating point numbers can only represent decimal numbers. There are two representations of floating point numbers, namely, standard representation and scientific notation. Floating-point numbers are divided into single-precision numbers and double-precision numbers.

3. Boolean constant

Boolean constants have two values: ture (or TURE) and false (or FALSE), which represent true and false, respectively. These two values are used to represent the state of things, and they must be one of them. You can determine whether to perform a step by measuring a Boolean value.

4. Character type constant

A character-type constant is a single character in the form of a character enclosed by two single quotes. But two single quotation marks cannot be quoted in single quotation marks and backslash, that is,''and'\ 'are incorrect.

It is important to keep in mind that the characters in the Java language are 16-bit Unicode characters, which is different from languages such as Cpicard +.

String constant

String constants are a series of characters enclosed by double quotation marks. Unlike in the Java language, strings are not implemented by an array of characters, but by string classes (that is, String classes).

Variables and data types

In Java, there are four types of data: basic data types, array types, classes, and interface types. Any data must be one of the above data types.

The basic data types can be divided into four categories: Boolean, character, integer and floating point. These types of data are basically the same as the types of constants described earlier, so I won't say much about them here. The ranges of values for integer and floating-point variables are listed below:

Table 1,

Values for type width range from byte8 bits to 256 bits 16 bits-32768 to 32767 int32 bits-2147483648 to 2147483647long64 bits-9223372036854775808 to 9223372036854775807

Table 2.

Positive integer 2147483647 (31 of 2 minus 1) 923372036854775807L (63 of 2 minus 1) octal * * positive integer 07777777777 (31 of 2 minus 1) 077777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777777 ) hexadecimal smallest negative integer 0x80000000 (- 2 to the power of 31) 0x8000000000000000L (- 2 to the power of 63)

The values that floating point numbers can represent from small to large are negative infinity, bearing limit, negative zero, positive zero, positive finite value and positive infinity. Positive zero and negative zero are equal in numerical comparison, but there is still a way to distinguish between the two values, for example, a positive infinite result is obtained at 1.0 and 0.0. Other finite values of non-zero and non-infinity are expressed in terms of slogm * (2 ^ e). The ranges of s and m are shown in the following table:

Table 3.

Positive integers of type smfloat + 1 or-1 to the 24th power of 2. Integers double + 1 or-1 to the 53rd power of 2 are integers 1045 to 1000.

What is different from the CAccord + language is that in the Java language, all basic data types have presets, which means that when we declare a variable, even if we don't assign a value to it, its value is certain, not as unpredictable as it is in CAccord.

5. The comparison between Java and CCompact.

Since the syntax of Java comes directly from CCompact, the program control statement of Java is exactly the same as that of Cmax, so I won't say any more here. The following focuses on the differences between Java and Cmax Cure +.

Pointer

Everyone must have been bothered by the complex calculation of pointers when programming in C language. In the process of programming with C-pointer + language, pointers may cause a lot of errors. The errors caused by these complex pointer algorithms are often puzzling, resulting in a lot of time wasted by programmers here. For this reason, pointers are removed from Java. The function of the pointer is replaced by reference, which makes it easier to implement complex data structures in Java, because it is more reliable to implement complex data structures with objects and object arrays.

Function

There is no widely used function in Java, and the functions realized by functions in Java are realized by classes and methods.

Inherit

C++ supports multiple inheritance, which is an effective way to derive a class from multiple parent classes, but this derivation is complex and problematic. Therefore, multiple inheritance is not used in Java, but Java's Objective C protocol interface can accomplish all the functions of multiple inheritance in C++.

Goto statement

As we all know, using too many Goto statements when programming in a high-level language can make the program very confusing, so the Goto statement is not used in Java. However, it is undeniable that when the program is easy to control, the proper use of Goto statements can make the program run faster, so multiple break and continue statements (allowing break and continue statements to use tags) are used in Java to achieve the function of C++ goto statements.

Union and structure

There are three kinds of data structures in C++: Union, structure and class; there are only classes in Java, which can implement the functions of the above three.

String

The string is implemented through the character array in C _ pact _ blank +, but because the space of the array is not dynamically allocated, it is prone to out-of-bounds errors, for example, there are often strings that cannot be entered after the predetermined character array. In Java, a string is a specific class of objects, which makes the string more consistent and predictable.

Typedef, Define, and preprocessor

No # define statements, preprocessors, or typedef are required in Java, but header files are required. In Java, the information about a class must be contained in one file, while the information about the class in Chand Category + may be scattered across multiple files.

Automatic casting of data types

Forcing a type of data element to become a less precise data type in Java requires programmers to use predictive methods.

At this point, I believe you have a deeper understanding of "what are the basic syntax of Java programming?" you might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report