On Wed, 7 Jun 2000, David Blankley wrote:
> I went on an interview last week and was thrown this question:
...
I tested out your example with the jikes java compiler and
it produced two class files that were not the same size.
public class B1 {
public boolean b;
}
public class B2 {
public boolean b = false;
}
% jikes B1.java B2.java
% ls -la B1.class B2.class
-rw-rw-r-- 1 mo mo 198 Jun 7 20:46 B1.class
-rw-rw-r-- 1 mo mo 213 Jun 7 20:46 B2.class
If you look at the generated byte codes, you will see.
% javap -c B1
Compiled from B1.java
public synchronized class B1 extends java.lang.Object
/* ACC_SUPER bit set */
{
public boolean b;
public B1();
}
Method B1()
0 aload_0
1 invokespecial #12 <Method java.lang.Object()>
4 return
javap -c B2
Compiled from B2.java
public synchronized class B2 extends java.lang.Object
/* ACC_SUPER bit set */
{
public boolean b;
public B2();
}
Method B2()
0 aload_0
1 invokespecial #12 <Method java.lang.Object()>
4 aload_0
5 iconst_0
6 putfield #14 <Field boolean b>
9 return
The generated B1 constructor looks like this:
B1() {
super();
return
}
While the B2 constructor looks like this:
B2() {
super();
this.b = false;
return
}
So the autogenerated code is a little bit different.
It does not seem like this will matter much in real
code. It is only a couple of bytes. I am sure a JIT
would remove any runtime cost.
Mo Dejong
Red Hat Inc.
----------------------------------------------------------------------
To UNSUBSCRIBE, email to [EMAIL PROTECTED]
with a subject of "unsubscribe". Trouble? Contact [EMAIL PROTECTED]