The internal comparison method of the final keyword and string for Java

Source: Internet
Author: User

Recently smoked a bit of time, so some of the fragmented problems have been tidied up. I decided to put some of the problems that have been in the past to write blog articles, to learn younger brothers learn a warning.

Today's story begins with an example:

@Testpublic void Testfinal () {String s1= "Happybksoffer"; String s2= "Happybks"; final string s3= "Happybks";//s3.replace ("H", "H"); String s4=s2+ "Offer"; String s5=s3+ "Offer", if (S1 = = S4) {System.out.println ("S1==s4");} else {System.out.println ("S1!=s4");} if (S1 = = S5) {System.out.println ("s1==s5");} else {System.out.println ("s1!=s5");} if (S1.equals (S4)) {System.out.println ("S1 equals S4"),} else {System.out.println ("S1 not equals S4");} if (S1.equals (S5)) {System.out.println ("S1 equals S5"),} else {System.out.println ("S1 not equals S5");}}

If you see this code is inexplicable and accompanied by a little guilty, think not all is the same, then congratulations, after reading the example today, you will not have to jump into the pit. If you see the hole in one eye, then congratulations, you can comment on my blog under the BB, "This is a problem, too basic." (This article is from: http://my.oschina.net/happyBKs/blog/493904, please indicate your own source)

The actual operating results are as follows:

S1!=s4s1==s5s1 equals s4s1 equals S5

Is there anything unexpected about you?


So what is this, S4 and S5 are both "Happbks" and "offer" by virtue of, why again = = and equals under the result is not the same?

Let's first talk about the difference between = = and Equals method. The two ways of judging whether two values are equal are essentially different. = = In determining the basic data type, there is no difference, is to determine whether the value of a basic data type is equal, when determining the object data type, according to the object's class overrides the Equals method to determine whether the object is equal (if there is no re-implementation of the Equals method of the class, The runtime determines whether the address of two objects points to the same object).

At this time, perhaps the innocent children will say: "I understand, if it is an int type, then equals and = = no difference, the integer type is not." At this time, there will be someone in the next sneer, thinking: "Unpacking and boxing do not understand, hehe ... It must have been the same.

Let me give you an example:

@Testpublic void Testint () {int a=1,b=1;if (a = = b) {System.out.println ("a==b");} else {System.out.println ("a!=b");} if (A.equals (b)) {//system.out.println ("a equals B"),//} else {//system.out.println ("A not equals B");//}integer c=new Integer (1), d=new Integer (1), if (c = = d) {System.out.println ("C==d"),} else {System.out.println ("C!=d");} if (C.equals (d)) {System.out.println ("C equals D");} else {System.out.println ("C not equals D");} Integer e=1,f=1;if (E = = f) {System.out.println ("e==f");} else {System.out.println ("e!=f");} if (E.equals (f)) {System.out.println ("E equals F");} else {System.out.println ("E not equals F");}}

So what's the result?

A==BC!=DC equals D1e==fe equals f

Yes, laugh at others simple people's eyes are not blind, the results are as simple as the children expect. Well, don't wringing pretending suddenly, simple results behind there are not simple reasons.

the first int = = = There is nothing more to say. Why does the integer type C and D,e and F have two different demerits in equals? This is due to the optimizations of the Java compiler: C and D each initialize an object type, so when = = comparison, they are not referring to an object, so = = comparison is different, and the Equals method of the integer has been rewritten, comparing the actual integer value of the two, so is equal. E and F Do use boxed unboxing, but the Java compiler is optimized at compile time, and the two constant 1 is optimized to an object, so that E and F point to the same integer object, so the = = is the same.

Go back to the example we just made. Although both S2 and S3 are "Happybks", S2 is the final type, and S3 is the general type. When the Java compiler compiles, the constant values are also optimized, and the compiler optimizes s4=s2+ "offer" because S2 is constant, "offer" is also a constant, so S4 will be converted directly to s4= after compilationHappybksoffer , and the S1 initialization assignment is also a constant "happybksoffer", so the compiler will again refine them to a string object, S1 and S4 point to the same string object, so both are in the = = is the same. and s5=s3+ "offer", S3 is not final, so must be evaluated at run time, so S5 only at run time to generate an object, it is certainly not the same as the S1 of the same string object, so S1 and s5 in the = = comparison must be different.

You got it. Perhaps you are still very worried, we only give a suggestion here: that is, try to develop the good habit of equals!


Well, finally, the final of our team is a summary: Final can modify classes, methods, and variables.

Modifier class

When a class is decorated with final, it indicates that the class cannot be inherited. In other words, if a class you never let him inherit, you can use final to decorate. Member variables in the final class can be set to final as needed, but note that all member methods in the final class are implicitly specified as the final method.

When using the final modifier class, be careful to choose, unless the class is not intended to be inherited later or for security reasons, try not to design the class as the final class.

Modification methods

There are two reasons to use the final method. The first reason is to lock the method in case any inherited class modifies its meaning, and the second reason is efficiency. In earlier versions of Java implementations, the final method was converted to inline invocation. However, if the method is too large, you may not see any performance gains from inline calls. In the recent Java release, these optimizations do not need to be made using the final method.

Therefore, the method is set to final only if you want to explicitly prohibit the method from being overwritten in the subclass.

Note: The private method of the class is implicitly specified as the final method.

Modifier variables

The modified variable is the most used place in final use, and it is also the focus of this article. First look at the basic syntax of the final variable:

For a final variable, if it is a variable of the base data type, its value cannot be changed once initialized, and if it is a variable of a reference type, it cannot be pointed to another object after it has been initialized.


String types We also make a supplementary summary:

String is not a primitive type, but an object type, and inside yo a final char array, so the method in string does not provide a way to modify the content characters in a string. Even substring, replace, and so on seem to modify the method of string, actually just returned a new string object, and the original array does not change. The purpose of this is to have a lot of performance, thread safety considerations, I will be a special article in the future to talk about this.



The internal comparison method of the final keyword and string for Java

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.