Perform a simple Java script under MacOS terminal

Installing the MacOS Java JVM, the configuration environment variables may perform a simple java script in the terminal, wherein the first javac compiler used to compile byte code files into .java files, then execute java bytecode interpreter files .

Write a simple script

1 class Demo{
2 
3     public static void main(String[] args){
4         System.out.println("hello world!");
5     }
6     
7 }

After writing saved as .java format, use the terminal to see here:

Open a terminal compilation

After using javac compilation, after the implementation of the target folder will be more out of a Demo.class bytecode files.

Open a terminal interpreted

java command interpreted, can be found in normal output "hello world", execution OK.

Java file name and class name as consistent as possible

Now there is a question, if I java file class name changed to HelloWorld, what will happen, what will compile the results, the following revised the class name as follows using vim at a terminal.

Recompile, finding the target folder more out of a HelloWorld.class byte code file, the file name is the name of the class, use the java command to properly interpret the original compilation.

If you have many files, inconsistencies such class name and java file names should be avoided, so you can add a modifier public, can constrain java file name and file name of the class must be consistent, otherwise unable to compile, as it suggests the proposed changes consistent java file name and class name.

Java file to write more than one class

如果在java文件中定义多个类也是可以的,编译后生成多个class文件。

结论

(1)Java执行需先将代码转换成字节码文件,字节码文件被解释器执行解释后变成机器码,机器码才能被计算机识别执行。

(2)编译命令:javac 编译的java文件全名,包括文件类型java。

(3)运行命令:java 要运行的class文件名,一般为一个类对应一个class文件

 

参考博文:https://www.cnblogs.com/chengdabelief/p/6576320.html

Guess you like

Origin www.cnblogs.com/youngchaolin/p/11258375.html
Recommended