Java those little-known reserved words

Source: Internet
Author: User

one: 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 for java.

Its purpose is to test whether the object on its left is an instance of the class to the right of it, returning a Boolean type of data.

(2) Example: Instanceof has some use. For example, we wrote a system that handles bills, and there are three of these:

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 an object of type bill and calculates the amount. Suppose the two billing methods are different, and the incoming Bill object may be any of the two, so use instanceof to determine:
Public double Calculate (Bill instanceof PhoneBill)//Calculate phone bill} if (Bill instanceof Gasbill) {//Calculate gas  Bill} ...} This allows the two seed class to be processed in one way.

However, this practice is often thought of as not taking good advantage of the polymorphism in object-oriented. In fact, the above function requires a method overload can be fully implemented, this is an object-oriented approach should be done to avoid back to the structured programming mode. As long as the two names and return values are the same, a different method of accepting the parameter types is available:

Public double Calculate (PhoneBill Bill) {//Calculate phone bill} public double calculate (Gasbill Bill) {//Calculate gas bill}

(3) Therefore, the use of instanceof in most cases is not recommended practice, should make good use of polymorphism, of course, for the sake of simplicity is sometimes possible.

II:java volatile keyword

(1) like a const that you are more familiar with, volatile is a type modifier (type specifier). It is designed to modify variables that are accessed and modified by different threads.

Without volatile, it would basically result in the inability to write multithreaded programs or the compiler losing a lot of optimization opportunities.

(2) A variable defined as volatile means that the variable may be unexpectedly changed so that the compiler does not assume the value of the variable. Precisely, the optimizer must carefully re-read the value of the variable each time it uses the variable, rather than using the 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 variables that are accessed in an interrupt service subroutine (non-automatic variables)
3). Variables shared by several tasks in multi-threaded applications
This is the most 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 volatile variables to be used. Not knowing volatile content will bring disaster. (not embedded can not see)

(3) A volatile variable is a very simple, yet very fragile synchronization mechanism compared to a lock, which in some cases provides better performance and scalability than a lock. If you strictly follow the use of volatile conditions-that is, the variable is truly independent of the other variables and its own previous values- in some cases you can use volatile instead of synchronized to simplify the code. However, code that uses volatile is often more error-prone than code that uses locks. the pattern described in this article covers some of the most common use cases where volatile can be used instead of synchronized. Following these patterns (not exceeding their limits when using them) can help you implement most use cases safely and use volatile variables for better performance.

III: java synchronized keyword

(1)   synchronized  keyword , on behalf of this method lock, the equivalent of whichever thread (for example, thread a), run to this method, to check if there is no other thread B (or C, D, etc.) is using this method, Some words wait for thread B (or C, D) that is using the Synchronized method to run this thread a after running this method, and then run it directly. It consists of two usages: Synchronized method and synchronized block, at most one thread at a time executes this piece of code.

(2) Synchronized method
1. When the method declaration is used, it is placed before the scope operator (public, etc.), before the return type declaration (void, etc.). At this point, the thread obtains a member lock, that is, only one thread can enter the method at a time, and the other thread wants to call the method at this time, only queued, the current thread ( Is the thread inside the Synchronized method) after the method is executed, other threads can enter. For example:

Public synchronized void Synmethod () {//Method body}2. Used for a block of code, synchronized followed by parentheses, and in parentheses is a variable so that only one thread at a time enters that block of code. At this point, the thread obtains a member lock. For example: public int Synmethod (int a1) {synchronized (A1) {//Once only one thread enters}}
Four:java synchronized keyword

< Span style= "line-height:24px" > (1) Super class : In Java terminology, the inherited class is called a superclass, and the inherited class is called a subclass, so use super. One of the most important features of
object-oriented programming is the ability to use the methods and domains of previously created classes.
using simple classes to create powerful classes can significantly save programming time and, more importantly, to reduce the chance of code errors, and to construct a new class on a previous class, you must extend the class in the class declaration.
By extending a superclass, you can get a new copy of the class, and you can add additional functionality to it. If you do not add any work to the new class, it works exactly like a superclass, with all the methods and domains that the superclass declares and inherits, with access to the

(2) For superclass, the use of this and super keyword should be understood exactly, demonstrating the use of these two keywords.

Import java.awt.*;p ublic class Testthissuper extends Frame{<span style= "White-space:pre" ></span>int B; Public Testthissuper (String a) {<span style= "White-space:pre" ></span>this (a,0);} Public Testthissuper (String a,int b) {<span style= "White-space:pre" ></span>super (a); <span style= " White-space:pre "></span>this.b= b;}}

V: strictfp, i.e. strict float point (exact float)

(1) The STRICTFP keyword can be applied to a class, interface, or method. When you declare a method with the STRICTFP keyword, all of the float and double expressions in the method adhere strictly to the fp-strict limit and conform to the

IEEE-754 specification. When using the STRICTFP keyword on a class or interface, all code in the class, including the initial setpoint and code in the nested type, is evaluated Strictly. Strict restraint means

The result of all expressions must be the expected result of the IEEE 754 algorithm on the operand, expressed in single-precision and double-precision formats. If you want to make your floating-point arithmetic more accurate and not inconsistent with the results of different hardware platforms, 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 Mycla SS2 () {}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.6710339double:0.04150553411984792sum:0.71253945297742238   
VI: Java provides a mechanism called serialization

(1) Persist a Java object with an ordered format or sequence of bytes that contains the object's data, the type of the object, and the type of data saved in the object. So, if we have serialized an object, it can be read and deserialized through the object's type and other information, and eventually get the prototype of the object.
(2) ObjectInputStream and ObjectOutputStream objects are high-level stream objects that contain methods for serialization and deserialization.
ObjectOutputStream has many methods for serializing objects, most commonly:
(3) Where is serialization required? Serialization is typically used where data is transferred over the network, or when the object is saved to a file. The data that is said here is the object, not the text.
The problem now is that both our network architecture and the hard disk recognize only binary and byte, not Java objects.
Serialization is the translation of value/states in Java objects into bytes to be transmitted over the network or saved. In addition, deserialization is done by reading the bytecode and translating it back to the Java object


Java those little-known reserved words

Related Article

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.