Java: If Statements

Instructor: Lonny Meinecke

Lonny was once a software programmer (video game industry). He now teaches psychology at King University. He has a bachelor's in IT and a PhD in psychology.

This lesson will explain how to use if statements in Java. A few examples of how to create short Java programs that use these statements will also be provided to demonstrate their usage.

The Java If Statement

What is a Java if statement? Well, even though it is possible to write an application that just performs a set of steps, that wouldn't be very interesting or allow us to do very much. Life is full of choices, and the outcome of each choice is conditional on which choice we make. When we encounter an 'if', something may or may not happen. Life's many 'ifs' are a lot like forks in the road, and it's those forks which may or may not be taken that make life interesting.

Computer applications want to be interesting too, so computer languages like Java offer forks in their roads too, which are called conditional statements. So, think of the Java if statement like one of life's forks in the road --a decision must be made because the program can't take both roads.

The Java if statement is like a fork in the road
Fork in the road

A Java Refresher

Before we jump right into the Java if statement, let's refresh what we know about creating a minimal Java program, shall we?

Each Java application should have three things:

  • A class definition (every application must begin with one)
  • A main method (so the Java runtime engine knows where to start)
  • Some comments (to help you and others understand the code)

Everything else in your Java application can then achieve something that you, the author, have in mind. Sound good?

Examples of If statements in Java

Now we are ready to demonstrate what the Java if statement can do for us, which is to evaluate an expression to see if it is true or false (we call that a Boolean expression). By reducing any expression to a result of true or false, the Java program can decide very clearly what to do next. The format is simply:

if(Boolean_expression) {
  // Anything here will be executed if the expression is true
// For example, we can see if the variable x is less than 10 like so:
if(x < 10) {
  // do something

Using the if statement with numbers is easy enough since we can tell if numbers are less than, equal to, or more than each other. But you may ask how a string expression like 'blue' can be reduced to true or false. Good question! The letters that make up a string are actually numbers in the computer. Java provides a way to compare strings too. Here is an example in which the user may try to guess the author's favorite color.

  * @author Java Jill
  * @version 1.0
public class IfExample1 {
  public static void main(String[] args) {
   try {
    String s1 = args[0];
    System.out.println("Your guess was: + s1);
    if(s1.equals("red")) {
     System.out.println("Your guess is my favorite color!");
     System.out.println("Good job");
   } catch (Exception e) {
    System.out.println("Error - please include an argument");

Let's look at how this Java application works.

  • The application class is named IfExample1.
  • Inside the main() method, we assign the user's command line argument to a local variable (s1) to make things easier to read and process, and because the String object type has a nifty method to compare strings with.
  • Then we print out the user's guess with the System println() method.
  • Next, we evaluate the string variable s1 to see if it's true (if the string contains the word 'red').
  • If it does, we let the user know he or she guessed correctly.

Note that we do some basic error checking, since the user might not remember to type in the color, and this would generate an exception in Java. This is often a good practice. Anyway, that's it!

The output of this first example would look like this - first without the color and then with the color entered on the command line too:

Output of
Output of

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account