Java Advanced Tutorial (i) keyword __java

Source: Internet
Author: User
Tags assert serialization
Java Advanced Tutorial (i) Key Words

Synchronized: When it is used to modify a method or a block of code, can ensure that at the same time at most only one thread to execute the code.

Instanceof: Indicates at run time whether an object is an instance of a particular class.
Result = Object Instanceof class

strictfp:strict float point (precise floating-point) to ensure that the floating-point results are consistent after the platform migration.

Transient: If you declare an instance variable with transient, its value does not need to be maintained when the object is stored. The member variables marked with the Transient keyword do not participate in the serialization process.

Assert: Assertions can prove the correctness of the program.
Assert expression1
Assert Expression1:expression2
Expression1 represents a Boolean expression, Expression2 represents a base type, an expression, or an object that outputs an error message when it fails.
In eclipse, you must open the relevant settings. Specifically in window->preferences->java->installed JREs, select the relevant version, click Edit, and enter-ea in the default VM arguments.

Import Java.io.FileInputStream;
Import Java.io.FileOutputStream;
Import Java.io.ObjectInputStream;
Import Java.io.ObjectOutputStream;

Import java.io.Serializable;
        public class Demo {public static void main (string[] args) {People people = new people ();
            try {//serialization ObjectOutputStream Oos = new ObjectOutputStream (New FileOutputStream ("People.dat"));
            Oos.writeobject (people);
            Oos.close ();
            Deserialization ObjectInputStream ois = new ObjectInputStream (New FileInputStream ("People.dat"));
            People = (people) Ois.readobject ();
        Ois.close ();
        catch (Exception e) {e.printstacktrace ();
        }//thread Myrunner boy = new Myrunner (People, "GG", "Boy");
        Myrunner girl = new Myrunner (People, "MM", "Girl");
        Thread thread1 = new Thread (Boy, "boy");
        Thread thread2 = new Thread (Girl, "girl");
        Thread1.start ();
 Thread2.start ();       Assertion Boolean ispeople = people instanceof people;
    Assert!ispeople: "It ' s wrong";
    Class people implements serializable{private String name;
    Private String sex;

    Private transient String school = "FJNU";
        Public synchronized void set (String name,string sex) {this.name = name;
    This.sex = sex;
    public void print () {System.out.println ("name=" +name+, sex= "+sex+", school= "+school");
    } class Myrunner implements runnable{private people people;
    private String name;

    Private String sex;
        Public Myrunner (People people,string name,string sex) {this.people = people;
        THIS.name = name;
    This.sex = sex;
            @Override public void Run () {for (int i=0;i<10;i++) {people.set (name, sex);
        People.print (); }
    }

}
The results of the operation are as follows:
Exception in thread "main" name=mm,sex=girl,school=null
Name=mm,sex=girl,school=null
Name=mm,sex=girl,school=null
Name=gg,sex=boy,school=null
Name=mm,sex=girl,school=null
Name=mm,sex=girl,school=null
Name=mm,sex=girl,school=null
Name=mm,sex=girl,school=null
Name=mm,sex=girl,school=null
Name=mm,sex=girl,school=null
Name=mm,sex=girl,school=null
Name=mm,sex=girl,school=null
Name=gg,sex=boy,school=null
Name=gg,sex=boy,school=null
Name=gg,sex=boy,school=null
Name=gg,sex=boy,school=null
Name=gg,sex=boy,school=null
Name=gg,sex=boy,school=null
Name=gg,sex=boy,school=null
Name=gg,sex=boy,school=null
Java.lang.AssertionError:it ' s wrong at Demo.main (DEMO.JAVA:32) invites readers to consider the following questions and welcome to interact in the comments:
What happens if the people set method removes the Synchronized keyword.
Why school is null.

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.