- 1Java Exceptions
- 2Java Keywords
- 3Java abstract Keyword
- 4Java assert Keyword
- 5Java boolean Keyword
- 6Java break Keyword
- 7Java byte Keyword
- 8Java case Keyword
- 9Java catch Keyword
- 10Java char Keyword
- 11Java class Keyword
- 12Java const Keyword
- 13Java continue Keyword
- 14Java default Keyword
- 15Java do Keyword
- 16Java double Keyword
- 17Java else Keyword
- 18Java enum Keyword
- 19Java extends Keyword
- 20Java final Keyword
- 21Java finally Keyword
- 22Java float Keyword
- 23Java for Keyword
- 24Java goto Keyword
- 25Java if Keyword
- 26Java implements Keyword
- 27Java import Keyword
- 28Java instanceof Keyword
- 29Java int Keyword
- 30Java interface Keyword
- 31Java long Keyword
- 32Java native Keyword
- 33Java new Keyword
- 34Java null Keyword
- 35Java package Keyword
- 36Java private Keyword
- 37Java protected Keyword
- 38Java public Keyword
- 39Java return Keyword
- 40Java short Keyword
- 41Java static Keyword
- 42Java strictfp Keyword
- 43Java super Keyword
- 44Java switch Keyword
- 45Java synchronized Keyword
- 46Java this Keyword
- 47Java transient Keyword
- 48Java try Keyword
- 49Java void Keyword
- 50Java volatile Keyword
- 51Java while Keyword
- 52Java String Methods - Syntax and Description
- 53Java String
charAt()
method - 54Java String
codePointAt()
method - 55Java String
codePointBefore()
method - 56Java String
codePointCount()
method - 57Java String
compareTo()
method - 58Java String
compareToIgnoreCase()
method - 59Java String
concat()
method - 60Java String
contains()
method - 61Java String
contentEquals()
method - 62Java String
copyValueOf()
method - 63Java String
endsWith()
method - 64Java String
equals()
method - 65Java String
equalsIgnoreCase()
method - 66Java String
format()
method - 67Java String
getBytes()
method - 68Java String
getChars()
method - 69Java String
hashCode()
method - 70Java String
indexOf()
method - 71Java String
intern()
method - 72Java String
isEmpty()
method - 73Java String
join()
method - 74Java String
lastIndexOf()
method - 75Java String
length()
method - 76Java String
matches()
method - 77Java String
offsetByCodePoints()
method - 78Java String
regionMatches()
method - 79Java String
replace()
method - 80Java String
replaceAll()
method - 81Java String
replaceFirst()
method - 82Java String
split()
method - 83Java String
startsWith()
method - 84Java String
subSequence()
method - 85Java String
substring()
method - 86Java String
toCharArray()
method - 87Java String
toLowerCase()
method - 88Java String
toString()
method - 89Java String
toUpperCase()
method - 90Java String
trim()
method - 91Java String
valueOf()
method - 92Java ArrayList Methods - Complete Reference with Syntax and Description
- 93Java LinkedList Methods - Complete Reference with Syntax and Description
- 94Java HashMap Methods - Syntax and Descriptions
Java if
Keyword
Usage and Examples
if
Keyword in Java
The if
keyword in Java is your first step into controlling the flow of your program. It allows your code to make decisions — a fundamental aspect of programming logic.
Whenever you want something to happen only when a specific condition is true, if
is what you use.
Syntax of if
Statement
if (condition) {
// code to execute if condition is true
}
The condition inside the parentheses must evaluate to a boolean value — true
or false
. If the result is true
, the code block runs. If it's false
, the block is skipped.
Example 1: Basic if
Statement
public class IfExample1 {
public static void main(String[] args) {
int age = 20;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
System.out.println("Check completed.");
}
}
You are eligible to vote.
Check completed.
Explanation:
Here, the condition age >= 18
is true
because age
is 20. Therefore, the message inside the if
block is printed. The program then continues to the next line after the block.
Example 2: Condition Fails
public class IfExample2 {
public static void main(String[] args) {
int age = 16;
if (age >= 18) {
System.out.println("You are eligible to vote.");
}
System.out.println("Check completed.");
}
}
Check completed.
Explanation:
This time, age
is 16. Since age >= 18
is false
, the body of the if
statement is skipped. The program continues to print Check completed.
Example 3: Using Boolean Variables
public class IfExample3 {
public static void main(String[] args) {
boolean isLoggedIn = true;
if (isLoggedIn) {
System.out.println("Welcome back, user!");
}
}
}
Welcome back, user!
Notice how we used a boolean variable directly in the if
condition. If it's true
, the block runs. This pattern is common when checking flags or system states.
Curly Braces: Optional for Single Statements
public class IfExample4 {
public static void main(String[] args) {
int number = 10;
if (number > 5)
System.out.println("Number is greater than 5");
System.out.println("End");
}
}
Number is greater than 5
End
When there's only one statement to execute under an if
condition, Java lets you skip the curly braces. But be cautious — for readability and maintainability, it's usually better to keep them.
Example 5: Nested if
Statements
public class IfExample5 {
public static void main(String[] args) {
int marks = 85;
if (marks >= 50) {
if (marks >= 80) {
System.out.println("Excellent score!");
}
}
}
}
Excellent score!
Nested if
statements are useful when you want to validate one condition only after another one is true. In this case, the program first checks if the score is at least 50, then checks if it’s 80 or more.
Common Mistake: Semicolon After if
if (x > 10);
System.out.println("This will always run!");
A semicolon directly after if
ends the statement prematurely. The following block will run regardless of the condition — a classic beginner mistake.
Best Practices for Using if
in Java
- Always use braces, even for single-line statements, to avoid logic errors.
- Keep your conditions simple and readable.
- Use boolean expressions or variables clearly — avoid nested logic when possible.
- Comment your decision branches, especially if the condition is complex.