The story behind Javac--empty class

Source: Internet
Author: User

The story behind Javac--empty class


Please indicate the original source and author information of the article and this statement in the form of a hyperlink.

Http://www.blogbus.com/dreamhead-logs/5999838.html


Programmers are most familiar with the source code, but in order to make the program really effective, without the help of the compiler. Javac's role is to compile Java code into JVM directives. Because the Java language and the JVM are out of the same door, so, a little familiarity, it is not difficult to find that they are almost directly corresponding. Of course, in order to simplify the writing of code, JAVAC in addition to direct translation, but also secretly help us do a lot of work, we look at the simplest situation.

public class Test {}

We compile this code with JAVAC, and JAVAP can help us decompile the generated class file.

Javap-c Test

Here is the result of the anti-compilation.

public class Test extends Java.lang.object{public test (); code:0: Aload_0 1:invokespecial #1; Method java/lang/object. " <init> ":() V 4:return}


Aside from the specific contents of the instruction, the results of the above anti-compilation clearly tell us that the empty class we have written is not empty at all, because there is also a constructor. This is the work that Javac did for us. Yes, it's javac, but it's not necessarily the JVM's requirement. In fact, classes running on the JVM can have no constructors at all. However, the previous example has clearly told us that, because the role of Javac, directly in the Java language is unable to construct a real empty class. So let's start with the bytecode and construct the real "empty" class with ObjectWeb asm.

Public class noctorgenerator {    public static void main ( String[] args)  throws exception {        string  className =  "Noctor";        classwriter cw  = new classwriter (CLASSWRITER.COMPUTE_MAXS);         cw.visit (opcodes.v1_2, opcodes.acc_public, classname, null,  "Java/lang/object",  null);         cw.visitend ();                 try {             os = new fileoutputstream (classname +  ". Class");             os.write (Cw.tobytearray ());         } finally {            if  (os !=  null)  {                 os.close ();            }         }    }}

With JAVAP, we can see the result of the build, and indeed there is no constructor.

public class Noctor extends java.lang.object{}

However, because no constructors exist, we cannot create objects with this class, but the following code proves that the classes generated by this class are actually available.

public class Noctormain {public static void main (string[] args) {System.out.println (noctor.class); }}

Running this code, we can get the following output:

Class Noctor


The story behind Javac--empty class

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.