Java keywords and their functions

I. Overview:

 

5 Abstract class extends final implements Interface Native new 6 Static Strictfp synchronized TRANSIENT volatile 7 8 program control 9 Break continue return do while if else for instanceof switch 10 Case Default 11 12 Exception handling 13 Try cathc throw throws 14 15 Package related 16 Import Package 17 18 Basic type 19 Boolean byte char double Float int long short NULL true false 20 21 Variable reference 22 super this void 23 24 Reserved word 25 goto constCopy the code

 

Second, detailed explanation

1. Access control

1) Private

The private keyword is an access control modifier that can be applied to a class, method, or field (a variable declared in a class). You can only reference a private (internal) class, method, or field in a class that declares it. They are not visible outside the class or to subclasses. The default access scope for all class members is package access, which means that class members can be accessed from any class in the same package unless specific access-control modifiers are present.

2) Protected

The protected keyword is an access control modifier that can be applied to a class, method, or field (a variable declared in a class). You can reference protected classes, methods, or fields in any class that declares them, any other class in the same package, and any subclass (regardless of the package in which the subclass is declared). The default access scope for all class members is package access, which means that class members can be accessed from any class in the same package unless specific access-control modifiers are present.

3) It’s public

The public keyword is an access control modifier that can be applied to a class, method, or field (a variable declared in a class). Public classes, methods, or fields may only be referenced in any other class or package. The default access scope for all class members is package access, which means that class members can be accessed from any class in the same package unless specific access-control modifiers are present.

2. Class, method, and variable modifiers

1) Abstract

The abstract keyword can modify a class or method. Abstract classes can be extended (adding subclasses), but cannot be instantiated directly. The abstract method is not implemented in the class in which it is declared, but must be overridden in a subclass. Classes that use abstract methods are abstract classes and must be declared abstract.

2) class class

The class keyword is used to declare a new Java class, which is a collection of related variables and/or methods. Class is the basic construction unit of object-oriented programming method. Classes usually represent some physical entity, such as a geometric shape or a person. A class is a template for an object. Each object is an instance of a class. To use a class, you typically use the new operator to instantiate the object of the class and then call the class’s methods to access the class’s functionality.

3) extends

The extends keyword is used in a class or interface declaration to indicate that the declared class or interface is a subclass of a class or interface whose name is followed by the extends keyword. Subclasses inherit all public and protected variables and methods from their parent class. Subclasses can override any non-final method of their parent class. A class can extend only one other class.

It is final

The final keyword can be applied to a class to indicate that the class cannot be extended (there can be no subclasses). The final keyword can be applied to a method to indicate that the method cannot be overridden ina subclass. A class cannot be abstract and final at the same time. Abstract means that the class must be extended, and final means that it cannot be extended. A method cannot be abstract and final at the same time. Abstract means that the method must be overridden, and final means that the method cannot be overridden.

5) implements

The implements keyword is used in the class declaration to indicate that the declared class provides implementations of all methods declared in the interface specified by the name after the implements keyword. A class must provide implementations of all methods declared in the interface. A class can implement multiple interfaces.

6) interface interface

The interface keyword is used to declare a new Java interface, which is a collection of methods.

Interfaces are a powerful feature of the Java language. Any class can declare that it implements one or more interfaces, which means that it implements all the methods defined in those interfaces.

Any class that implements an interface must provide implementations of all methods in that interface. A class can implement multiple interfaces.

7) native land

The native keyword can be applied to a method to indicate that the method is implemented in a language other than Java.

8) New

The new keyword is used to create a new instance of the class.

The argument after the new keyword must be the class name, and the class name must be followed by a set of constructor arguments (parentheses must be used).

The parameter collection must match the signature of the constructor of the class.

The type of the variable to the left of = must be assignment compatible with the class or interface to be instantiated.

9) the static static

The static keyword can be applied to inner classes (classes defined in another class), methods, or fields (member variables of the class).

In general, the static keyword means that the entity to which it is applied is available outside of any particular instance of the class in which it is declared.

A static (inner) class can be instantiated and referenced by other classes (even if it is a top-level class). In the example above, code in another class can instantiate the MyStaticClass class by qualifying its name with the name of the class that contains it, such as myClass.myStaticClass.

Static fields (member variables of a class) exist only once in all instances of a class.

Static methods can be called from outside the class without instantiating the class first. Such a reference always includes the class name as the qualifier for the method call.

Public final static <type> varName = <value>; Commonly used to declare class constants that can be used outside of a class. References to such class constants need to be qualified by the class name. In the example above, another class could reference the MAX_OBJECTS constant in the form myClass.max_objects.

10) StrictFP is strict and accurate

Strictfp means FP-strict, which means precise floating point. If strictFP keyword is not specified in Java virtual machine floating-point operation, the Compiler and runtime environment of Java take a kind of almost arbitrary behavior to complete these operations on the expression of floating-point operation, so that the results are often not satisfactory. When strictFP is used to declare a class, interface, or method, the declared scope of the Java compiler and runtime environment is executed according to the floating-point specification IEEE-754. So if you want to make floating-point calculations more accurate and not have inconsistent results from different hardware platforms, use the keyword strictfp.

You can declare a class, interface, and method as STRICtFP, but you are not allowed to declare strictFP keywords for methods and constructors in the interface

11) synchronized

The synchronized keyword can be applied to methods or blocks of statements and provides protection for critical code segments that should only be executed by one thread at a time.

The synchronized keyword prevents key sections of code from being executed by multiple threads at once.

If applied to static methods, the entire class is locked when the method is executed by one thread at a time.

If applied to an instance method, the instance is locked when the method is accessed by one thread at a time.

If applied to an object or array, the object or array is locked when the associated code block is executed by one thread at a time.

12) transient is short

The transient keyword can be applied to a member variable of a class to indicate that the member variable should not be serialized when the class instance containing it has been serialized.

When an object is serialized, transient variables are not included in the serialized representation, whereas non-transient variables are included.

Java’s serialization provides a mechanism for persisting object instances. When an object is persisted, there may be a special object data member that we do not want to use serialization mechanisms to store. To turn off serialization on a field for a particular object, you can prefix the field with the keyword TRANSIENT. Transient is a Java language keyword used to indicate that a field is not part of the object’s serialization. When an object is serialized, transient variables are not included in the serialized representation, whereas non-transient variables are included.

13) volatile volatile

The volatile keyword is used to indicate a member variable that can be asynchronously modified by multiple threads.

Note: The volatile keyword is not implemented in many Java virtual machines. The goal of volatile is to ensure that all threads see the same value for a specified variable.

Volatile variables in the Java language can be thought of as a form of “lesser synchronized”; Volatile variables require less coding and have less runtime overhead than synchronized blocks, but they do only a fraction of what synchronized does.

3. Program control statement

1) Break off

The break keyword is used to exit the for, while, or DO loop early, or to end a case block in a switch statement.

Break always exits the deepest while, for, do, or switch statement.

2) continue to continue

The continue keyword is used to jump to the next iteration of the for, while, or DO loop.

Continue always skips to the next iteration of the deepest while, for, or DO statement.

3) return to return

The return keyword causes the method to return to the method that called it, passing a value that matches the return type of the returned method.

If a method has a return type other than void, the return statement must have arguments of the same or compatible type.

The parentheses around the return value are optional.

4) do run

The do keyword is used to specify a loop that checks its condition at the end of each iteration.

The do loop body executes at least once.

A conditional expression must be followed by a semicolon.

5) the while loop

The while keyword is used to specify a loop that repeats as long as the condition is true.

6) if if

The if keyword indicates conditional execution of a block of code. The result of the condition must be a Boolean value.

An if statement can have an optional else clause that contains the code to be executed if the condition is false.

Expressions containing Boolean operands can only contain Boolean operands.

7) the else otherwise

The else keyword is always used in conjunction with the if keyword in if-else statements. The else clause is optional and executes if the if condition is false.

8) for loop

The for keyword is used to specify a loop that checks its conditions at the end of each iteration.

The for statement is of the form for(initialize; condition; increment)

The initialize statement is executed once the control flow enters the for statement.

The result of the condition is evaluated each time the body of the loop is executed. If condition is true, the body of the loop is executed.

After each execution of the body of the loop, the INCREMENT statement is executed before the condition of the next iteration is evaluated.

9) instanceof instance

The instanceof keyword is used to determine the class to which the object belongs.

10) switch to observe

The switch statement is used to select one of multiple code blocks to execute based on an expression.

The result of the switch condition must be equal to byte, char, short, or int.

Case blocks have no implicit end point. The break statement is usually used at the end of each case block to exit the switch statement.

If there is no break statement, the execution stream will enter all subsequent case and/or default blocks.

Case returns the result of the observation

Case marks each branch of the switch statement.

Case blocks have no implicit end point. The break statement is usually used at the end of each case block to exit the switch statement.

If there is no break statement, the execution stream will enter all subsequent case and/or default blocks.

12) the default default

The default keyword is used to mark the default branch in the switch statement.

The default block has no implicit end point. The break statement is typically used at the end of each case or default block to exit the switch statement when the block is complete.

If there is no default statement, a switch statement whose arguments do not match any case block will do nothing.

4. Error handling

1) Try to catch exceptions

The try keyword is used to contain a block of statements that might throw an exception.

Every try block must have at least one catch or finally clause.

If a particular exception class is not handled by any catch clause, the exception is propagated recursively down the call stack to the next enclosing try block. If no exception is caught by any of the enclosing try blocks, the Java interpreter exits with an error message and stack trace information.

2) Catch exceptions

The catch keyword is used to define an exception handling block ina try-catch or try-catch-finally statement.

The opening and closing tags {and} are part of the syntax of a catch clause and cannot be omitted even if the clause contains only one statement.

Every try block must have at least one catch or finally clause.

If a particular exception class is not handled by any catch clause, the exception is propagated recursively down the call stack to the next enclosing try block. If no exception is caught by any of the enclosing try blocks, the Java interpreter exits with an error message and stack trace information.

Throw throws an exception object

The throw keyword is used to throw an exception.

The throw statement takes java.lang.throwable as an argument. The Throwable propagates up the call stack until it is caught by the appropriate catch block.

Any method that throws a non-RuntimeException must also declare the exception it throws using the throws modifier in the method declaration.

Throws throws; throws throws

The throws keyword can be applied to methods to indicate that the method has thrown a particular type of exception.

Throws keyword takes as an argument a comma-separated list of java.lang.Throwables.

Any method that throws a non-RuntimeException must also declare the exception it throws using the throws modifier in the method declaration.

To include a call to a method with a throws clause in a try-catch block, the caller of the method must be provided.

5. Package

1) introduced the import

The import keyword makes one or all of the classes in a package visible in the current Java source file. You can refer to imported classes without using fully qualified class names.

When multiple packages contain classes with the same name, many Java programmers simply use specific import statements (without “*”) to avoid uncertainty.

2) package bag

The package keyword specifies the Java package in which the class declared in the Java source file resides.

The package statement (if present) must be the first non-annotated text in the Java source file.

Example: the Java. Lang. Object.

If the Java source file does not contain package statements, the classes defined in that file will be in the “default package.” Note that classes in the default package cannot be referenced from classes that are not in the default package.

6. Basic types

1) Boolean type

Boolean is a Java primitive type. Boolean variables can be true or false.

Boolean variables can only take a value of true or false. Boolean cannot be converted to and from numeric types.

Expressions containing Boolean operands can only contain Boolean operands.

A Boolean class is a wrapper object class for the Boolean primitive type.

2) byte

Byte is the Java primitive type. Byte Integer values that can be stored in the range [-128, 127].

The Byte class is a wrapper object class for the primitive type of Byte. It defines MIN_VALUE and MAX_VALUE constants that represent a range of values of this type.

All integer values in Java are 32-bit ints, unless the value is followed by l or L (such as 235L), which means that the value should be interpreted as long.

3) Char characters

Char is the Java primitive type. The char variable can store a Unicode character.

The following char constants can be used: \b – space, \ F – page feed, \ N – newline, \ R – Carriage return, \ T – horizontal TAB, \’ – single quotes, \” – double quotes, \\ – backslashes, \ XXX – Latin-1 characters encoded in XXX. \x and \xx are both legal forms but may cause confusion. \ uXXXX – Unicode character encoding XXXX in hexadecimal.

The Character class contains static methods that you can use to handle char variables, including isDigit(), isLetter(), isWhitespace(), and toUpperCase().

Char values are unsigned.

4) Double precision

Double is a Java primitive type. The double variable can store a double-precision floating-point value.

Since floating-point data types are approximations of actual values, equality comparisons of floating-point values are generally not performed.

Java floating point values can represent infinity and NaN (non-numeric). The Double wrapper object class defines constants MIN_VALUE, MAX_VALUE, NEGATIVE_INFINITY, POSITIVE_INFINITY, and NaN.

5) float float

Float is a Java primitive type. A float variable can store single-precision floating point values.

Follow these rules when using this keyword:

Floating-point literals in Java always default to double precision. To specify a single-precision literal value, add f or f after the value, such as 0.01f.

Since floating-point data types are approximations of actual values, equality comparisons of floating-point values are generally not performed.

Java floating point values can represent infinity and NaN (non-numeric). The Float wrapper object class is used to define constants MIN_VALUE, MAX_VALUE, NEGATIVE_INFINITY, POSITIVE_INFINITY, and NaN.

6) int integer

Int is a Java primitive type. The int variable can store 32-bit integer values.

The Integer class is a wrapper object class for the primitive type int. It defines MIN_VALUE and MAX_VALUE constants that represent a range of values of this type.

All integer values in Java are 32-bit ints, unless the value is followed by l or L (such as 235L), which means that the value should be interpreted as long.

7) long integer

Long is a Java primitive type. The long variable can store 64-bit signed integers.

The Long class is a wrapper object class for the primitive type of Long. It defines MIN_VALUE and MAX_VALUE constants that represent a range of values of this type.

All integer values in Java are 32-bit ints, unless the value is followed by l or L (such as 235L), which means that the value should be interpreted as long.

8) Short

Short is the Java primitive type. The short variable can store 16-bit signed integers.

The Short class is a wrapper object class for the primitive type of Short. It defines MIN_VALUE and MAX_VALUE constants that represent a range of values of this type.

All integer values in Java are 32-bit ints, unless the value is followed by l or L (such as 235L), which means that the value should be interpreted as long.

9) null empty

Null is a Java reserved word that indicates no value.

Assigning NULL to a non-original variable frees the object previously referenced by the variable.

Null cannot be assigned to primitives (byte, short, int, long, char, float, double, Boolean).

10) true true

The true keyword represents one of two valid values for a Boolean variable.

11) false false

The false keyword represents one of two valid values for a Boolean variable.

7. Variable references

1) super class

The super keyword is used to refer to the superclass of the class that uses the keyword.

Appearing as a separate statement, super means calling the constructor of the superclass.

Super. <methodName>() indicates the method that calls the superclass. This usage is only necessary if you want to call a method that was overridden in the class so that you can specify that the method in the superclass should be called.

2) this class

The this keyword is used to reference the current instance.

The this keyword can be used to refer to the current instance when the reference might be ambiguous.

3) void No return value

The void keyword indicates the null type.

Void can be used as the return type of a method to indicate that the method does not return a value.

8. Reserved words

It is important to correctly identify keywords and reserved words in the Java language. Java keywords have special meaning to the Java compiler, they are used to represent a data type, or to indicate the structure of a program, etc. Reserved words are reserved keywords for Java that, although not currently used as keywords, may be used as keywords in future updates.

Recognize Java language keywords and do not confuse them with those of other languages such as C/C ++. Const and goto are Java reserved words. All keywords are lowercase

1) goto jump

Goto preserves the keyword but does nothing. Structured programming can complete processes without goTO statements, and the use of GOTO statements tends to make programs less readable, so Java does not allow GOto jump.

2) const static

A const reserved word, which is a type modifier. Objects declared with const cannot be updated. Similar in some ways to final.

3) native land

Java is not perfect. In addition to the fact that the running speed of Java is much slower than that of traditional C++, Java cannot directly access the bottom layer of the operating system (such as system hardware, etc.), so Java uses native methods to expand the functions of Java programs.

Native method can be compared to the interface between Java program and C program, and its implementation steps are as follows:

1. Declare native() methods in Java and compile them.

2. Create a. H file using Javah.

3, write a. CPP file to implement native export method, which needs to include the. H file generated in the second step (note that it also includes the JDK with the jni.h file);

4. Compile the. CPP file in step 3 into a dynamic link library file.

5. Use the System.loadLibrary() method to load the DLL generated in step 4 in Java, and the native() method can be accessed in Java.