Most efficient null avoidance

F

Frank Cisco

What's the most efficient way to avoid nulls when getting objects from array

eg.

String eg = array.get("test");
if(eg==null){eg="";} //without this then null pointer gets thrown...

if(!eg.equals("EXAMPLE")){ // ...here
//do something
}

or maybe...

String eg = array.get("test")==null?"":array.get("test"); //neater but 2
lookups with this though

or maybe...

String eg=null;
if((eg=array.get("test"))!=null){
//do stuff
}


I take it the 3rd one is the most efficient?
 
A

Arne Vajhøj

Frank said:
What's the most efficient way to avoid nulls when getting objects from array

eg.

String eg = array.get("test");
if(eg==null){eg="";} //without this then null pointer gets thrown...

if(!eg.equals("EXAMPLE")){ // ...here
//do something
}

or maybe...

String eg = array.get("test")==null?"":array.get("test"); //neater but 2
lookups with this though

or maybe...

String eg=null;
if((eg=array.get("test"))!=null){
//do stuff
}

I take it the 3rd one is the most efficient?

I would go for:

String eg = array.get("test");
if(eg != null) {
//do stuff
}

because I believe it most clearly expresses the logic.

BTW, I would never never never name a variable of something
that is not an array as "array" (I would probably not name
an array as "array", but that would be a minor issue).

Arne
 
F

Frank Cisco

Arne Vajhøj said:
I would go for:

String eg = array.get("test");
if(eg != null) {
//do stuff
}

because I believe it most clearly expresses the logic.

BTW, I would never never never name a variable of something
that is not an array as "array" (I would probably not name
an array as "array", but that would be a minor issue).

Arne

cheers, bit daft now i think of it :)

thats the one
 
D

Daniel Pitts

Frank said:
What's the most efficient way to avoid nulls when getting objects from array

eg.

String eg = array.get("test");
if(eg==null){eg="";} //without this then null pointer gets thrown...

if(!eg.equals("EXAMPLE")){ // ...here
//do something
}

or maybe...

String eg = array.get("test")==null?"":array.get("test"); //neater but 2
lookups with this though

or maybe...

String eg=null;
if((eg=array.get("test"))!=null){
//do stuff
}


I take it the 3rd one is the most efficient?
What about being the most correct? Shouldn't that be the first
consideration? After that, all of the above are practically the same in
speed.

Note that "" and null have different semantic meanings, and should not
generally be mixed and matched unless you have good justification.

How about this:
final String eg = array.get("test");
// NPE is avoided because string literal is never null:
if ("EXAMPLE".equals(eg)) {
// do something.
}

Or,
if (myString != null && myString.equals(otherString)) {
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top