Booleans in Java

Java has a simple type, called boolean, for logical values. It can have only one of two possible values, true or false. This is the type returned by all relational operators, such as a < b. boolean is also the type required by the conditional expressions that govern the control statements such as if and for.

Example to demonstrate boolean values.


// Demonstrate boolean values.
public class BoolTest {
	public static void main(String args[]) {
		boolean b;
		
		b = false;
		System.out.println("b is "+b);
		b = true;
		System.out.println("b is "+b);
		
		// a boolean value can control the if statement
		
		if(b)
			System.out.println("This is executed.");
		b = false;
		if(b)
			System.out.println("This is not executed.");
		//outcome of relational operator is boolean value 
		System.out.println("10 > 9 is " + (10 > 9));
	}

}
Output

b is false
b is true
This is executed.
10 > 9 is true


Post a Comment

0 Comments