Null check vs try/catch when 99% of the time object is not null

Jags

Normally I prefer null check. But in current scenario I know that most of the time my if condition will pass and there are few legitimate scenario where object may be null.

Also, load is huge (about 5 million calls / hour)

Now I trying to find which way is better from performance perspective. Already checked try/catch vs null check in java but my case is unique.

Also checked Which is faster, try catch or if-else in java (WRT performance) but both this one and above ones are in generic context where knowledge of pass/fail ratio is not available.

public void process(Job job) {
    //... some code which processes job

    SubJob subJob = job.getSubJob();
    if(subJob != null) {  // 99% of the time this will pass
        //.. do something
    }               
}

try/catch version

public void process(Job job) {
    //... some code which processes job

    SubJob subJob = job.getSubJob();
    try {
        //.. do something  
    }catch(NullPointerException e) { //This may occure only 1% of the time. 
        //...   
    }               
}

Update:

Winner is null check. In Try/catch, internally JVM will do null check and throw NPE anyway and on top of that exception handling in JVM (creation of stack etc) will be overhead. Also as per another answer, modern CPUs are intelligent enough to handle these scenario with good prediction which in my unique case will always work in favor.

I also wrote program (posted below under my name) and results are clearly indicating that null check is way better on my AMD processor.

Thank you folks for guiding me.

wero

TL;DR: If you don't do the null check in your code, the runtime will insert one for you anyway. Null checks almost always have zero cost.


You need to view this problem from the perspective of HotSpot or an optimizing JIT compiler:

When you call a method on an object variable

someObject.callMethod()

then the runtime needs to throw a NPE if the variable is null (Pseudo ASM):

check someObject ref not null else throw NPE
invoke 'callMethod' on 'someObject' 

Now sometimes the runtime can be sure that a variable is not null. This analysis is called null check elimination.

-- no need: check someObject ref not null else throw NPE
invoke 'callMethod' on 'someObject' 

The clue is that your check in the Java source code

if (someObject != null)

is good enough to prove to the runtime that the variable is not null.

The rationale:

Always prefer a null check over catching a NPE. If you don't do the null check then the runtime will insert it for you.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

Null check vs try/catch when 99% of the time object is not null

分類Dev

Null object design pattern Vs null object check

分類Dev

Javascript Date() object with NULL Time

分類Dev

Check for non null array during filtering an object

分類Dev

Which is better when check a list is null : not null or use Any

分類Dev

Check if a String is null or empty when it's null sometimes and sometimes not

分類Dev

How to check for NULL when mapping nested JSON?

分類Dev

(obj == null)vs(null == obj)?

分類Dev

Object.create vs. Object.create()vs。Object.create(null)

分類Dev

Linq to SQL check for null on object before accessing property

分類Dev

how to check sqlite column contain null object reference?

分類Dev

java.util.Objects.isNull vs object == null

分類Dev

Check if CGRect null in getter

分類Dev

Check for NULL or empty '' in function?

分類Dev

If statement with a null check

分類Dev

Check, if a variable is null in a view

分類Dev

Check '!= null" not working

分類Dev

Conditional statement for null check

分類Dev

object == nullまたはnull == object?

分類Dev

Issue with typing object TypeScript - when using a predefined type with null

分類Dev

How to handle null exception when converting from object to string?

分類Dev

C# 6 null propagation what value is set when object is null

分類Dev

What is a "null" object in Mockito?

分類Dev

checking an object for null element

分類Dev

Accessing a variable in an object contained within another object when the child object is null

分類Dev

How to check if more than one value is null at the same time SQL Server

分類Dev

check null,empty or undefined angularjs

分類Dev

null check in try-with-resources

分類Dev

check null,empty or undefined angularjs

Related 関連記事

  1. 1

    Null check vs try/catch when 99% of the time object is not null

  2. 2

    Null object design pattern Vs null object check

  3. 3

    Javascript Date() object with NULL Time

  4. 4

    Check for non null array during filtering an object

  5. 5

    Which is better when check a list is null : not null or use Any

  6. 6

    Check if a String is null or empty when it's null sometimes and sometimes not

  7. 7

    How to check for NULL when mapping nested JSON?

  8. 8

    (obj == null)vs(null == obj)?

  9. 9

    Object.create vs. Object.create()vs。Object.create(null)

  10. 10

    Linq to SQL check for null on object before accessing property

  11. 11

    how to check sqlite column contain null object reference?

  12. 12

    java.util.Objects.isNull vs object == null

  13. 13

    Check if CGRect null in getter

  14. 14

    Check for NULL or empty '' in function?

  15. 15

    If statement with a null check

  16. 16

    Check, if a variable is null in a view

  17. 17

    Check '!= null" not working

  18. 18

    Conditional statement for null check

  19. 19

    object == nullまたはnull == object?

  20. 20

    Issue with typing object TypeScript - when using a predefined type with null

  21. 21

    How to handle null exception when converting from object to string?

  22. 22

    C# 6 null propagation what value is set when object is null

  23. 23

    What is a "null" object in Mockito?

  24. 24

    checking an object for null element

  25. 25

    Accessing a variable in an object contained within another object when the child object is null

  26. 26

    How to check if more than one value is null at the same time SQL Server

  27. 27

    check null,empty or undefined angularjs

  28. 28

    null check in try-with-resources

  29. 29

    check null,empty or undefined angularjs

ホットタグ

アーカイブ