Java the way of the Gods (3)-keywords (top)

Source: Internet
Author: User
Tags define exception finally block stack trace

Keywords (UP) 1.final

The ① is final decorated for the base type, indicating that the modified variable is a constant and cannot be modified. A field that is both static and final indicates that it occupies only a certain amount of storage space that cannot be changed.

When ②final is used for object application, final makes the application constant. Once a reference is initialized to an object, it is no longer possible to point it to another object.

③final method: One is to lock the method to prevent the subclass from modifying its meaning, and the other is to ensure that the method behavior remains unchanged and will not be overwritten in the inheritance. All private methods in a class are implicitly specified as final.

④final parameter: For basic types of variables, this does not make any sense, because the basic type of the variable when calling the method is a value, that is, you can change the parameter in the method of the variable without affecting the call statement, but for the object variable, but it is very useful, The object variable is passed its reference when it is passed, so that your modification of the object variable in the method will also affect the object variable of the calling statement, and when you do not need to change the variable as a parameter in the method, explicitly using final declaration will prevent you from unintentionally modifying the calling method.

⑥final class: When the whole of a class is defined as final, it indicates that the class is not allowed to be inherited.

2.this and Super

① a parameter name in your method has the same name as a member of the current object, in order not to be confused, you have to explicitly use the This keyword to indicate that you want to use a member, using this. The name of the member without this is the formal parameter. Alternatively, you can refer to a method of the current object with the this. Method name, but this is not required, and you can access that method directly using the method name.

② a method of the parent class can be called with super when the method of the parent class is overridden. If the parent's method can be called by a quilt class, you can use it as you would with this, using the super. Name of the member in the parent class to invoke.

③super and this follow the appropriate parameters directly thereafter, so the meaning of this is changed. Super is used to invoke a constructor in the parent class that has the same form, and the after argument invokes the constructor that currently has the same parameters.

④this usually refers to the current object, and super usually refers to the parent class.

3.static

① typically defines a method as static in a class, meaning that an object without this class can be called directly, and the method can be directly referenced by the class name.

② static variables are similar to static methods. All such instances share this static variable, meaning that when the class is loaded, only one storage space is allocated, and all objects of this class can access this block of storage space.

Variables defined by ③static take precedence over any other non-static variables, regardless of their order of occurrence.

④static{} (also known as static code block) is used for explicit static variable initialization, which is initialized only once and when the class is first loaded.

⑤ when it comes to inheritance, the static variables of the parent class are initialized first, and then the child classes.

⑥ usually a normal class is not allowed to be declared static, only one inner class can. In this case, the static inner class can be used directly as a normal class without the need to instantiate an external class.

4.abstract

① is used in the declaration of a class to indicate that a class cannot be instantiated, but can be inherited by other classes. An abstract class can use abstract methods, and abstract methods do not need to be implemented, but they need to be implemented in subclasses. Its purpose is to provide generic information to subclasses. An abstract class can contain anything that a normal class contains, that is, classes and instance variables, and methods with any modifiers. Only abstract classes may have abstract methods. If a class that is not abstract contains an abstract method, a compilation error will occur.

Example: If one of the following is true, then one has an abstract method:

A. It explicitly declares an abstract method.

B. It inherits an abstract method from its immediate parent class.

C. A direct class's parent excuse declares or inherits one of its methods (this must therefore be abstract)

② If the user declares private,static, and the final method is ABSTARCT, a compilation error will occur. It is not possible to reset a private method because a abstarct private can never be implemented. The static method is always available, so there must be an implementation; static abstract will never be implemented. The final method cannot be reset, so there is no return to the implementation of the final abstract method

The ③abstract keyword can modify a class or method.

The ④abstract class can be extended (adding subclasses), but not directly instantiated.

The ⑤abstract method is not implemented in the class that declares it, but must be overridden in a subclass.

Example:

Public abstract class myclass{

}

Public abstract String MyMethod ();

Attention:

Classes that use the abstract method are inherently abstract classes and must be declared abstractly.

The abstract class cannot be instantiated.

Subclasses of the abstract class can be instantiated only if the subclass of the abstract class implements all the abstract methods of its superclass. This class is called a concrete class to distinguish it from the abstract class.

If the subclass of the abstract class does not implement all the abstract methods of its superclass, the subclass is also an abstract class.

The abstract keyword cannot be applied to static, private, or final methods because these methods cannot be overridden and therefore cannot be implemented in subclasses.

The final class method cannot be abstract, because the final class cannot have subclasses.

5.boolean

The value of the Boolean variable can be true or false.

Example:

Boolean valid=true;

if (valid) {

<statement>

}

Attention:

A Boolean variable can only be a value with true or FALSE. Boolean cannot convert to and from numeric types.

An expression containing a Boolean operand can contain only a Boolean operand.

The Boolean class is the wrapper object class of the Boolean primitive type

6.break

Used to exit a for, while, or do loop prematurely, or to end a case block in a switch statement.

Example

for (i=0;i<max;i++) {

if (<loop finished early>) {

Break

}

}

int Type=<some value>;

Switch (type) {

CASE1:

<statement>

Break

CASE2:

<statement>

Break

Default

<statement>

}

Note:break always exits the deepest while, for, do, or switch statements.

7.byte

Byte is the Java primitive type.

Byte can be stored in an integer value that is within the range of [-128,127].

Example:byte b=124;

Comments:

The byte class is a wrapper object class of byte primitive type. It defines the Min_value and Max_value constants that represent the range of values of this type.

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

8.case

The case is used to mark each branch in the switch statement.

Example:

int arg=<somevalue>;

Switch (ARG) {

CASE1:

<statements>

Break

CASE2:

<statements>

Break

Default

<statements>

Break

}

Comments:

The case block does not have an implicit end point. Break statements are usually used at the end of each case block to exit the switch statement.

Without a break statement, the execution stream enters all subsequent case and/or default blocks.

9.catch

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

Example:

try{

< blocks that may throw exceptions >

} catch (<java.lang.exception or subclass >e) {

< handling exception E's code >

}

try{

< blocks that may throw other exceptions >

} catch (Fooexception e) {

< handling code for Fooexception e >

}catch (Barexception e) {

< handling code for Barexception e >

}

Comments:

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.

10.char

Char is the Java primitive type.

A char variable can store a Unicode character.

Example

Char delimiter= ' a ';

Comments

You can use the following Char constants:

\b-spaces

\f-page Change

\ n-Wrap

\r-Enter

\t-Horizontal Tab

\ '-Single quote

\ "-Double quotation marks

\ "-Back slash

\xxx-uses the XXX encoded Latin-1 character. \x and \xx are legal forms, but may cause confusion.

The \uxxxx-uses hexadecimal encoded XXXX Unicode characters.

The character class contains some static methods that you can use to handle char variables, including isdigit (), Isletter (), Iswhitespace (), and toUpperCase ().

Char value is not signed.

11.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.

Example

public class rectangle{

float width;

float height;

Public Rectangle (Float w,float h) {

Width=w;

Height=h;

}

public float getwidth () {

return width;

}

public float getheight () {

return height;

}

}

12.continue

The Continue keyword is used to jump to the next iteration of the for, while, or do loop (jumping out of the loop to perform the next loop).

Example

for (i=0;i<max;i++) {

<statements>

if (<done with this iteration>) {

Continue

}

<statements>

}

Comments:

Continue always jumps to the next iteration of the deepest while, for, or do statement.

14.default

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

Example:

int Arg=<some value>;

Switch (ARG) {

CASE1:

<statements>

Break

CASE2:

<statements>

Break

Default

<statements>

Break

}

Comments:

The default block does not have an implicit end point. Break statements are typically used at the end of each case or default block to exit the switch statement when the block is completed.

If there is no default statement, the switch statement whose arguments do not match any case block will not take any action.

15.do

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

Example:

do{

<statements>

}

while (!found);

Comments:

Do loop body executes at least once.

The conditional expression must be followed by a semicolon.

16.double

Double is the Java primitive type.

A double variable can store dual-precision floating-point values.

Example:

Double ratio=.01;

Double diameter=6.15;

Double height=1.35e03; 1.35*103 or 1350.0

Double height=1e-2; 1.0*10-2 or 0.01

Comments:

Because the floating-point data type is an approximation of the actual numeric value, it is generally not a comparison of whether a floating-point value is equal.

Java floating-point values can represent infinity and Nan (non-numeric). The double wrapper object class is used to define constants Min_value, Max_value, Negative_infinity, Positive_infinity, and Nan.

17.else

The Else keyword is always used in conjunction with the IF keyword in the if-else statement. The ELSE clause is optional, and if the IF condition is false, the clause is executed.

Example:

if (condition) {

<statements>

} else {

<statements>

}

18.extends

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.

Example:

public class Rectangle extends polygon{

}

Comments:

In the example above, the rectangle class inherits all public and protected variables and methods of the Polygon class.

The rectangle class can override any non-final method of the Polygon class.

A class can only extend one other class.

19.false

The False keyword represents one of the two legal values of a Boolean variable.

Example:

Boolean iscomplete=false;

20.final

The final keyword can be applied to a class to indicate that the class cannot be extended (No child classes).

The final keyword can be applied to a method to indicate that a method in any subclass cannot be overridden.

Example:

Public final class myfinalclass{

}

public class myclass{

Public final String Myfinalmethod () {

<statements>

}

}

Comments:

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.

21.finally

The finally keyword is used to define blocks that are always executed in the try-catch-finally statement.

Finally blocks typically contain cleanup code, which resumes normal operation after partially executing a try block.

Example:

try{

< blocks that may throw exceptions >

} catch (<java.lang.exception or subclass >e) {

< handling exception E's code >

} finally {

< statements executed with or without exception >

}

Comments:

The start and end tags {and} are part of the FINALLY 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 any part of the try block is executed, the code in the finally block will be executed regardless of whether an exception occurs or if the try or catch block contains a return, continue, or break statement.

If no exception occurs, the control skips the try block and enters the finally block.

If an exception occurs during the execution of a try block, and the corresponding catch block contains a break, continue, or return statement, the control first passes through the finally block before performing a break, continue, or return.

Java the way of the Gods (3)-keywords (top)

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.