So let's go ahead and look at few other important bytecode instructions. 2
One minute if you're doing water and instructions called in work special on Iraq which we'll just recap 3
here like in the case of a real CPA you JVM that is an abstract computing machine also has an instruction 4
set. 5
We know that a method bytecode stream would act as instructions for the JVM and the bytecode instructions 6
also have a similar representation as regular instructions office EPU each bytecode instruction basically 7
involves a run byte opcode followed by 0 or more options. 8
The awkward specifies the operation to be performed when the operands specific additional information 9
. 10
For example the I add instruction that we saw earlier was just were adding two values. 11
In this case I add is the opcode and there were no options open and so actually implicit. 12
The upward sort implies popping the end values at the top of the operand stack and performing the necessary 13
operation. 14
We also have that Iler instruction. 15
One example was I load underscore one here the suffix one. 16
So says the opera and that is the instruction is telling JVM to dig the value at index 1 in the local 17
variables Ari on push it onto the top of the operands like like C.P. you DBMSs execution engine also 18
follows fetch on execution lifecycle. 19
And we know that that is GBM would fetch an upcourt on any trailing operands then it would execute the 20
operation requested by that upcourt and then it fetches the next instruction and so on. 21
Now let's use this simple program to review the bytecode instructions that it generates. 22
You can also find the program in resource section. 23
The program has a main method and one additional method called string which basically overrides the 24
string method in the Object class. 25
This method has a system that overprint Allen's statement underlines the string hello. 26
The main one that creates an instance of this class and simply in with a string method on that new instance 27
not to look at the bytecode we will use a jalopy too which is basically a bytecode disassembler that 28
comes with it. 29
We looked at it when discussing method binding. 30
It can be used to disassemble one or more class once and it can also take multiple command line options 31
. 32
For example if you include the option hyphen C it would print the byte code for each of them methods 33
in the class. 34
But if you can grab the option hyphen we are hyphen bobos then it would still print the byte code of 35
each of the methods. 36
But in addition it would also provide the dark constant boune on some additional information in case 37
you want to view all the possible options. 38
You can use hyphen help. 39
So let's go ahead and use hyphen VI option on the Hello class as shown here under the bytecode. 40
OK. 41
Now here is the Hello class. 42
So you're not going to look at that. 43
So I'm just going to compile this first and then. 44
So I'm just going to run the jalopy down with the hyphen B option. 45
OK. 46
And it's going to be hello dark class here. 47
OK so we are running or we are running it on the classified. 48
Now here is a byte code. 49
And we can see that this is the method called by a byte code Fard the two string mentored. 50
And similarly we have the bytecode for Main method here. 51
And this is the bytecode for the for the constructor for which is which is inserted automatically the 52
default constructor. 53
And we're going to analyze each of them separately the constructor and the two methods. 54
And then look at the byte code generated by them. 55
And here is the constant pool. 56
OK so this is the constant pool that's generated. 57
It's this constant pool and all of this is there. 58
And this was generated because of the hyphen v option. 59
OK. 60
And that is summertime information too and that's about it. 61
So let's just go ahead and review each of the methods under constructor. 62
Let's begin with looking at the bytecode generated for the constructor which was inserted automatically 63
by the compiler. 64
Now if you recall compiler when it auto inserts of default constructor it also ought to insert a statement 65
for invoking the superclasses default constructor. 66
And we know it's due to the constructor chaining process. 67
The first two byte code instructions that you see here correspond to that statement. 68
And let's see what they signify the first instruction here is in order underscores zero earlier in the 69
bit I load instruction the prefix in this gives indicates an object reference. 70
Well in the case of I load the prefix i indicates an in data type. 71
Other than the small difference the operation performed is identical. 72
That is it tigs the element at the specified index in the local variables array and pushes it onto the 73
top of the upper and stock index because the index is zero. 74
Not index zero we have this reference and that's because Kunstler is like an instance method unreleased 75
because that for instance Medders the zeroth element in the local variables will be a reference to the 76
current object that is this reference. 77
The reason why we have this reference is because it allows the code in the method or the constructor 78
to access any instance variables. 79
Similarly if the current method or constructor is invoking another method or constructor in the same 80
class or in a superclass then the this reference will be set in the local variables array corresponding 81
to that method or constructor next instruction is an expression and it has one operand which is hash 82
one getting a constructor in the superclass which is the object us apart from constructor. 83
Invoke special instruction will also be generated when we are in working either a private method or 84
when we are in Woking as superclass method we are the supercooled. 85
So you can try that out just are a private matter and then and look at the byte code and you should 86
be able to see and invoke special instruction. 87
Now here we are referring the constructor as instance initialization method and that's because of JVM 88
to do it. 89
In fact what we are seeing here in this particular snapshot is an instance initialization method and 90
such a method is inserted for each constructor in that class. 91
But in addition to constructor called this instance initialization method will also initialize all the 92
instance variables which in the source code might have been initialized outside of the constructor. 93
Let me actually show that to you in the code. 94
So let me go ahead and I had an instance variable here. 95
Should I use it to 3 so it's initialized outside the constructor. 96
In fact there is no constructor here. 97
So let's just go ahead and compile this and then do a java command. 98
Now let's look at the construct of code. 99
As you can see here in the constructor called. 100
So we have these three bytecode instructions in order underscore zero icons to underscore three and 101
put free. 102
So this is the code that is initializing the instance variable. 103
So that's the thing. 104
So in addition to the constructor code itself which is in this case in working the superclass constructor 105
it also initializes the instance variables. 106
How in this instance initialization method is also referenced using a special name called in it which 107
you can see here in the common the name here in the signature. 108
It still has the class name but in the constant bool it really did it for instance in it and we will 109
see that in a moment. 110
No come into the operation hash run it is simply an offset into the runtime constant pull of the current 111
class. 112
So let's look at the relevant section of the constant pull period is the constant Paulas sequential 113
number but to highlight only the relevant part. 114
I extracted only the relevant lines from the constant pull index one is basically a symbolic reference 115
to him either. 116
And in this case it's a constructor index one is referencing two other indices mind and pointy and nicks 117
nine is referencing in index 28 which is a symbolic reference to the object us. 118
Similarly in next one p is intended for rinsing. 119
And this has been an 11 word 10 gives the method name which as mentioned earlier is especially in that 120
index Levan holds information about what is called does matter descriptor which includes the matter 121
parameters under it and type. 122
In this case there are no parameters. 123
We only have an empty promises when the return type is void which is indicated by the letter B. 124
Know at runtime. 125
When GBM and contest this in Rook's special instruction it performs a few actions. 126
First it performs method resolution which in this case is a resolution of the instance initialization 127
method. 128
We know that this would entail replacing symbolic reference with direct reference. 129
Next JVM creates a new stack frame for the instance initialization method and pushes it onto the stack 130
. 131
Nexted pops this reference from the operand stack and started index 0 of the local variables array of 132
the newly created stack. 133
So the reason for having Elorde underscores zero was for storing this reference variable on the stack 134
so that Iraq's Special can use it next. 135
Know the instance initialization method is ready to be in work. 136
And so the program counter will be set with the first instruction in the instance initialization method 137
of the superclass. 138
So the execution continues from the first instruction. 139
So that was a constructor court. 140
I need an artist to review the Invoke special instruction. 141
Now let's look at the main method byte order. 142
The first statement in the main method creates a new instance of Hello. 143
Now this statement generates the first three bytecode instructions that you'll see here. 144
So let's start to make sense of this mapping. 145
The first bytecode instruction which includes a opcode new creates an instance of hello on pushes the 146
object reference onto the operand stack. 147
So at this point only the object is created but for it to be fully formed we need to also in the constructor 148
code. 149
For that we know that we need an in special instruction but that's the third instruction. 150
And there is a second instruction. 151
The second instruction is doop and it basically makes a copy of the item which is on the top of the 152
operand stack and also stores it on the top of the opening stack. 153
Now we need to make this copy as one of the things in walks Bishan does is to pop the top element from 154
the operand stack and store it as the zero element and the local variables array off the new frame that 155
it is going to create. 156
But if we don't have the loop instruction then invoke special would Bob the only object reference that 157
we have and with that we will not have a way to end with the string method which is the second statement 158
of the main method. 159
So we do need the instruction. 160
Next the following three bytecode instructions correspond to the second statement the instruction A-story 161
must go to one Bob's the hallowed reference from the operand stack unstarted at. 162
And next one in the local variables at it. 163
But if you are storing it index one at index 0 we have the only method parameter off the main method 164
which is a reference to the string theory. 165
Next a lot underscore one does the exact opposite. 166
That is it moves the element back to the stack from the local variables adding finally in-work which 167
will BOP's the hallowed reference from the stack. 168
And it also in wig's that boosting method. 169
Know if you recall we discussed in work which will indeed did in the method by adding lesson which was 170
covered in the inheritance and polymorphism chapter. 171
That lesson we said that for instance methods met that signature binding happens at compile time itself 172
but the method implementation binding happens only at runtime. 173
That's because with instance methods we do not know which method to work until runtime. 174
That is it is dependent on the object in context. 175
For example here you can see that at compile time method signature is bonded to the string method in 176
that object class. 177
But at runtime the string method in the Hello class will be executed just like in the case of in Woakes 178
mission. 179
Let's also review the steps performed when in which what is encountered also autoloader the pop instruction 180
which is the next one simply pops the top element on the stack. 181
It has a value returned by the spring mother. 182
Finally the return instruction at the end simply implies right as written. 183
When JVM encounters in-work what will it Fussball force method resolution which in our case is the resolution 184
of the string method and the object us. 185
This could simply mean pointing to an index in the metric table of the object class and the element 186
there would point to the actual string method code. 187
But the method to be executed is dependent on the actual object for that runtime. 188
We know that the operand stack has a reference to this actual object. 189
So JVM performs a lookup operation here. 190
It starts with the reference on the operand stack and follows it to the object on heap and then into 191
its method table. 192
In the metro area the actual implementation will be in the metal table at the next phone doing the matter 193
resolution step. 194
So the index number found during method resolution step then applies here in them at the table corresponding 195
to that Holocaust. 196
We discussed this when discussing method tables and we said that we are able to use the same index member 197
as a reference type of the variable is a class not an interface does not that using Mathare table in 198
such a way for a method implementation binding is entirely up to JVM implementation. 199
Some implementations might do it in a different way too. 200
At this point we know the method to be executor. 201
So the next three steps are identical to what we saw in works Bishan that is create a new stack frame 202
for boosting that and push it onto the stack. 203
But this reference from operand stack unstarted that index zero of the local variables array of the 204
new stack frame find a decent program counter to the first instruction of the new method. 205
Now here is a last snapshot which corresponds to a boosting method heading through quota system not 206
all that Prindle and segment which prints a string hello. 207
It also returns a string literal Hello no in the expression system that called We know that system is 208
a class an out is a static variable defined in that class. 209
The type of code really obvious but in stream from the Java dot or package sprint and method is invoked 210
on the reference variable. 211
So we need to forestall the old reference variable in the opening stack and this is done by get static 212
instruction. 213
So it basically pushes the value of all credible onto the open and stack next instruction LTC gets the 214
reference to the string literal Hello from the runtime constant pull on pushes it onto the open and 215
stack. 216
Finally invoke what you want. 217
BOP's both the string literal hello and the reference. 218
And in walks the print method. 219
Then the string literal hello as the argument. 220
No the last two instructions correspond to retaining the string Hello Azmat that's output under instruction 221
. 222
Arrogance simply implies that we are returning an object reference. 223
There are other variations we're primitive types like I've written for returning an end date type. 224
So that's about it. 225
They look at both in word spatial and in word what you're willing to put in was started in detail and 226
method by adding lesson. 227
And it is related to in working static methods. 228
You can review that read you again if needed another instruction you know as in rock interface in our 229
program the reference type is a class object and the actual type of the object was hello. 230
But if the reference type was an interface then it would have generated and invoke interface instruction 231
. 232
So you can try it out by making the Hello class implement an interface Resorcin section includes a pointer 233
to the JVM specification page that includes detail information about all the bytecode instructions. 234
So if you want to get a more in-depth understanding of any of the instructions that recolored or if 235
you want to learn about any new instruction then you can refer to this page. 236
So that's about it. 237
Thanks and I hope you'll enjoy learning about the bytecode instructions.