The expression in an if statement must be a boolean expression. : if « Statements « SCJP

Home
SCJP
1.Java Source And Data Type
2.Operators
3.Modifiers
4.Type Casting
5.Statements
6.Object Oriented
7.Thread
8.Utility Classes
9.File
SCJP » Statements » if 
5.7.2.The expression in an if statement must be a boolean expression.
public class MainClass{
    public static void main(String[] argv){
        int y = 5;
        int x = 2;
        if (((x > 3&& (y < 2)) | doStuff()) {
           System.out.println("true");
        }
    }
    static boolean doStuff(){
      return true;
    }
}

public class MainClass{
    public static void main(String[] argv){
        int x = 3;
        if (x = 5) { }  // Won't compile because x is not a boolean!
    }
}
Exception in thread "main" java.lang.Error: Unresolved compilation problem: 
	Type mismatch: cannot convert from int to boolean

	at MainClass.main(MainClass.java:4)
5.7.if
5.7.1.The basic format of an if statement
5.7.2.The expression in an if statement must be a boolean expression.
5.7.3.Use an else block to execute code that is executed under the conditions that the test returns false.
5.7.4.Use if/else in a nested fashion, refining conditions to more specific, or narrower, tests at each point.
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.