Details of dynamic and static compilation instances in Java

  • 2020-06-15 09:11:07
  • OfStack

Details of dynamic and static compilation examples in Java

First, let's talk about dynamic and static compilation.

Q: What is the difference between java and javascript?

There are the following points:

1. First, the java code is translated into class file on JVM and javascript is loaded and run directly on the browser.

2. As you can see from point 1, the java code needs to be compiled, while javascript does not.

3. In terms of language nature, java is a high-level programming language with strict requirements on variable checking, while javascript is a simple interpreted scripting language with weak requirements on variable checking.

javascript is a dynamic language, while java is "semi-dynamic".

javascript is dynamic, not to be doubted. Why is java semi-dynamic? See the following code:


public class Color{ 
  
   public void changeColor(){ 
     System.out.println(" The color to change is red "); 
   } 
 
} 

 public class Frame{ 
 
    public static void main(String args[]){ 
      Color c=new Color(); 
      c.changeColor(); 
    } 
 
} 
 

The Frame class calls its methods by directly using new1 Color objects, which are identified at compile time to determine the relationship. I think this is a manifestation of the static nature of the java language. If the compiled Color. class file is missing, an error is reported.

java.lang.NoClassDefFoundError........

We find java dynamic if we follow the following code:


public interface Color{ 
  
   public void changeColor(); 
 
} 

public class ColorImp implements Color{ 
 
     public void changeColor(){ 
       System.out.println(" Change the color to red "); 
     } 
} 

public class Frame{ 
 
    public static void main(String args[])throws Exception{ 
      System.out.println(" Please enter the name of the class you want to enter "); 
      // The input 1 The name of a class  
      java.util.Scanner sc=new java.util.Scanner(System.in);  
      String s=sc.next(); 
      // Load this class  
      Class c=Class.forName(s); 
      // Gets the instantiation object of this class  
      Object obj=c.newInstance(); 
      // Mandatory transformation  
      Color color=(Color)obj; 
      color.changeColor(); 
    } 
 
} 

Note the Frame class. When we compile it, only two class files will show up -- Frame.class and Color.class. Then we run the program and throw an exception -- java.lang.ClassNotFoundException -- because we didn't compile ColorImp.

So, we open another cmd process, compile the ColorImp class separately, and then type ColorImp into the previous cmd dialog. The program will run normally.

This allows us to dynamically load a class without stopping the program. I think this shows the dynamic nature of Java. From the above example, I consider java to be a "semi-dynamic" language.

As you can see, the example above USES the Color interface class. I have to mention a little touch of interface here.

When using a database, the Java language just defines one database interface and then different databases to implement this excuse. These include (take the mysql database for example)
Load database driver:


Class.forName("com.mysql.jdbc.Driver"); 

Establish a link to the database:


java.sql.Connection conn = java.sql.DriverManager.getConnection( 
          url, user, password); 

Get the compiled object, result set object, etc., which just define the interface, and leave the implementation to the database developer, as long as the interface is implemented. This reflects the extensibility and standardization of the interface.

Thank you for reading, I hope to help you, thank you for your support to this site!


Related articles: