Java details, how much have you noticed?

Source: Internet
Author: User
Tags shallow copy

As an excellent object-oriented programming language, Java is being used by more and more people. In the actual development of some of the Java language is easy to ignore the details, below to share the hope to be able to learn the Java language to help people.

1, the displacement operation is out of bounds how to deal

What is the result of the following code output?

int a=5;

System.out.println (a<<33);

According to common sense speculation, move a to the left 33 should be all the effective bits of a to move out, the rest is 0 ah, so the output should be 0 to ah, but after the implementation of the discovery output is 10, why? Because the Java language is optimized for displacement operations, the Java language converts a< to a<< (b%32), so when the number of bits B to shift is more than 32 o'clock, the number of bits actually shifted is the value of b%32, then the above code a<<33 the equivalent of a <<1, so the output is 10.

2, can I get i!=i?

When you see this proposition you will think I'm crazy, or the Java language is crazy. This seems to be absolutely impossible, how can a number not equal itself? Perhaps the Java language is really crazy, do not believe that the following code output what?

Double i=0.0/0.0;

if (i==i) {

System.out.println ("Yes i==i");

}else{

System.out.println ("No i!=i");

}

The above code output "No i!=i", why this? The key is the value of 0.0/0.0, which retains a special value in the IEEE754 floating-point arithmetic rule to denote a number that is not a number. This value is Nan (the abbreviation for "not Anumber"), which will get this value for all floating-point computations that are not well-defined, such as: 0.0/0.0; we can also use Double.NaN directly to get this value. The IEEE 754 specification specifies that Nan is not equal to any value, including itself. So I have it! The =i code.

3, what kind of equals is safe?

We all know that the 5 principles covered by the Equals method are defined in the Java specification: reflexive (reflexive), symmetric (symmetry), transitive (transitive), consistent (consistent), Non-null (non-nullability). Then examine the following code:

public class student{

private String name;

private int age;

Public Student (String Name,int age) {

This.name=name;

This.age=age;

}

public boolean equals (Object obj) {

if (obj instanceof Student) {

Student s= (Student) obj;

if (S.name.equals (this.name) && s.age==this.age) {

return true;

}

}

return super.equals (obj);

}

}

Do you think the above code is safe to overwrite the Equals method? The surface seems to be no problem, so it does satisfy the above five principles. But in fact such coverage is not very safe, if the student class has a subclass collegestudent, if I take a student object and a Collegestudent object equals, as long as the two objects have the same name and age, They are considered equal, but in fact they are two different types of objects. The problem is on the instanceof operator, because the operator is backwards compatible, meaning that a Collegestudent object is also considered an instance of student. How to solve this problem? It is only possible to use the GetClass () method of the object to determine whether two objects are of the same type without the instanceof operator, for example, by modifying the Equals () method above to:

public boolean equals (Object obj) {

if (Obj.getclass () ==student.class) {

Student s= (Student) obj;

if (S.name.equals (this.name) && s.age==this.age) {

return true;

}

}

return super.equals (obj);

}

This ensures that the Obj object must be an instance of student, and not an instance of any of the student subclasses.

4, shallow copy and deep copy

1) Shallow copy and deep copy concept

⑴ Shallow copy (shallow clone)

All the variables of the copied object contain the same value as the original object, and all references to other objects still point to the original object. In other words, a shallow copy simply duplicates the object being considered, not the object it refers to.

⑵ deep Copy (Deep clone)

All the variables of the copied object contain the same values as the original object, removing the variables that refer to other objects. Variables that refer to other objects will point to new objects that have been copied, not those that are already referenced. In other words, a deep copy copies the objects that are referenced by the object being copied again.


Java details, how much have you noticed?

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.