Java Keywords (summary)

Source: Internet
Author: User
Tags define exception error handling instance method stack trace throwable try catch volatile java keywords

What are the 1.Java keywords (reference URL: 1.https://www.cnblogs.com/chenglc/p/6922834.html

2.https://zhidao.baidu.com/question/89449594.html)

1.1) Access control: Private (private), protected (protected), public (common), default (sometimes referred to as friendly, which is designed for this package access, any class, interface, exception, etc. under this package can be accessed by each other) , even members of the parent class that are not decorated with protected)

This is described in more straightforward words:

Before I explain these four keywords, I want to make a simple definition of the relationship between classes, for inheriting their own class,base class can think that they are their own children, and for their own directory of classes, think is their friends.

1, public:public indicates that the data member, the member function is open to all users, all users can directly make the call;

2, private:private means private, private means that except class himself, no one can directly use, private property sacred inviolability, even children, friends, can not be used.

3, protected:protected for children, friends, is public, free to use, without any restrictions, and for other external class,protected become private.

Scope Current class same package descendant class other package

Public√√√√

Protected√√√x

Friendly√√xx

Private√xxx

Note: Default to friendly when not written

1.2) class, method, and variable modifiers

1.2.1) Abstract Declaration abstraction

The abstract keyword can modify a class or method. Modifying a class would make this class an abstract class that would not generate an object instance, as a type of object variable declaration, that is, a compile-time type, and an abstract class that, like a class of semi-finished products, requires subclasses to inherit and overwrite the abstract methods. The abstract modification method, which makes this method an abstraction, declares (defines) without implementation, requires subclass inheritance implementations (Overrides). The abstract modification method is to require its subclasses to overwrite (implement) this method. Calls can be called in a polymorphic way after a subclass overrides (implements) the method, which means that the abstract method must be implemented in its subclasses, unless the subclass itself is an abstract class. The abstract modifier must be placed before the class name when decorating the class. Classes that use the abstract method are inherently abstract classes and must be declared abstractly.

1.2.2) 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 program design method. A class typically represents some kind of actual entity, such as a geometry or a person. A class is a template for an object. Each object is an instance of the class. To use a class, you typically instantiate an object of the class with the new operator, and then call the class's methods to access the functionality of the class.

1.2.3) extends inheritance, extension

Extends is inherited from the parent class, as long as the class is not declared final or the class is defined as abstract can inherit, Java does not support multiple inheritance, but can be implemented with interfaces, so it is necessary to use the implements, inheritance can inherit only one class, But implements can implement multiple interfaces, separated by commas on the line. The extends keyword is used in a class or interface declaration to indicate that the class or interface being declared is a subclass of the class or interface with the extends keyword followed by its name. The subclass inherits the public and protected variables and methods of the parent class. Subclasses can override any non-final method of the parent class. A class can only extend one other class.

1.2.4) Final, non-changing

The final keyword can be applied to a class, but the final decorated class cannot be inherited. The final keyword can be applied to a method to indicate that this method cannot be overridden in a subclass. A class cannot be both abstract and final. Abstract means that the class must be extended, and final means that the class cannot be extended. A method cannot be both abstract and final. Abstract means that the method must be overridden, and final means that the method cannot be overridden. Final can also modify variables, the final modified variable is a constant, can only be assigned once

1.2.5) implements implementation

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

1.2.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 these interfaces. Any class that implements an interface must provide an implementation of all methods in that interface. A class can implement multiple interfaces.

1.2.7) Native Local

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

1.2.8) New Creation

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

The argument after the new keyword must be a class name, and the class name must be followed by a set of constructor method parameters (which must be enclosed in parentheses). The parameter collection must match the signature of the constructor method of the class.

= The type of the variable on the left must have an assignment compatibility relationship with the class or interface to be instantiated.

1.2.9) static (reference URL: https://www.cnblogs.com/dolphin0520/p/3799052.html)

1.2.10) STRICTFP Strict, precise

STRICTFP means fp-strict, that is to say, the meaning of the precise floating point. When a Java virtual machine makes a floating-point operation, if the STRICTFP keyword is not specified, the Java compiler and the runtime's expression on floating-point operations take an approximation of their own behavior to accomplish these operations, so that the resulting results are often unsatisfactory. Once STRICTFP is used to declare a class, interface, or method, the compiler and the runtime environment for the declared scope of Java are executed exactly as the floating-point specification IEEE-754. So if you want to make floating-point arithmetic more accurate and not inconsistent with the results of different hardware platforms, then use the keyword STRICTFP.

You can declare a class, interface, and method as STRICTFP, but do not allow the STRICTFP keyword to be declared on methods and constructors in an interface

1.2.11) synchronized thread, sync (reference URL: http://www.importnew.com/21866.html)

The Synchronized keyword can be applied to a method or a block of statements and provides protection for critical code snippets that should only be executed by one thread at a time.

The Synchronized keyword prevents key code snippets from being executed by multiple threads at once.

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

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

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.

Synchronized is a key word in Java and is a synchronous lock. It modifies objects in the following ways:

1. Modify a code block, the modified code block is called the synchronous statement block, its scope is the curly braces {} In the code, the object is to call the object of this block of code;

2. Modify a method, the modified method is called the synchronous method, its scope is the whole method, the object is to call the object of this method;

3. Modify a static method whose scope of action is the entire static method, and the object of the action is all objects of this class;

4. Modify a class whose scope of action is synchronized the part enclosed in parentheses, and the object of the principal is all objects of the class.

1.2.12) Transient 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 that contains it is serialized.

When an object is serialized, the value of the transient variable is not included in the serialized representation, whereas the non-transient variable is included.

Java's serialization provides a mechanism for persisting object instances. When persisting an object, there may be a special object data member, and we do not want to use the serialization mechanism to save it. In order to turn off serialization on a domain of a particular object, you can precede the field with the keyword transient.

Transient is a keyword in the Java language that is used to indicate that a domain is not part of the serialization of that object. When an object is serialized, the value of the transient variable is not included in the serialized representation, whereas the non-transient variable is included.

1.2.13) volatile and volatile

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

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

Volatile variables in the Java language can be thought of as a "light synchronized", and volatile variables require less coding and run-time overhead than synchronized blocks, but the functionality they can achieve is only Part of the synchronized.

1.2) Program Control statement: slightly

1.3) Error Handling

1.3.1) Try catch exception

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

Each try block must have at least one catch or finally clause. If a particular exception class is not handled by any catch clauses, the exception is recursively propagated along the call stack to the next enclosing try block. If none of the enclosing try blocks catch an exception, the Java interpreter exits and displays the error message and stack trace information.

1.3.2) Catch Handling exception

The catch keyword is used to define exception handling blocks in a try-catch or try-catch-finally statement.

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

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

If a particular exception class is not handled by any catch clauses, the exception is recursively propagated along the call stack to the next enclosing try block. If none of the enclosing try blocks catch an exception, the Java interpreter exits and displays the error message and stack trace information.

1.3.3) Throw throws an exception object

The Throw keyword is used to throw an exception.

The throw statement takes java.lang.Throwable as a parameter. The throwable propagates up in the call stack until it is captured by the appropriate catch block.

Any method that throws a non-runtimeexception exception must also use the throws modifier in the method declaration to declare the exception it throws.

1.3.4) throws declares an exception that may be thrown

The throws keyword can be applied to a method to indicate that the method throws a specific type of exception.

The throws keyword takes a comma-delimited list of java.lang.Throwables as arguments.

Any method that throws a non-runtimeexception exception must also use the throws modifier in the method declaration to declare the exception it throws.

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

Java Keywords (summary)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.