(The Wait () or notify () must be called with the current lock) && (class. Method vs Object. method) __java-Exercise

Source: Internet
Author: User

public class Test1 {
public static void Main (string[] args) {
Integer a = 100;
Integer b = 100;
System.out.println (A==B); True
System.out.println (A.equals (b)); True

    Integer C =;
    Integer d = m;
    System.out.println (C==d);  False
    System.out.println (C.equals (d));//true
}

}
/**
* In the encapsulated class of Integer, its interior opens up a buffer for storing the number of -128~127, when an object value created externally (int) is referenced directly from the buffer area data, i.e., A and B elements are memory that returns the reference to the same piece of address , that is, the same address; When the data is not in this area, the internal implementation of the class is reset to the new area on the heap, at which point the address must certainly be different; just like C and D are respectively new to the area, at this time = Certainly not the same, equal () The implementation of the system is directly a comparison of values so all are true.
*/

/*public class Test2 {
public static void Main (string[] args) {
Object obj = new Object ();
Synchronized (Thread.CurrentThread ())

{
    obj.wait ();
    Obj.notify ();
}

}
}
/**
* 1 edit not pass, its result:
*exception in thread "main" java.lang.Error:Unresolved compilation problem:
Unhandled Exception type Interruptedexception
*/
/*public class Test2 {
public static void Main (string[] args) {
Object obj = new Object ();
Synchronized (Thread.CurrentThread ())
{
try {
Obj.wait ();
catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}

    Obj.notify ();
  }

}*/
/*
* 2 editors can pass, the result:
* Exception in thread "main" java.lang.IllegalMonitorStateException
*/

/**
* Best Practices:
* 1 the Wait () function must be caught unexpectedly;
* 2 invocation of Wait () or notify () must be invoked with the current lock, that is, the object in synchronized must be used
*/

Package secondday;

public class Test3 {
public static void Main (string[] args) {

   String str = new String ("xyz");

   System.out.println (2<<3);   
}

}
/*
* 1 describes the process and purpose of the JVM loading Java files.
* A. java file is first compiled with a compiler, such as Eclipse. class file, he is a bytecode file, not the machine is the final running machine code, when the program is running, the virtual machine again to execute each byte code to the interpreter (different platform, interpreter interpretation of different ways), interpreter and translates it into machine code on a particular machine. The interpreters of different platforms are interpreted differently and the machine codes are not the same, so they can be cross-platform.
* 2 String s = new String ("XYZ"): Several string Object created?
* Answer: Two.
* 3 char variable can store a Chinese character, why?
* A: Yes, because Java is a Unicode encoding, one char occupies 16 bytes, and one Chinese character is 16 bytes.
* 42 object values are the same (x.equal (y) = = true), but they can have different hash code, right.
* Answer: Error. Hash code must be the same
* 52 objects have the same hash code, two objects (x.equal (y) = = true) right.
* Answer: Error.
*/

Public class Test4 {
private static void TestMethod ()
{
System.out.println ("TestMethod");
}
public static void Main (string[] args) {
((TEST4) null). TestMethod ();//Run normal output TestMethod
}
br>/**
* Annotations:
* 1 This is a call to a method by a class, not an object to a method, and the
* 2 method is a static, passive method, directly using the class. method, because the static method uses the not-dependent object to be created, and null can be coerced to any type (not any type of object), as a result, it is possible to execute static methods
* 3 Non-static methods using the method of "object. Method", you must rely on object creation before you can invoke it, and if you remove the
* static before the TestMethod () method, you will report a null pointer Exception, the 2 point of view is also validated here.
*
*/

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.