Java Booleans

Java Booleans

A boolean in Java is a data type that can store only two values:

  • true

  • false

Booleans are commonly used in conditions, decision making, and logical expressions.


✅ Declaring Boolean Variables

boolean isJavaEasy = true;
boolean isRaining = false;

System.out.println(isJavaEasy);
System.out.println(isRaining);


✅ Boolean Expressions (Comparison)

Boolean values are often returned from expressions involving comparison operators:

int x = 10;
int y = 20;

System.out.println(x > y); // false
System.out.println(x < y); // true
System.out.println(x == 10); // true


✅ Boolean with Logical Operators

Operator Meaning Example
&& AND true only if both conditions are true
! NOT reverses boolean value

Example:

boolean a = true;
boolean b = false;

System.out.println(a && b); // false
System.out.println(a || b); // true
System.out.println(!a); // false


✅ Boolean in If-Else

Booleans are mostly used for decision making:

int age = 18;
boolean canVote = age >= 18;

if (canVote) {
System.out.println("You are eligible to vote.");
} else {
System.out.println("You are not eligible to vote.");
}


🚀 Boolean Method Example

Some methods also return boolean:

String name = "Java Programming";

System.out.println(name.contains("Java")); // true
System.out.println(name.startsWith("Python")); // false


⭐ Full Example

public class Main {
public static void main(String[] args) {

boolean isStudent = true;
int a = 5, b = 10;

System.out.println("Boolean Value: " + isStudent);
System.out.println("a < b: " + (a < b));
System.out.println("a == b: " + (a == b));
System.out.println("Logical AND: " + (a < b && a > 2));
System.out.println("Logical OR: " + (a > b || a == 5));
}
}


📌 Summary

Feature Description
Values true, false
Type Primitive
Common Uses Comparisons, loops, if-else
Operations Logical (&&, `

CodeCapsule

Sanjit Sinha — Web Developer | PHP • Laravel • CodeIgniter • MySQL • Bootstrap Founder, CodeCapsule — Student projects & practical coding guides. Email: info@codecapsule.in • Website: CodeCapsule.in

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *