Skip to main content

if(null!=list) v.s. if(list!=null)

1 reply [Last post]
tamerkarakan
Offline
Joined: 2008-01-23
Points: 0

These codes produce different disassemblies code. This behaviour for performance or thread optimization?!

import java.util.Random;
import java.util.ArrayList;

class iff {

Random random = new Random();
ArrayList list = new ArrayList();
public iff(){

if(random!=null){
random = new Random();
}
if(list!=null)
list=new ArrayList();
}

}

class iff extends java.lang.Object{
java.util.Random random;

java.util.ArrayList list;

public iff();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."":()V
4: aload_0
5: new #2; //class java/util/Random
8: dup
9: invokespecial #3; //Method java/util/Random."":()V
12: putfield #4; //Field random:Ljava/util/Random;
15: aload_0
16: new #5; //class java/util/ArrayList
19: dup
20: invokespecial #6; //Method java/util/ArrayList."":()V
23: putfield #7; //Field list:Ljava/util/ArrayList;
26: aload_0
27: getfield #4; //Field random:Ljava/util/Random;
30: ifnull 44
33: aload_0
34: new #2; //class java/util/Random
37: dup
38: invokespecial #3; //Method java/util/Random."":()V
41: putfield #4; //Field random:Ljava/util/Random;
44: aload_0
45: getfield #7; //Field list:Ljava/util/ArrayList;
48: ifnull 62
51: aload_0
52: new #5; //class java/util/ArrayList
55: dup
56: invokespecial #6; //Method java/util/ArrayList."":()V
59: putfield #7; //Field list:Ljava/util/ArrayList;
62: return

}

----------------------------------------------------------------------------------------------------------
import java.util.Random;
import java.util.ArrayList;

class iff {

Random random = new Random();
ArrayList list = new ArrayList();
public iff(){

if(null!=random){
random = new Random();
}
if(null!=list)
list=new ArrayList();
}

}

class iff extends java.lang.Object{
java.util.Random random;

java.util.ArrayList list;

public iff();
Code:
0: aload_0
1: invokespecial #1; //Method java/lang/Object."":()V
4: aload_0
5: new #2; //class java/util/Random
8: dup
9: invokespecial #3; //Method java/util/Random."":()V
12: putfield #4; //Field random:Ljava/util/Random;
15: aload_0
16: new #5; //class java/util/ArrayList
19: dup
20: invokespecial #6; //Method java/util/ArrayList."":()V
23: putfield #7; //Field list:Ljava/util/ArrayList;
26: aconst_null
27: aload_0
28: getfield #4; //Field random:Ljava/util/Random;
31: if_acmpeq 45
34: aload_0
35: new #2; //class java/util/Random
38: dup
39: invokespecial #3; //Method java/util/Random."":()V
42: putfield #4; //Field random:Ljava/util/Random;
45: aconst_null
46: aload_0
47: getfield #7; //Field list:Ljava/util/ArrayList;
50: if_acmpeq 64
53: aload_0
54: new #5; //class java/util/ArrayList
57: dup
58: invokespecial #6; //Method java/util/ArrayList."":()V
61: putfield #7; //Field list:Ljava/util/ArrayList;
64: return

}

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
peter__lawrey
Offline
Joined: 2005-11-01
Points: 0

> This behaviour for performance or thread optimization?!
Neither, this is a choice of code style. Do which you find easiest to read.

I you were worried about performance or clarity, I would suggest you delete the if() blocks because they never do anything.