One: The instanceof keyword in Java
(1) Definition: instanceof is a Java two-dollar operator, and ==,>,< is the same kind of stuff. Because it is made up of letters, it is also a reserved keyword in java.
its role is to test whether the object on its left is an instance of its right class, and returns a Boolean type of data. (2) Example: instanceof some useful. For example, we wrote a billing system with three classes:
public class Bill {//omit details}
public class Phonebill extends Bill {//omit details}
public class Gasbill extends Bill {//omit details}
There is a method in the handler that accepts a bill type object and calculates the amount. Suppose the two bills are calculated differently, and the incoming Bill object may be either of two, so use instanceof to determine:
Public double Calculate (Bill Bill) {
if (Bill instanceof Phonebill)
//Calculate phone bill
}
if (Bill instanceof Gasbill) {
//Calculate gas bill
}
...
This allows you to handle two of seed classes in one way.
However, this approach is often considered to be a failure to take advantage of the object-oriented polymorphism. In fact, the above features require method overload can be fully implemented, this is an object-oriented approach should be to avoid the return to structured programming mode. As long as two names and return values are the same, methods that accept different parameter types are available:
Public double Calculate (Phonebill Bill) {
//Calculate phone Bill
} public
double calculate (Gasbill Bill) {
//calculation burn Gas Bill
}
(3) Therefore, the use of instanceof in most cases is not recommended practice, should make good use of polymorphism, of course, in order to simple sometimes is also possible.
Two: Java volatile keywords
(1) just like the more familiar const, volatile is a type modifier (kind specifier). It is designed to modify variables that are accessed and modified by different threads.
If there is no volatile, it will basically lead to the result that either the multithreaded program cannot be written, or the compiler loses the opportunity for a lot of optimizations.
(2) A variable defined as volatile means that the variable may be unexpectedly altered so that the compiler does not assume the value of the variable. Precisely, the optimizer must carefully reread the value of the variable every time it uses it, instead of using a backup stored in the register. Here are a few examples of volatile variables:
1. Hardware registers for parallel devices (e.g., status registers)
2). Non-automatic variable (non-automatic variables) that is accessed in an interrupt service subroutine
3. Variables shared by several tasks in multi-threaded applications
This is the basic problem of distinguishing between C programmers and embedded system programmers : Embedded system programmers often deal with hardware, interrupts, RTOs, and so on, all of which require the use of volatile variables. Not knowing volatile content will bring disaster. (non-embedded can not be looked at)
(3) compared to locks, 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. If you strictly follow the volatile conditions-that is, the variables are truly independent of other variables and their previous values- you can use volatile instead of synchronized in some cases to simplify the code. However, code that uses volatile is often more error-prone than code that uses locks. the patterns described in this article cover some of the most common use cases where you can use volatile instead of synchronized. Following these patterns (note that you do not exceed their limits when you use them) can help you achieve most of the use cases securely, using volatile variables for better performance.
Three: Java synchronized keywords
(1) The synchronized keyword , which represents this method of locking, is equivalent to whichever thread (for example, thread a), when running to this method, check that there are other threads B (or C, D, etc.) using this method, You have to wait. Thread B (or C, D) that is using the Synchronized method runs this method before running this thread A, without the words, running directly. It includes two uses: the Synchronized method and the synchronized block, and at the same time only one thread executes this segment of code.
(2) Synchronized method
1. When using the method declaration, after the range operator (public, and so on) is returned, before the type declaration (void, and so on) is received. At this point, the thread obtains the member lock, that is, only one thread can enter the method at a time, and other threads can only wait in line to call the method at this time It is the thread within the synchronized method that executes the method, and the other thread can enter. For example:
Public synchronized void Synmethod () {
//method Body
}
2. Used for a block of code, synchronized followed by parentheses, a variable in parentheses, so that only one thread at a time entered the code block. , the thread gets the member lock. For example: public
int Synmethod (int a1) {
synchronized (A1) {
//Only one thread can enter} at a time
}
Four: Java Super keyword
(1) superclass: In Java terms, the inherited class is called a superclass, and the inherited class is called a subclass, so the super is used.
One of the most important features of object-oriented programming is the ability to use the methods and domains of previously created classes. creating powerful classes with simple classes can save programming time significantly and, more importantly, reduce the chance of code errors; to construct a new class on a previous class, you must extend the class in the class declaration.
By extending a superclass, you get a new copy of the class, and you can add additional functionality to it. If you don't add any work to this new class, it works exactly like a superclass, and the new class contains all the methods and fields that the superclass declares and inherits that have access rights
(2) For super classes, you should understand the use of this and super keywords exactly, and the following illustration shows the use of these two keywords.
Import java.awt.*;
public class Testthissuper extends Frame
{
int b;
Public Testthissuper (String a)
{this
(a,0);
}
Public Testthissuper (String a,int b)
{
super (a);
this.b= b;
}
}
V: strictfp, i.e. strict float point (exact floating-point)
(1) The STRICTFP keyword can be applied to a class, interface, or method. When you declare a method by using the STRICTFP keyword, all float and double expressions in the method strictly adhere to the fp-strict limit, in accordance with the
IEEE-754 specifications. When you use the STRICTFP keyword on a class or interface, all the code in that class, including the initial values and code in the nested type, is strictly evaluated. Strict constraints mean
The results of all expressions must be those expected by the IEEE 754 algorithm on the operands, expressed in single and double precision formats. if you want to make your floating-point operations more accurate, and not because the different hardware platform to perform inconsistent results, you can use the keyword STRICTFP.
(2) Example
The following example shows a method that is declared using the STRICTFP modifier.
//Example of precision control with STRICTFP: Public
class MyClass2
{public
float afloat;
public double adouble;
Public MyClass2 () {} public
strictfp double Add (float A, double b)
{return
(A + b);
}
public static void Main (string[] args)
{
MyClass2 myClass2 = new MyClass2 ();
Myclass2.afloat = 0.6710339f;
myclass2.adouble = 0.04150553411984792d;
Double sum = Myclass2.add (myclass2.afloat, myclass2.adouble);
System.out.println ("float:" + myclass2.afloat);
System.out.println ("Double:" + myclass2.adouble);
System.out.println ("sum:" + sum);
}
Sample output
float:0.6710339
double:0.04150553411984792
sum:0.71253945297742238
VI: Java provides a mechanism called serialization
(1) The Java object is persisted through an ordered format or byte sequence, containing the object's data, the object's type, and the data type saved in the object. So, if we have serialized an object, it can be read and deserialized by the type and other information of the object, and eventually the object's prototype is obtained.
(2) ObjectInputStream and ObjectOutputStream objects are high-level stream objects that contain methods for serialization and deserialization.
ObjectOutputStream has a number of methods for serializing objects, most commonly:
(3) So where is serialization needed? Serialization is usually used when you need to transfer data over a network, or to save objects to a file. The data here is the object, not the text.
The problem now is that our network architecture and hard disk can only recognize binaries and bytes, but not Java objects.
Serialization is the translation of value/states in Java objects into bytes to be transmitted or saved over the network. In addition, deserialization is done by reading the bytecode and translating it back into the Java object