JAVA-000

返回Week_01主页

作业要求

1、自己写一个简单的 Hello.java,里面需要涉及基本类型,四则运行,if 和 for,然后 自己分析一下对应的字节码,有问题群里讨论。

演示Java Class文件的字节码(bytecode),以及 javac 和 javap 工具的使用

Reference:

这里介绍一个最基本的用法,以 helloworld 为例,按照 IntelliJ IDEA 的文件组织结构编译java文件为class文件:

cd "C:\Data\Code\GitHub\GeekbangJavaTrainingCamp\Week_01\Bytecode\bytecode"
javac "src\main\java\org\example\helloworld\HelloByteCode.java" -d "target\classes"

javac helloworld

javap

javap (Java Disassembler): Disassembles one or more class files.
javap是JDK提供的一个命令行工具,javap能对给定的class文件提供的字节代码进行反编译。 通过它,可以对照源代码和字节码,从而了解很多编译器内部的工作,对更深入地理解如何提高程序执行的效率等问题有极大的帮助。

Usage: javap <options> <classes>
where possible options include:
  -? -h --help -help               Print this help message
  -version                         Version information
  -v  -verbose                     Print additional information
  -l                               Print line number and local variable tables
  -public                          Show only public classes and members
  -protected                       Show protected/public classes and members
  -package                         Show package/protected/public classes and members (default)
  -p  -private                     Show all classes and members
  -c                               Disassemble the code
  -s                               Print internal type signatures
  -sysinfo                         Show system info (path, size, date, MD5 hash) of class being processed
  -constants                       Show final constants
  --module <module>, -m <module>   Specify module containing classes to be disassembled
  --module-path <path>             Specify where to find application modules
  --system <jdk>                   Specify where to find system modules
  --class-path <path>              Specify where to find user class files
  -classpath <path>                Specify where to find user class files
  -cp <path>                       Specify where to find user class files
  -bootclasspath <path>            Override location of bootstrap class files

GNU-style options may use = instead of whitespace to separate the name of an option
from its value.

Each class to be shown may be specified by a filename, a URL, or by its fully
qualified class name. Examples:
   path/to/MyClass.class
   jar:file:///path/to/MyJar.jar!/mypkg/MyClass.class
   java.lang.Object