50 keywords in Java

Source: Internet
Author: User
Tags define abstract goto modifiers

50 keywords in Java

Keywords, also known as reserved words, refer to identifiers that specify specific meanings in the Java language. For reserved words, the user can only be used in the manner prescribed by the system and cannot be defined by itself. There are 50 common keywords in Java:

Related to data type (10)

Key words Type Number of bytes Note
Boolean Boolean Data types 1 Has a value of true and false two
Int Integer data type 4
Long Long-integer data types 8
Short Short-Integer data types 2
Byte Byte data type 1 Size range is -27-27-1
Float Floating point type (real type) 4
Double Double type data type 8
Char Character type data type 2 (Unicode code)
Class Represents a class Not sure Can be used to define a class.
Interface Represents an interface Not sure Can be used to define an interface
Related to Process Control (13)

if: indicates conditional judgment, general usage if (relational expression), followed by else or {...}

Else: conditional jog, such as if (relational expression) {statement block 1}else{statement Block 2}, if the value of the relational expression is true, the statement block 1 is executed, otherwise the statement block 2 is executed.

Do......while ... :do and while are typically used together to represent loop statements. Do{...} while (relational expression) ... When the value of the relational expression is true, the loop continues .

For : used to represent loops, for loops is the most commonly used loop, format for (expression A; Expression B; Expression c) The book inside the parentheses is usually used to control the number of cycles, typically with a variable class of type int, such as (int i=0; i<10; i++) expression A is used for the start value of the process control, and expression B represents a cyclic termination condition, and the expression C is used for counting.

Switch (condition a)case ... :Switch and case are combined to represent the conditional branching process. such as:
while (int c) {
Case 1: {statement block 1}
Case 2: {statement block 2}
......?????? ......
Case N: {statement block n}
Default:exit (0);
}

If C is 1, the statement block 1 is executed, if C is 2, the statement block 2 is executed, and so on, if C is N, the statement block N is executed. Default indicates what happens except case.

default: In Switch......case ... The branch statement indicates that default is used when all case conditions are not true. Used for packages that represent the current package under the S "RC" folder, and if used for classes, it can only be accessed by other classes within this file.

Break : used to end this layer loop or jump out of a layer loop.

continue: used to jump out of this loop, and break jumps out of this layer loop. Break and continue can be implemented similar to the use of goto statements in c\c++:
Label0:
{
for (int k = 0; k < k++) {
..........
Label1:
for (int j = 0; J <; J + +) {
................
Break label0; //Jump to Label0
}
...........
Label2:
for (int kk = 0; KK < kk++) {
..............
Break label0; //Jump to Label2

}
}

Return : Returns a value, typically used in a function, that returns a value of a specific type. Such as:

public int fuction ()
{
int A;
......
return A; //Returns the value of type int
}

try......catch......finally ... : for exception handling, using the form such as:

try{
......
}catch (Type B) {
......
}finally{
......
}

Try{...} A block of statements in which an exception may occur is placed, catch () {...} Used to catch exceptions, {} to define how to handle when an exception occurs. Finally{...} Indicates that the processing in finally{} is performed regardless of whether the exception occurred.

Related to modifiers (12) Access permissions for different access modifiers
Range This class Same package Different packet neutron class Different packages and no inheritance Effect (meaning)
Public OK OK OK OK Public-owned
Protect OK OK OK
The protected
Default (no permission modifier) OK OK

The current
Private OK


Privately owned

Such as:

A ". Java" suffix name in the file can only be a publi type of Cass class. If there is a Helloworld.java file under the C13.swing package, the following code is in the file: Packages C13.swing;public class HelloWorld {public void show () { System.out.println ("helloworld!");}} Under the default package, there is the following code: Import c13.swing.*;//introduces all the classes in the required package public class Test {public static void main (String args[]) {HelloWorld h = new HelloWorld (); H.show ();}} At this point, the result: helloworld! If you change the public class HelloWorld to Privateclass HelloWorld it will run an error, because private is proprietary and cannot be referenced by objects within other packages.

final: ( similar to const in C + +), use the following:
The value of the a,final variable cannot be changed; 1), final member variable; 2) final local variable;
The B,final method cannot be rewritten;
The C,final class cannot be inherited.

void: null type, used to represent null return value before function.

Static : can be used to define static member variables or methods

strictfp: meaning fp-strict, that is to say, the meaning of 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 do so in an approximate way, so that the resulting results do not always satisfy you. 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 your floating-point arithmetic to be 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 the interface, such as the following code:

1. Legal use of keyword STRICTFP
STRICTFP interface A {}

Public STRICTFP class FpDemo1 {
STRICTFP void F () {}
}
2. How to use the error
Interface A {
STRICTFP void f ();
}

public class FpDemo2 {
STRICTFP FpDemo2 () {}
}

Once the keyword STRICTFP is used to declare a class, interface, or method, all floating-point operations within the scope declared by this keyword are accurate and conform to the IEEE-754 specification. For example, if a class is declared as STRICTFP, then all methods in the class are STRICTFP.

Abstract : abstract meaning, can be used to define abstract classes, abstract functions. The usage rules for abstract classes are as follows:
A, when you decorate a class with the abstract keyword, this class is called an abstract class; When you use abstract to modify a method, the method is called an abstract method.
b, classes containing abstract methods must be declared as abstract classes, abstract classes must be inherited, and abstract methods must be rewritten;
C, abstract classes cannot be instantiated;
D, abstract methods only need to be declared, but not implemented.

Transient: The Java language keyword 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.
Working with objects: Fields
Description: A field is not part of an object's persistence and should not be strung together with fields and objects.

synchronized: locks the current object and does not promise other threads to break the insert while executing the current object. The methods used are:

This is the note I studied during the summer vacation.

(1), class Timer {
private static int num = 0;
Public synchronized void Add (String name) {//Locks the current object during execution of this method.
num + +;
try {
Thread.Sleep (1);
} catch (Interruptedexception e) {
Return
}
SYSTEM.OUT.PRINTLN (name + ", you are the first" + num + "thread to use the timer.) ");
}
}
(2), class Timer {
private static int num = 0;
public void Add (String name) {
Synchronized (this) {//Lock curly braces This area of code snippet
num + +;
try {
Thread.Sleep (1);
} catch (Interruptedexception e) {
Return
}
SYSTEM.OUT.PRINTLN (name + ", you are the first" + num + "thread to use the timer.) ");
}
}
}

volatile :Volatile variables can only be accessed by one thread at a time. ensure thread safety.
These valid values that can be written to a volatile variable are independent of the state of any program, including the current state of the variable.
Therefore, the Volatile variable is a very simple, but also very fragile synchronization mechanism, which in some cases will provide better performance and scalability than the lock.

native : The implementation of a native function that is used as a collaboration between Java and other languages (such as C + +) is not written in Java, and native means to notify the operating system that you must give me the implementation because I want to use it. So the function of the native keyword is implemented by the operating system, and Java can only be called.

Related to Motion (10)

Package : Pack, write Java programs in a package.

Import: Introduction to registration, usage:
A, write all-inclusive name: Jsxt.java140.Cat c = new Com.bjsxt.java140.Cat
A, introduce a specific object in a package: Xt.java140.Cat;
b, introduce all the images in a package: import com.bjsxt.java140.cat.*;

Throw : throws the user's own defined exception

throws : throws a System exception

extends: Inheritance (class-inheriting class, or interface-inheriting interface)

implements: Implementing, implementing interfaces through classes

This : is the identifier that points to the object itself

Super: Call the constructor of the base class

instanceof: Determine whether an object is the object you want

New : used to create a new object, that is, the instantiation of the class

Other (5)

true : represents a Boolean type of truth

false: represents a Boolean type of false value, that is, a condition that is not true

null: represents a null value, or an object that indicates that this object is empty, and if it is a data type, the default value for the corresponding data type, such as int, is 0.

Goto: The unconditional turn statement is implemented in C\c++, which is now generally not used in Java for structured programming.

Const: As with const, in Java, Const is used as a reserved word for expansion. You can replace the const with final, in general C + + in Const,java with final.


This article is from "Ghost" blog, please make sure to keep this source http://caizi.blog.51cto.com/5234706/1548284

50 keywords in Java

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.