1, Magic number in programming
In the first article of constant definition in Section II of the programming specification of Alibaba Java Development Manual:
Magic value (magic number) refers to an undefined constant, while the "ID × Taobao × U" in the opposite example is called Magic String. This rule has been mentioned in "Clean Code" and code specification manuals of major companies, so why such a rule?
This view is expressed in the book "construction and interpretation of computer programs":
The first meaning of the code is for human understanding, and the second is that it can be executed correctly by the machine.
No program ape wants to read code without comments. Comments are a good thing, but who wants to add comments after every constant in his code? Don't you feel tired? We look tired! A project in the company is basically multi person cooperation and multi group handover. No one wants to spend a few days to read and understand the code he wrote before taking over the last stall. Therefore, a good code specification is necessary for every program ape. Static variables, enumerations, macro definitions and other methods are used to avoid magic numbers in the code, greatly improving the readability of the code, so that you can Our partner will take your villain and stab you.
2, Magic number in file
Magic number in files is to determine the type of files. Many files have magic number, such as "BM" in bmp and "JFIF" in JPEG/JFIF. In addition, some files in audio and video formats have magic number, including Java. Here is a simple java file
public class MagicNumber { public static void main(String[] args) { System.out.println("Cafe Babe"); } }
In the current directory, shift + right-click to open the Power Shell, compile the java file with javac, and press Alt+X in Emacs to enter hexl mode to open the class file in hexadecimal mode:
Pictured
- The first four bytes of cafebabe is the magic number of the Class file
- 5. 6 bytes is the minor version number of Class file
- 7. 8 bytes is the main version number of Class file, 0034, that is, the decimal 52.0 corresponds to JDK 1.8.0
About cafebabe, one of the founders of Java, James Gosling wrote on his blog:
We used to go to lunch at a place called St Michael's Alley. According to local legend, in the deep dark past, the Grateful Dead used to perform there before they made it big. It was a pretty funky place that was definitely a Grateful Dead Kinda Place. When Jerry died, they even put up a little Buddhist-esque shrine. When we used to go there, we referred to the place as Cafe Dead. Somewhere along the line it was noticed that this was a HEX number. I was re-vamping some file format code and needed a couple of magic numbers: one for the persistent object file, and one for classes. I used CAFEDEAD for the object file format, and in grepping for 4 character hex words that fit after "CAFE" (it seemed to be a good theme) I hit on BABE and decided to use it. At that time, it didn't seem terribly important or destined to go anywhere but the trash-can of history. So CAFEBABE became the class file format, and CAFEDEAD was the persistent object format. But the persistent object facility went away, and along with it went the use of CAFEDEAD - it was eventually replaced by RMI. 0xCAFEBABE's decimal number is 3405691582. If we add up all the bits we get 43. Exactly greater than 42-Ultimate Answer to the Life, the Universe, and Everything. The other 43 is also a prime number. You see, magic everywhere. Even in the last sentence.
Part of the translation is as follows:
"We often go to a place called St Michael's Alley for lunch. According to local legend, a long time ago, a band called Grateful Dead often performed here before becoming famous We often go there, we call it Cafe Dead. I noticed that the name is a string of hexadecimal numbers. At that time, I needed a pair of magic numbers to represent Class files and Object files (files serialized by objects). I used CAFEDEAD to represent Object files, and then replaced the last two bytes. Finally, I decided to use CAFEBABE to represent Class files The Object file is replaced by RMI (remote method call), and CAFEDEAD goes with it. 0xCAFEBABE's decimal number is 3405691582, and when you add up each one, you get 43, which is exactly greater than 42 - the ultimate answer to life, the universe, and everything (from Douglas Adams's novel the guide to the galaxy, lol). In addition, 43 is also a prime number. You see, magic is everywhere, even in the last sentence. (lmaooooo)”
Sure enough, the brain circuits of the big guys are different. Hahahahahahaha, and in this way, the naming of Java coffee is a little bit slim from here.