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

  1. package com.kb.compilercode;
  2.  
  3. public class CompilerCodeDemo {
  4.     int x;
  5.  
  6. }
package com.kb.compilercode;

public class CompilerCodeDemo {
	int x;

}


After compiler adding its code

Example1.class

  1. package com.kb.compilercode;
  2.  
  3. public class CompilerCodeDemo extends Object {
  4.     int x;
  5. public CompilerCodeDemo(CompilerCodeDemo this) {
  6.    super(this);
  7.     }
  8.  
  9. }
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

  1. package com.memory.management;
  2.  
  3. public class Example2 {
  4.     int x;
  5.     float y;
  6.     public Example2(int x,float y) {
  7.         System.out.println("parameterized constructor");
  8.         x=x;
  9.         y=y;
  10.     }
  11.  
  12.     public static void main(String[] args) {
  13.         Example2 obj1 = new Example2(1,1.5f);
  14.         System.out.println(obj1.x);
  15.         System.out.println(obj1.y);
  16.         Example2 obj2 = new Example2(2,2.5f);
  17.         System.out.println(obj2.x);
  18.         System.out.println(obj2.y);
  19.     }
  20. }
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

  1. package com.memory.management;
  2.  
  3. public class Example2 extends Object{
  4.     int x;
  5.     float y;
  6.     public Example2(Example2 this,int x,float y) {
  7.         super(this);
  8.         System.out.println("parameterized constructor");
  9.         x=x;
  10.         y=y;
  11.     }
  12.  
  13.     public static void main(String[] args) {
  14.         Example2 obj1 = new Example2(address of obj1,1,1.5f);
  15.         System.out.println(obj1.x);
  16.         System.out.println(obj1.y);
  17.         Example2 obj2 = new Example2(address of obj2,2,2.5f);
  18.         System.out.println(obj2.x);
  19.         System.out.println(obj2.y);
  20.     }
  21. }
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.

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on