In Java (Eclipse), when having a statement such as if (true || false), it will end up true but the question is will the compiler evaluate the second statement if the first is true?
This is of an importance to me because I have an operation I need to do if the variable is null OR it has a certain value.
My statement looks like if (array == null || array[i] < array[j]).
You can see the reason for my question, because if array is set to null then the second statement will produce an error.
So, will the true from array == null suffice or will the compiler evaluate array[i] < array[j]) also?
            Asked
            
        
        
            Active
            
        
            Viewed 1.7k times
        
    12
            
            
         
    
    
        Sharon Dorot
        
- 542
- 1
- 4
- 15
1 Answers
30
            No it won't.
- With boolean operator ||, if first term istruesecond term won't be evaluated.
- With bitwise operator |both terms are evaluated
Similarly...
- With boolean operator &&, if first term isfalsesecond term won't be evaluated
- With bitwise operator &, both terms are evaluated
Java operators docs here.
 
    
    
        Mena
        
- 47,782
- 11
- 87
- 106
- 
                    Thank you! I could have tested it on my expression but reaching the point where I could debug it is far away and it would be a shame to do all that work and end up fixing it. – Sharon Dorot May 09 '14 at 10:15
- 
                    @SharonJDDorot - have a look at the Groovy Console (http://groovyconsole.appspot.com/) - really useful for putting these little snippets in. – Al Sweetman May 09 '14 at 10:18
- 
                    flawless answer @Mena – Gaurav Aug 04 '21 at 16:01