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.

Lets see what it can add

1) Extends Object 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
2
3
4
5
6
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

1
2
3
4
5
6
7
8
9
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);
	}

}

Lets see one more program on the same

Example2.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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);
    }
}
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);
	}
}

What will be the output of this program?

Look at the below program to see what compiler has added to it.

Example2.class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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);
    }
}
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, we don’t get this to be added to these variables.

About the Author

Karibasappa G C (KB)
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