Home  >  Article  >  Java  >  What are the knowledge points about keywords in Java?

What are the knowledge points about keywords in Java?

WBOY
WBOYforward
2023-05-17 13:13:381070browse

Basic syntax of java

1. Keyword

Definition: A string given a special meaning by the Java language and used for special purposes

Features: All letters in the keyword are lowercase

What are the knowledge points about keywords in Java?

2. Identifier

Definition: The character sequence used by Java to name various variables, methods, classes and other elements is called an identifier (any place where you can give a name is called an identifier)

Legal rules:

1. Composed of 26 English letters, uppercase and lowercase, 0-9, _ or $

2. Numbers cannot begin with

3 .Keywords or reserved words cannot be used, but keywords and reserved words can be included

4.Java is strictly case-sensitive and has no length limit

5.Identifiers cannot contain spaces

Naming convention:

1. Package name: When composed of multiple words, all letters are lowercase: xxxyyyzzz

2. Class name, interface name: When composed of multiple words, all letters The first letter of the word is capitalized:

Constant names should be in all uppercase letters, and if they consist of multiple words, underscores should be used to connect each word: XXX_YYY_ZZZ

3. Variable

Concept:

1. It is a storage area in the memory

2. This area has its own name (variable name) and type (data type)

3. Each variable in java must be declared (defined) first, and then used

4. The data in this area can continue to change within the same type range (for example, declare int i = first 1; then declare i = 2; then the value of i changes from 1 to 2)

5. Variables access this area through variable names

Define variable format: Data type variable Name = initialization value

Note:

1. The scope of the variable: valid between a pair of { }

2. There must be an initialization value (the first time given Variable assignment): For example, int i = 0; If only int m; is used directly, it is an error because the variable is not initialized

3. The = in the process of declaring a variable is different from the = in the mathematical sense, in java Indicates assignment

Classification of variables - by data type (except for the 8 basic data types, the others are reference data types)

8 basic data typesWhat are the knowledge points about keywords in Java?

1. Integer types: byte, short, int, long

Note:

1. Each integer type in java has a fixed table number range and byte length are not affected by the specific OS to ensure the portability of java programs. For example: byte b = 129 exceeds the range of byte table numbers and is illegal

2.java's integer constant default For int type

3. When declaring a long type constant, add ‘l’ or ‘L’ (long integer type). For example: long l = 6L (it is best to use uppercase L, because lowercase l and 1 resemblance)

2. Floating point types: float, doubleWhat are the knowledge points about keywords in Java?

Note:

1.Java floating point types also have a fixed table number range and field length, which are not affected by OS impact

2. Java’s floating-point type constants default to double type

3. To declare a float type constant, add ‘f’ or ‘F’

4. Floating-point constants have two representation forms: decimal number form (such as 5.12 512.0f .512) must have a decimal point

Scientific notation form (such as 5.12e2 512E2 100E-2) ending with "E "Number" indicates the power of 10 to be multiplied by the number before E. For example, 3.14E3 is 3.14 × 103 =3140, and 3.14E-3 is 3.14 x 10-3 =0.00314.

3. Character type: charWhat are the knowledge points about keywords in Java?

Note:

1.Char type data is used to represent "characters" in the usual sense (2 bytes)

2. Expression form of character constants: Character constants are single characters enclosed in English single quotes'', covering all written language characters in the world. For example: char c1 = 'a'; char c2 = '6'; char c3 = '中';

Java also allows the use of the escape character '\' to convert the following characters into special characters type constant. For example: char c3 = '\n'; // '\n' represents the newline character

3. The char type can be operated on because it all corresponds to Unicode code.

Boolean type: booleanWhat are the knowledge points about keywords in Java?

Note: 1. The boolean type is suitable for logical operations and is generally used for program flow control: if conditional control statement; while loop control statement; do-while Loop control statement;for loop control statement;

2.Boolean type data only allows the values ​​true and false, no null (0 or non-0 integers cannot be used to replace true and false) For example: boolean b1 = true; or boolean b1 = false;

Besides the basic types are reference types: for example, String class

Features of reference types:

1. In Java, reference type variables are very similar to C/C pointers. A reference type points to an object, and a variable pointing to an object is a reference variable. Specify a specific type when declaring, like Employee, Puppy, etc. Once a variable is declared, its type cannot be changed.

2. Objects and arrays are reference data types.

3. The default value of all reference types is null.

4. A reference variable can be used to reference any compatible type.

String class:

1. The value null can be assigned to any reference type variable, which is used to indicate that the address saved in this reference variable is empty. The String class belongs to the reference type and can be assigned with null.

String class typically embodies the characteristics of immutable classes. Once a String object is created, its content cannot be changed. The created string will be stored in the data area to ensure that there is only one constant for each string and no multiple copies will be generated. For example: int i0 = 1; int i1 = 1; In this case, two 1 values ​​will be stored in the memory. , and String s0 = "hello"; String s1 = "hello"; In this case, there will only be one "hello" in the memory. Assume that the memory address of "hello" is xxxxxx. When declaring the s0 variable, assign the value "hello" to s0 In fact, the s0 variable refers to the memory address xxxxxx of "hello". When we declare that the variable s1 is also assigned the value "hello", we actually directly reference the existing memory address of "hello" to s1

3. The String class can be spliced ​​with a plus sign, for example: String s3 = "he" "ll" "o"; The output is "hello"

Basic data type conversion

1. Automatic type Conversion: Data types with small capacity are automatically converted to data types with large capacity. Data types are sorted by capacity as follows:

What are the knowledge points about keywords in Java?

The system will automatically convert all data into the data type with the largest capacity when performing mixed operations on multiple types of data, and then perform calculations

3.byte, short, char, There is no mutual conversion between them. The three of them are first converted to int

4 during calculation. When any basic type value is connected to a string ( ), the basic type value will Automatically converted to string type

Note: 1. Type conversion cannot be performed on boolean type.

 2. The object type cannot be converted into an object of an unrelated class.

 3. Forced type conversion must be used when converting a large-capacity type into a small-capacity type.

4. The conversion process may cause overflow or loss of precision, for example:

int i =128;

byte b = (byte)i;

Because the byte type is 8 bits and the maximum value is 127, when the int is cast to the byte type, an overflow will occur when the value is 128.

 5. The conversion of floating point numbers to integers is obtained by discarding decimals instead of rounding, for example:

     (int)23.7 == 23; 45.89f == -45;

 6. When there is a series of operations, if a certain part contains a string, then the string will be viewed according to string concatenation in the future, for example:

  String str = 1 2 3 "a" 4 5; The printed result is 6a45

Forced type conversion

1. The condition is that the converted data types must be compatible.


2. Format: (type)value type is the data type to be forced to type conversion


int k = 7;


byte b = (byte)k; //The converted data type should be enclosed in parentheses


Generally, strings cannot be directly converted to basic types, but they can be packaged by basic types. class to accomplish this conversion.

The above is the detailed content of What are the knowledge points about keywords in Java?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete