To talk about the role of the final turn of the final role

This article is reproduced from May CJ: talk about the role of final

Foreword

Always wanted to write about this topic. Company representatives also interviewed a number of candidates, each interview I will ask one of two questions is "you talk about understanding of the final keyword." This is a simple little question, but do not underestimate it, the answer to this question as well as some simple extension of job-seekers will be able to see whether a solid foundation and whether inquiring mind. OK, get down.

 

The role of final keyword

I believe the final usage, most people can casually say three sentences:

1, modified final class can not be inherited

2, the final modified methods can not be rewritten

3, the modified final variables can not be changed

Key is the third sentence. The modified final variables can not be changed and what can not be changed it, is a reference to a variable? Inside the content or variable? Or both can not be changed? Write an example look to know:

public class FinalString
{
    private String str;
    
    public FinalString(String str)
    {
        this.str = str;
    }

    public String getStr()
    {
        return str;
    }

    public void setStr(String str)
    {
        this.str = str;
    }
}
public class Test
{
    public static void main(String[] args)
    {
        final FinalString fs = new FinalString("1");
        fs.setStr("2");
        System.out.println(fs.getStr());
    }
}

Run it, no problem at all. Slightly modified it:

public static void main(String[] args)
{
     final FinalString fs = new FinalString("1");
     final FinalString fss = new FinalString("333");
     fs = fss;
}

Line 5 incorrect report, "The final local variable fs can not be assigned". Visible, the final modification is immutable variable reference instead of citations point to, point to reference content can be changed . OK, that final modification of the array it?

public static void main(String[] args)
{
    final String[] strs0 = {"123","234"};
    final String[] strs1 = {"345","456"};
    strs1 = strs0;
    strs1[1] = "333";
}

Similarly, line 5 being given "The final local variable strs1 can not be assigned", line 6 no problem at all. Variables and arrays, as are references to immutable, pointing to a variable reference content . In fact, if used FindBugs plug-in should be aware that if the code inside a final modification of an array, then the code will be diverted as a bug findBugs is to find out, because "with a final modified array is meaningless."

Next, let's look at a final modification of the method parameters of the scene:

public class Test
{
    public static void main(String[] args)
    {
        FinalString fs = new FinalString("");
        A(fs);
    }
    
    private static void A(final FinalString fs)
    {
        fs.setStr("123");
        FinalString fss = new FinalString("22");
        fs = fss;
    }
}

Like, the same error line 13, line 11 is no problem, I believe we already know why.

 

to sum up

"Reference" is a very important concept in Java, a reference for understanding is not deep, it is easy to make some of their own are not aware of the error. The modified final variables, no matter what kind of variables are variables, remember immutable is a reference to a variable rather than a reference to an object's contents . In addition, the role of this article there are two things not mentioned in the final:

1, the modified final method, JVM will attempt to seek whom inline, which for enhancing the efficiency of Java is very important. Thus, if the determination method can not be inherited, then try to define the method as final.

2, modified final constants at compile time constant pool will be credited to the calling class.

Guess you like

Origin www.cnblogs.com/alimayun/p/12046462.html