Compiler code
When we write a program in Java, we know that it gets converted to byte code, but before that compiler adds some intelligent code into our code.
Let’s see what compiler will add
1) Extends Object class to our class
2) Default constructor if there are no constructor defined for a class
3) Adds this argument to the constructor
4) Adds super statement inside constructor as first statement (since there is no super or this exist already)
Example1.java
- package com.kb.compilercode;
- public class CompilerCodeDemo {
- int x;
- }
package com.kb.compilercode; public class CompilerCodeDemo { int x; }
After compiler adding its code
Example1.class
- package com.kb.compilercode;
- public class CompilerCodeDemo extends Object {
- int x;
- public CompilerCodeDemo(CompilerCodeDemo this) {
- super(this);
- }
- }
package com.kb.compilercode; public class CompilerCodeDemo extends Object { int x; public CompilerCodeDemo(CompilerCodeDemo this) { super(this); } }
Let’s see one more program on the same
Example2.java
- package com.memory.management;
- public class Example2 {
- int x;
- float y;
- public Example2(int x,float y) {
- System.out.println("parameterized constructor");
- x=x;
- y=y;
- }
- public static void main(String[] args) {
- Example2 obj1 = new Example2(1,1.5f);
- System.out.println(obj1.x);
- System.out.println(obj1.y);
- Example2 obj2 = new Example2(2,2.5f);
- System.out.println(obj2.x);
- System.out.println(obj2.y);
- }
- }
package com.memory.management; public class Example2 { int x; float y; public Example2(int x,float y) { System.out.println("parameterized constructor"); x=x; y=y; } public static void main(String[] args) { Example2 obj1 = new Example2(1,1.5f); System.out.println(obj1.x); System.out.println(obj1.y); Example2 obj2 = new Example2(2,2.5f); System.out.println(obj2.x); System.out.println(obj2.y); } }
Look at the below program to see what compiler has added to it.
Example2.class
- package com.memory.management;
- public class Example2 extends Object{
- int x;
- float y;
- public Example2(Example2 this,int x,float y) {
- super(this);
- System.out.println("parameterized constructor");
- x=x;
- y=y;
- }
- public static void main(String[] args) {
- Example2 obj1 = new Example2(address of obj1,1,1.5f);
- System.out.println(obj1.x);
- System.out.println(obj1.y);
- Example2 obj2 = new Example2(address of obj2,2,2.5f);
- System.out.println(obj2.x);
- System.out.println(obj2.y);
- }
- }
package com.memory.management; public class Example2 extends Object{ int x; float y; public Example2(Example2 this,int x,float y) { super(this); System.out.println("parameterized constructor"); x=x; y=y; } public static void main(String[] args) { Example2 obj1 = new Example2(address of obj1,1,1.5f); System.out.println(obj1.x); System.out.println(obj1.y); Example2 obj2 = new Example2(address of obj2,2,2.5f); System.out.println(obj2.x); System.out.println(obj2.y); } }
Compiler has added below things
1) Extends Object class to our class
2) Added this argument to the constructor
3) Added super as the first statement to the constructor
4) Added address of object as first argument to the constructor(hence this always points to the address of current object)
Note :
Since we are assigning x and y values in constructor, it has no effect to the instance variables, because local variables takes precedence. Since we used same name for local variables to assign the values and receive the values, compiler would not add this to these variables.
i saw may tutorials, no one mentions compiler adds some intelligent code into our code properly.
thanks for giving this post
Thank you…Keep reading