Do whatever you want in programming [Christmas Edition]

As we all know, the value in the final String in Java is immutable. Everyone knows that String's + (concatenation) operation discards the memory reference and reclaims the address in memory, and it does. But are final variables really immutable? I would like to open the programmer's thinking with this article and think out of the stereotype. I hope this article will bring new thinking to your programmer's career.

a simple example

This example is a long time ago and has been done by predecessors, but not all programmers have come into contact with it. Programmers who usually like to "church" will not be unfamiliar with this.


import java.lang.reflect.Field;

public class ChangeFinalString {

    public static void main(String[] args) throws Exception {
        final String s = "12345: caiyongji";
        System.out.println(s);
        System.out.println("hashcode: " + s.hashCode());
        Field f = String.class.getDeclaredField("value");
        f.setAccessible(true);
        char[] value = (char[]) f.get(s);
        value[0] = (char) 20851;
        value[1] = (char) 27880;
        value[2] = (char) 20844;
        value[3] = (char) 20247;
        value[4] = (char) 21495;
        System.out.println(s);
        System.out.println("hashcode: " + s.hashCode());
    }
}

In the example, a String is initialized through final, and then the Field named value in the String class is obtained by reflection, and the value is reassigned. After execution, you will find that the hashcode value of String has not changed, but the value of String has changed. In addition, you will also find Easter eggs for these simple lines of code.

get conclusion

Look! Do you feel that routines can be broken and that with enough skill you can do whatever you want in programming? Maybe some serious programmers will point out that the implementation of the hashcode method depends on the JVM and does not directly reflect the memory situation. To be honest, you don't even need to memorize the examples in this article, I just want to break your stereotype.

However, the above is not really the conclusion I want to say.

Here's what the " Christmas version " really means.

Maybe some people will find it very rewarding, I have the answer to another interview question. That's right, even in domestic giants like Ali and Baidu, some interviewers (note that there are some ) do ask some silly questions like "final variables are immutable". They study a language and even know all Specific implementation details, and even language bugs. But from another angle, while delving into it like this, have you considered your time cost, your girlfriend, and your offspring? !

interface theory

As a programmer, you are the interface that the business logic and code implements. No one cares about how you implement the business, only whether your code is efficient, accurate, easy to use, and easy to expand (specifically, how many ms to return the result, whether the parameter structure is simple, and whether it is easy to add new functions). Similarly, if you use Java as your tool to implement business logic, you can choose Python, Node.js, Kotlin or even Linux shell scripts, so do you need to understand all the implementations of the Java language? As I said in How to Be a 10x Faster Programmer Don't Memorize.

Am I calling on everyone not to ask for more?
Yes.

reverse design

If you think of programming as a tool, nothing more than languages, documents, and frameworks, all of which can be regarded as part of the tool. Don't try to open the screws with a hammer, although you can. But this is counter-engineered. Just like this example, you can change final by reflect, but to put it bluntly, this technique is not useful, it will bring you a lot of trouble, such as considering compatibility when expanding and upgrading java versions.

Therefore, when using a "tool", try to use it according to the thinking mode of the tool maker. All these so-called "techniques" are anti-design.


Finally, focus, rigor, and clear logic are the qualities of a programmer, but in the context of such rapid technological iterations, it is impossible for a human to pursue the ultimate in code (if you measure it by mastering more skills, you You can never master all the skills, and the skills you have mastered are gradually phased out in the process of technology iteration.), don't forget the friends, family and your hobbies in your life.

Christmas is a new year, I wish all programmers a Merry Christmas (this article was written on Christmas Eve), and start a new and different life for programmers in the new year.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325398222&siteId=291194637