0

I'm doing past paper for exam

public static void triangleTest( int a, int b, int c) 
{
    if ( a > 0 & b > 0 & c > 0 ) 
    { 
        if ( a==b || b==c ); 
        { 
            System.out.println("Equilateral"); 
        }
        else if ( a==b || a==c || b!=c ) 
            System.out.println("Scalene"); 
        }
        else if ( a+b>c && a+c>b || b+c>a ); 
        {
            System.out.println(Isoceles); 
        } 
    }
    else 
    { 
        System.out.println(""); 
    } 
} 

I have been given this code and told to find mistakes, and when i checked the answers

if ( a > 0 & b > 0 & c > 0 ) 

this has been identified as mistake and && were meant to be used instead of & I ran the code and it worked fine with &, so my question would be what's what's the reasoning behind this?

Etan Reisner
  • 77,877
  • 8
  • 106
  • 148

4 Answers4

0

& is a bitwise operator whereas && is a logical and operator.

Principles of short circuit does not apply in bitwise and operator i.e &.

& <-- verifies both operands

&& <-- stops evaluating if the first operand evaluates to false since the result will be false

Aniket Thakur
  • 66,731
  • 38
  • 279
  • 289
  • for boolean arguments would I be correct in saying that doesn't matter however? And that the difference is that & is not short circuited? – Richard Tingle Jan 10 '14 at 12:12
  • `&` also works as a logical and operator. – Keppil Jan 10 '14 at 12:12
  • @RichardTingle Yes principles of short circuit does not apply in bitwise and operator i.e `&`. – Aniket Thakur Jan 10 '14 at 12:15
  • @Keppil Yes it can be used but the terminology is so. `&` is bitwise and `&&` is logical. You can refer to accepted answer of this [question](http://stackoverflow.com/questions/7199666/difference-between-and-in-java). – Aniket Thakur Jan 10 '14 at 12:20
  • @AniketThakur: So maybe a link to that answer/question in the comments would be more appropriate than to copy it here as a new answer? – Keppil Jan 10 '14 at 12:22
  • @Keppil I searched it after seeing your comment. If i had know a similar problem is already present that would be my course of action. – Aniket Thakur Jan 10 '14 at 12:24
0

& evaluates both values

where as && checks the first one if it's false it sets result as false.

Saurabh Sharma
  • 489
  • 5
  • 20
0

If you are using & it will check second condition although first is false. While in && it will not check next condition if first is false.

In some case it may cause exceptions. For ex:

// Some Code

if ( a != null & a.length() > 0 ){
//coding part
}

above code will throw NullPointerException with & if a is null. because it will check second condition every time either first is true or false.

so it will go like this...

// Some Code

if ( a !=null && a.length() > 0 ){
//coding part
}

For more : refer this SO Que

Community
  • 1
  • 1
Not a bug
  • 4,286
  • 2
  • 40
  • 80
0

Short circuiting leads to efficient code

&& is the logical and and it is short circuited. & is the bitwise and. Although for other cases it is very different; for booleans the (practical) difference is just that it doesn't short circuit.

Short circuiting means it skips evaluating conditions that can't affect the outcome. For exampple

if(cheapTest() && expensiveTest() && expensiveTest2())

If the first test is false the expresion must be false, so it can not bother with the expensive tests.

if(FALSE && DONTCARE && DONTCARE)

The reverse is true for | vs ||.

Protection against exceptions

This can be even more important where the second test may raise exceptions if evaluated, for example

if (someObject!=null && someObject.someTest())

In this case if someObject is null then someObject.someTest() never runs, this is a common pattern when null values are expected. Where you to use the bitwise and both tests would run in all cases and if someObject was null you would recieve a NullPointerException

Community
  • 1
  • 1
Richard Tingle
  • 16,906
  • 5
  • 52
  • 77