Java basic syntax (1)
One keyword
Keyword Overview: some special-purpose words in Java are called keywords. The keyword has a special significance for the Java compiler.
Exercise caution when using the sequence.
Keyword features: all the letters that make up the keyword are in lower case.
Note: goto and const exist as reserved words and are not currently used.
50 frequently-used keywords in Java (when 48 words are added with 2 reserved words, the English words are memorized ):
Keyword description
Abstract method, modifier of abstract class
Whether the assert conditions meet
Boolean data type
Break jump out of loop or label code segment
Byte 8-bit Signed Data Type
A condition of the case switch statement
Catch and try catch exception information
Char 16-bit Unicode character data type
Class Definition class
Const not used
Continue does not execute the remaining part of the loop body
Default branch in the default switch statement
Do Loop statement, the loop body will execute at least once
Double 64-bit double-precision floating point number
Branch to be executed when the else if condition is invalid
Enum Enumeration type
Extends indicates that one class is a subclass of another class.
Final indicates that a value cannot be changed after initialization; it indicates that the method cannot be overwritten, or a class cannot have subclasses.
Finally is designed to complete the executed code, mainly for the robustness and integrity of the program. The code is executed no matter whether exceptions occur.
Float 32-bit Single-precision floating point number
For loop statements
Goto not used
If Condition Statement
Implements indicates that a class implements the interface
Import class
Instanceof tests whether an object is an instance of a class.
Int 32-bit integer
Interface, an abstract type, with only the definition of methods and constants
Long 64-bit integer
Native representation is implemented using non-java code
New allocates a new class instance
Package a series of related classes to form a package
Private indicates a private field or method, which can only be accessed from inside the class.
Protected indicates that a field can only access the subclass or other classes in the same package through the class or its subclass.
Public indicates a total of attributes or methods.
Return method return Value
Short 16-digit
Static indicates that all instances share
Strict rules apply to strictfp floating point numbers.
Super indicates the base class
Switch selection statement
Synchronized indicates a code block that can only be accessed by one thread at a time.
This indicates calling the current instance or calling another constructor.
Throw an exception
An exception that may be thrown by the throws definition method
Transient modifier fields not serialized
Try indicates that the code block must be exception handled or used together with finally to indicate whether to throw an exception and execute the code in finally.
The void flag method does not return any value.
The volatile flag field may be accessed by multiple threads at the same time without synchronization.
While LOOP
Binary identifier
Identifier Overview: The character sequence used by Java for names of various variables, classes, interfaces, and methods.
Composition rules: 1 uppercase/lowercase letters, 2 numbers, 3 dollar characters ($), and underscores (_).
When using identifiers, follow the following rules:
1. An identifier can contain letters, numbers, underscores (_), and dollar signs ($), but cannot contain other special characters such as @, %, and space. It cannot contain numbers.
. For example, 123 name is invalid.
2 identifiers cannot be Java keywords or reserved words (reserved keywords in Java, which may be used as keywords in later versions), but can contain
Key and reserved words. For example, void cannot be used as an identifier, but Myvoid can.
3. The identifier is case sensitive. Therefore, we must clearly distinguish between two different identifiers.
4. It is recommended that the name of an identifier reflect its role, so that you can understand the name.
Invalid identifier:
Valid identifier:
Common types of identifier naming:
One package (actually a folder, used to solve the problem of distinguishing the same class name), all in lower case.
Single Stage:
Example: student
Multilevel:
Example: student. number
Two types or interfaces:
One word: the first letter of a word must be capitalized.
Example: Student
Multiple words: the first letter of each word must be uppercase.
Example: HelloWorld
3 methods and variables:
One word: the first letter of a word must be in lowercase.
Example: main
Multiple words: starting from the second word, the first letter of each word must be capitalized.
Example: showAllName
4 constants:
One word: all words and letters must be in uppercase.
Example: PI
Multiple words: Each letter must be in uppercase and separated.
Example: STUDENT_MAX_AGE
3. Note
Note Overview: when writing a program, you often need to add some notes to describe the role of a piece of code. In general, for a standardized procedure
For source code, comments should account for more than 1/3 of the source code. Therefore, annotations are an important part of the program source code and must be paid attention.
Classification and format of annotations in Java:
There are three types of annotations in Java: single-line comments, multi-line comments, and document comments.
1 single line comment format: // comment text
2. Multi-line comment format:/* Comment text */. Note that multi-line comment cannot be nested, but a single line can.
3. Document comment:/** comment text */. This comment is used by the javadoc tool to parse and generate a statement.
Comment:
1. Explain the program to improve the readability of the program;
2. It can help us debug the program.
Let's look at an example:
Four constants and variables
Constant Overview: the value of a program cannot be changed during execution. A constant is a special variable whose value is
After the setting, it cannot be changed while the program is running.
Constant classification in Java:
1-character nominal value constant
A String constant: Content enclosed in double quotation marks.
Example: "hello" "world"
B integer constant: All integers.
Example: 100 300
C decimal constant: all decimals.
Example: 10.23
D character constant: a constant enclosed in single quotes.
Example: 'A ''c' o'
E Boolean constant: Special, only true and false
F null constant: null
2. Custom Constants
Syntax format: final constant name = value;
Using constants in a program can improve the maintainability of the Code. For example, during project development, we need to specify the user's gender. In this case, we can define
Constant SEX, with a value of "male". You can directly call this constant where you need to specify the user's gender to avoid program output due to the user's nonstandard value assignment.
Error.
Example:
Variable
Variable Overview: During program execution, the value is the amount that can be changed within a certain range.
In Java, three elements are used to describe variables: variable type, variable name, and variable value. The variable name is the identifier.
Variable Definition Format:
A: data type variable name = initialization value;
B: data type variable name;
Variable name = initialization value;
Java is a strongly typed language. In layman's terms, the data stored in Java is of a type and must be determined at compilation.
Type. String is a common reference data type used to represent strings. In program development, many operations must be completed using strings, such
User name, password, and email address. This String reference type is special.
Java has two types of data: Eleven types
In the Java field, the basic data type variables are stored in the data itself, while the reference type variables are stored in the space address of the data. Here we use
After learning, I gradually realized it.
Basic Data Type
A: The integer occupies the number of bytes.
Byte 1
Short 2
Int 4
Long 8
B: Floating Point Number
Float 4
Double 8
C: character
Char 2
D: Boolean
Boolean 1 (not sure)
Notes for using variables:
1. Scope: The scope of the variable in which the braces are defined is the scope of the variable. Two cannot be defined in the same scope.
Variable with the same name.
2. initialization value: values without initialization cannot be used directly.
3. We recommend that you define only one variable and multiple variables in a row, but not.
4. Variables in Java must be declared first and then used.
5. The variable can only be assigned one value at a time, but can be modified multiple times.
6. variables defined in the main method must be assigned values before output.
In Java programs, data of different basic data types often needs to be converted to each other.
Note the following when defining the basic data types of variables:
1 The default Integer type is int, and the default value is double.
2. The long integer must be L or l (L is recommended ).
3. F or f must be added to a single-precision floating point number (F is recommended ).
(1) The boolean type is not involved in conversion.
(2) Default conversion
A: from small to large
B: byte, short, char -- int -- long -- float -- double
C: byte, short, and char are not converted to each other and directly converted to the int type for calculation.
(3) forced conversion
A: from big to small
B: There may be loss of precision. It is generally not recommended.
C: format:
Target data type variable name = (target data type) (converted data );
Questions and questions
A: Are there any differences between the following two methods?
Float f1 = 12.345f;
Float f2 = (float) 12.345;
B: Is there a problem with the following program? If so, where?
Byte b1 = 3;
Byte b2 = 4;
Byte b3 = b1 + b2;
Byte b4 = 3 + 4;
C: What are the following operation results?
Bytes a = 130;
Byte B = (byte) 130;
D: character involved in Calculation
Is to find the value in ASCII, just query the ASCII table
'A' 97
'A' 65
'0' 48
System. out. println ('A ');
System. out. println ('A' + 1 );
E: String involved in Operation
This is actually a string connection.
System. out. println ("hello" + 'A' + 1 );
System. out. println ('A' + 1 + "hello ");
System. out. println ("5 + 5 =" + 5 + 5 );
System. out. println (5 + 5 + "= 5 + 5 ");
The output of the above two questions is:
First come here. If there is any error, please correct it.