I have this byte code:
new java.lang.Object
// stack is [newObjectRef]
dup
// Stack is [newObjectRef newObjectRef]
invokespecial void java.lang.Object.<init>()
// Stack is [initializedAsTypeObjectObjectRef]
putstatic java.lang.Object class.a
// variable a has the reference of new object
getstatic java.io.PrintStream java.lang.System.out
// Take the static value of System.out
// Stack is [initializedAsTypeObjectObjectRef System.out]
Update this is the continuation:
> ldc "test" // Stack is
> [initializedAsTypeObjectObjectRef System.out "test"]
> jsr pos.0000026C // call a subrutine invokevirtual void
> java.io.PrintStream.println(java.lang.String) // actually print the
> result // stack is (I think) Empty at this time ?
Does the translation is:
Object a = new Object();
a = "test";
System.out.print(a);
Is my stack good ?
I am not sure to well understand out(). Probably I will have to use out() setter and to print() after ?
I always use out() to print habitually..
A bytecode program may be executed by parsing and directly executing the instructions, one at a time. This kind of bytecode interpreter is very portable. Some systems, called dynamic translators, or just-in-time (JIT) compilers, translate bytecode into machine code as necessary at runtime.
The bytecode format Bytecodes are the machine language of the Java virtual machine. When a JVM loads a class file, it gets one stream of bytecodes for each method in the class. The bytecodes streams are stored in the method area of the JVM.
class files consist of a bunch of bytecodes. Bytecode is to Java what assembler is to C++. Each bytecode is a number no larger than a byte and has a mnemonic. The numbers and their mnemonic are what you have listed in your question.
Bytecode is the instruction set of the Java Virtual Machine (JVM), and all languages that run on the JVM must eventually compile down to bytecode. Bytecode is manipulated for a variety of reasons: Program analysis: find bugs in your application.
If I compile the code
public static void main(String[] args) {
Object a;
a = "test";
System.out.println(a);
}
and run
javap -c Main
I see
public static void main(java.lang.String[]);
Code:
0: ldc #2 // String test
2: astore_1
3: getstatic #3 // Field java/lang/System.out:Ljava/io/PrintStream;
6: aload_1
7: invokevirtual #4 // Method java/io/PrintStream.println:(Ljava/lang/Object;)V
10: return
You can see that getstatic
loads the field System.out
Object doesn't have a method
called out()
so I don't believe you are looking at the code you believe you are.
getstatic
gets a static
fields e.g. System.out
is a static
field of System so if you write
System.out.println();
This will result in a use of getstatic
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With