Java language foundation
1. The difference between public class and class
* Multiple classes can be defined in one java source file
*The public class in a java source file is not necessary
*A class will define and generate a xxx.class bytecode file
*If a public class is defined in a java source file, there can only be one public class, and the name of the class must be consistent with the java source file name.
*The main method can be written in each class, and the entry of the program can be set. I want to execute the main method in B.class: java B , I want to execute the main method in X.class : java X
Note: When in the command window Execute java Hello in Hello.class, then there must be a main method in Hello.class. Without the main method, there will be runtime errors.
2. Identifier
1 What is an identifier?
-In the java source program, all words that programmers have the right to name themselves are identifiers.
-Identifiers are highlighted in black in the EditPlus editor
2 The naming rules of identifiers?
*A legal identifier can only consist of "numbers, letters, underscore_, dollar sign $", and cannot contain other symbols.
*Cannot start with a number
*Strictly case-sensitive
*Keywords cannot be used as identifiers
*There is no length limit in theory, but it is best not to be too long
3 The naming convention for identifiers? (It is just a specification, not a grammar, the compiler does not report an error)
* It is best to know the name
* Comply with CamelCase naming rules
SystemService
UserService
CustomerService
* Class name, interface name: capitalize the first letter, capitalize the first letter of each word after it.
* Variable name, method name: the first letter is lowercase, and the first letter of each word after it is uppercase.
* Constant name: all uppercase.
4 Legal and illegal identifiers
legitimate | illegal |
---|---|
_123Test | 123Test |
HelloWorld | Hello-World |
A_B_C | Hello World |
$ABC | HelloWorld# |
public1 | public |
3. Keywords and meaning
Keywords are all lowercase in Java
abstract indicates that the class or member method has abstract properties
assert assertion, used for program debugging
One of the basic data types of boolean, a keyword that declares the boolean type
break out of a block in advance
byte one of the basic data types, byte type
Case is used in the switch statement to indicate that one of the branches catch is used in exception handling to catch exceptions
One of the basic data types of char, the character type
class declares a class
const reserved keyword, no specific meaning
continue back to the beginning of a block
default Default, for example, used in a switch statement to indicate a default branch. Java 8 also acts on declaring the default implementation of interface functions
do is used in the do-while loop structure
One of the basic data types of double, double-precision floating-point number type
else is used in conditional statements to indicate the branch when the condition is not established
enum
extends indicates that one type is a subtype of another type. For class, it can be another class or abstract class; for interface, it can be another interface
final is used to describe the final attribute, indicating that a class cannot be derived from a subclass, or member methods cannot be overridden, or the value of a member field cannot be changed, used to define constants
finally is used to handle exceptions, and is used to declare a statement block that will basically be executed
One of float basic data types, single-precision floating-point number type
for a guiding word for a loop structure
goto reserved keywords, no specific meaning
The leading word of the if conditional statement
implements indicates that a class implements a given interface
import indicates that you want to access the specified class or package
instanceof is used to test whether an object is an instance object of a specified type
int one of the basic data types, integer type
interface interface
One of long basic data types, long integer type
Native is used to declare that a method is implemented by a computer-related language (such as C/C++/FORTRAN language)
new is used to create new instance objects
package package
private An access control method: private mode
protected An access control method: protected mode
public An access control method: shared mode
return returns data from member methods
One of short basic data types, short integer type
static indicates that it has static properties
strictfp is used to declare that FP_strict (single-precision or double-precision floating-point numbers) expressions follow the IEEE 754 arithmetic specification
super indicates the reference to the parent type of the current object or the construction method of the parent type
The leading word of the switch branch sentence structure
synchronized indicates that a piece of code needs to be executed synchronously
this refers to a reference to the current instance object
throw throws an exception
throws declares all exceptions that need to be thrown in the currently defined member method
transient declares a member field not to be serialized
try try a block that may throw an exception
void declares that the current member method has no return value
Volatile indicates that two or more variables must change synchronously
while used in loop structure