Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Why does the class file not respond when you add a blank line to the Java code?

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

The main content of this article is to explain "Why add a blank line to the Java code and the class file does not respond", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Why to add a blank line to the Java code class file does not respond" bar!

1. Fall out and don't recognize people.

Java claims to be compiled and run everywhere, probably thanks to the class file. The compiled class files of different Java versions are definitely different, because there is a version number in it, which must affect their contents.

Let's just see if the class file of the above code will change if you add a blank line to it.

This blank line can not be added at will. It may be above or below the xjjdog. It may be in {or at the end of the file.

(1) hit in the face

Before verifying, let's take a look at the current class file MD5 value.

I like being hit in the face very much, so let's take a look at a situation where it doesn't matter to add blank lines.

After compiling again, look at the MD5 value, and it was hit in the face. Fortunately, I have developed the ability of not blushing but not beating, and I have the cheek to accept this result.

(2) apply medicine

In order to echo the theme and comfort the broken heart, we transferred the blank line to here.

After compiling again, look at the MD5 value (I feel like I've already said that).

It's changed. It's really changed this time.

Use the hexdump command to analyze the bytecode generated twice and find that only one number has changed.

two。 Sao Desnai

I don't like to analyze such binary things in particular. Although the magic number of CAFEBABE is vivid in the first line. Coffee, baby? Why does it sound like a serial number?

Let's take a look at its prototype in javap.

Javap-p-v HelloWorld.class

By comparing the bytecode generated twice, we finally found that the change was caused by a structure called LineNumberTable.

Using asmtools.jar to drill down into this structure, you can see the same information.

LineNumberTable shows the correspondence between Java source line numbers and bytecode instructions. The preceding number represents the line number in the Java source code, while the colon represents the mapping of each line of instruction in the bytecode. When debugging the code, it can locate quickly and proceed smoothly.

In other words, this is auxiliary information, which we can erase when compiling. How do I erase it? Give javac a parameter and ok.

Javac-g:none HelloWorld.java

The compiled bytecode is compact, elegant and useless. No matter how many blank lines you add, the generated bytecode is the same. However, we can no longer be happy to debug.

{public HelloWorld (); descriptor: () V flags: ACC_PUBLIC Code: stack=1, locals=1, args_size=1 0: aload_0 1: invokespecial # 1 / / Method java/lang/Object. "": () V 4: return public static void main (java.lang.String []); descriptor: ([Ljava/lang/String ) V flags: ACC_PUBLIC, ACC_STATIC Code: stack=2, locals=1, args_size=1 0: getstatic # 2 / / Field java/lang/System.out:Ljava/io/PrintStream; 3: ldc # 3 / / String Hello xjjdog 5: invokevirtual # 4 / / Method java/io/PrintStream.println: (Ljava/lang/String ) V 8: return}

To make bytecode delicious and useful during the development phase, you can turn on all debugging information directly with the parameter-g. IDEA can turn on this parameter in the compilation options. Many students can not find the symbol table of local variables in the compiled code, which is also caused by the fact that this parameter is not turned on.

At this point, I believe you have a deeper understanding of "why add a blank line to the Java code class file does not respond", might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report