Monday, December 17, 2012

Use of & in Java

When we use && in conditional statement(if), we know that, if the first condition is false then it won't execute the second condition.

But if we use & instead of && in conditional statement(if), then it will execute the second condition though the first condition is false:

public class TypoMistake {

    public static void main(String[] args) {
       
        boolean isTrue = false;
       
        if(isTrue & isTrue()){
            System.out.println("I am in second for loop");
        }
    }
   
    public static boolean isTrue(){
        System.out.println("I am in isTrue() method");
        return true;
    }

}

Output:
I am in isTrue() method