Java syntax is based on the concept
Note
Single-line comments
// line comment Description
Multi-line comments
/ * Multi-line comments Description * /
Documentation Comments
/ ** * @author author of the program * @version version of the source file * @param parameter method of descriptive information * Document Explanatory Notes * /
Keyword
category |
Keyword |
Explanation |
Access control |
private |
private |
protected |
under protection |
|
public |
public |
|
Class, method and variable modifiers |
abstract |
Abstract statement |
class |
class |
|
extends |
Expansion, succession |
|
final |
The final value can not be changed |
|
implements |
Implement (Interface) |
|
interface |
interface |
|
native |
Local, native method (non-Java implementation) |
|
new |
New, created |
|
static |
Static state |
|
strictfp |
Strict, precise |
|
synchronized |
Threads, synchronization |
|
transient |
short |
|
volatile |
Volatile |
|
Program control statements |
break |
Out of the loop |
case |
Define a value for switch selection |
|
continue |
carry on |
|
default |
default |
|
do |
run |
|
else |
otherwise |
|
for |
cycle |
|
if |
in case |
|
instanceof |
Examples |
|
return |
return |
|
switch |
Execution based on the value selected |
|
while |
cycle |
|
Error Handling |
assert |
Whether the assertion expression is true |
catch |
Catch an Exception |
|
finally |
There are no exceptions to perform |
|
throw |
Throw an exception object |
|
throws |
Declare an exception might be thrown |
|
try |
Catch the exception |
|
Package Dependencies |
import |
Introduced |
package |
package |
|
basic type |
boolean |
Boolean |
byte |
Byte |
|
char |
Character |
|
double |
Double-precision floating point |
|
float |
Single-precision floating point |
|
int |
Integer |
|
long |
Long integer |
|
short |
Short integer |
|
Variable reference |
super |
The parent class, the superclass |
this |
This class |
|
void |
No return value |
|
Reserved Keywords |
goto |
Keyword, but you can not use |
const |
Keyword, but you can not use |
|
null |
air |
Identifier
In Java, variable names, class names, members of the variable names, method names are identifiers (Identifier) . Identifier made up of letters, numbers, underscores, dollar symbols and can not start with a number.
Constant name: All capital letters and underlined many words when connected; Package name: multi-word when all letters are lowercase;
The first letter of all words capitalized multi-word; variable names, method names:: class name, interface name multi-word, lowercase first letter of the first word capitalized other words;
Escape character
symbol |
Character Meaning |
\n |
Line feed (0x0a) |
\r |
Carriage return (0x0d) |
\f |
Formfeed (0x0C) |
\b |
Backspace (0x08) |
\0 |
Null character (0x20) |
\s |
String |
\t |
Tabs |
\" |
Double quotes |
\' |
apostrophe |
\\ |
Backslash |
\ddd |
Octal character (ddd) |
\uxxxx |
Unicode hexadecimal characters (xxxx) |
Modifiers
Modifiers used to define classes, methods, or variables, typically on the most distal end statement.
Access control modifiers : default, public, protected, private
Java can be used to control access to secure access to character classes, variables, methods, and constructors.
default (i.e., default): visible, without any modifiers in the same package. Using objects: classes, interfaces, variables, methods; Private: visible inside the same class. Use objects: variables and methods. Note: You can not modify classes (external class); public: visible to all classes. Using objects: classes, interfaces, variables, methods; protected: visible to all classes and subclasses in the same package. Use objects: variables and methods. Note: You can not modify the class (outside class).
Access control |
|||||
Modifiers |
The current class |
The same intracapsular |
Descendant class ( same package ) |
Descendant class ( in different packages ) |
Other packages |
public |
Y |
Y |
Y |
Y |
Y |
protected |
Y |
Y |
Y |
N |
|
default |
Y |
Y |
Y |
N |
N |
private |
Y |
N |
N |
N |
N |
Non-access control modifiers : final, abstract, static, synchronized
final modifier for modifying classes, variables and methods, a modified final class can not be inherited, a modified method of the class can not be inherited redefined, the modified variables constant, can not be modified. final class method final method can be inherited by subclasses, but can not modify the subclasses, the main purpose of the method is to prevent the final declaration of this method is modified. final Class final class can not be inherited, no class can inherit any properties of final class.
abstract modifier
Abstract class: abstract class can not be used to instantiate objects, the sole purpose of the statement of an abstract class is the class for future expansion. A class can not be modified abstract and final. If a class contains abstract methods, the class must be declared as an abstract class, otherwise the compilation error will occur. An abstract class may contain a non-abstract methods and abstract methods.
abstract class Caravan{
private double price;
private String model;
private String year;
public abstract void goFast (); // abstract method
public abstract void changeColor();
}
Abstract no method is a method of implementation, the specific implementation of the method provided by subclasses. Abstract methods can not be declared as final or static. Any subclass inherits all the abstract class abstract methods of the parent class must implement, unless the subclass is also abstract class. If a class contains a number of abstract methods, the class must be declared as an abstract class. An abstract class may not contain abstract methods. Abstract method declaration ends with a semicolon.
public abstract class SuperClass{
abstract void m (); // abstract method
}
class SubClass extends SuperClass{
// implement the abstract method
void m(){
.........
}
}
static modifier
Static variables: static keyword is used to declare static variables independent of the object, no matter how many objects a class is instantiated, it is only one copy of a static variable. Static variables also known as class variables. Local variables can not be declared as static variables.
Static method: static keyword is used static method is independent of the object declaration. Static methods can not use non-static variables class. Static method to get the data from the parameter list, the data is then calculated.
Access class variables and methods can be used directly classname.variablename and classname.methodname access manner.
package com.ahabest.demo;
public class InstanceCounter {
private static int numInstances = 0;
protected static int getCount() {
return numInstances;
}
private static void addInstance() {
numInstances++;
}
InstanceCounter () {
InstanceCounter.addInstance();
}
public static void main(String[] arguments) {
System.out.println("Starting with " +
InstanceCounter.getCount() + " instances");
for (int i = 0; i < 500; ++i){
new InstanceCounter();
}
System.out.println("Created " +
InstanceCounter.getCount() + " instances");
}
}
synchronized modifier
The method of synchronized keyword to declare the same time can only be one thread access. synchronized modifier can be applied to four access modifiers.
public synchronized void showDetails(){
.......
}
The volatile modifier
volatile modified member variable each time it is accessed threads are forced to re-read the value of the member variable from shared memory. Also, when members of the variable changes, a thread is forced to change the value written back to the shared memory. So that at any moment, two different threads always see the same value of a member variable. A volatile object reference may be null.
public class MyRunnable implements Runnable
{
private volatile boolean active;
public void run()
{
active = true;
while (active) // first line
{Code} //
}
public void stop()
{
active = false; // second row
}
}
Under normal circumstances, a thread calls the run () method (in the open Runnable threads), in another thread calls stop () method. If the value in the first row in the active buffer is used, the loop does not stop at the second row active is false. But the above code we use volatile modification active, so the cycle will stop.
transient modifier
When serialized object contains instance variables is transient modification, java virtual machine (JVM) to skip that particular variable. The modifier is included in the statement defines variables, and classes for pre-processing the data type of the variable.
public transient int limit = 55; // will not persist
public int b; // persistence
Reproduced in: https: //www.cnblogs.com/Aha-Best/p/10873428.html