Chapter 3 - Java for Beginners Course

Statements

Statements are complete units of execution in a Java application, and to use the same analogy as the Oracle documentation, they resemble sentences in natural languages.

Statements can be grouped in blocks of zero or more statements inside braces ({ …​ }), as we’ve seen in the examples of method declarations in Chapter 1. We’ll be using more examples of blocks of code in the next sections related to Control Flow statements.

Types of Statements

Declaration Statements

We’ve used them multiple times before and are the statements we use to declare a variable, for example:

int number;
Door door;

As mentioned in a previous section, we can mix a declaration and an assignment statement in a single line for simplicity, for example, we could do the following:

int number = 1;

Expression Statements

As mentioned in the previous section, different types of expressions can be converted into statements by adding a semi-colon at the end, for example:

// object creation statement
door = new Door();
// method invocation statements
door.lockDoor();
System.out.println("Is door locked? " + door.locked);
// assignment statement
number = 1;
// decrement statement
number--;
// increment statement
number++;

Control Flow Statements

In general, the statements will be executed in the order they are written in the Java code. However, there will be occasions where we need to decide whether a certain block of code needs to be executed or not, or where we need to repeat a block of code several times.

Control flow statements allow us to do this and will be covered in the following sections.

So far in this course, we haven’t used control flow statements.
Code in GitHub

Get the code for this tutorial using the links below.

Project Repo
Download code for this step
Main class for this step
Dependencies

This is a list of recommended tutorials or courses that might be useful before starting this one.

Contents
Welcome to the Course!
Course Introduction
Chapter 1 - Building Blocks
Quick introduction to Java Variables Classes And Objects Class Example - Defining a class Object Examples - Creating instances Java Application Example - Running our first app Accessing class members - The dot operator Packages - Organizing the code
Chapter 2 - Primitives and Operators
Primitives Arithmetic Operators Assignment Operator Unary Operators Equality and Relational Operators Conditional Operators
Chapter 3 - Statements and Control Flow
Expressions Statements If-Then Statement If-Then-Else Statement More If Statements Switch Statement While and Do-While Statements For Statement Branching Statements Exception Handling
Chapter 4 - Code Example
Example Project - A Simple Vending Machine Adding money Delivering Items Giving Change
Chapter 5 - Classes and Interfaces
Introduction Access Level Modifiers Class Declaration - Class, Methods and Fields Class Declaration - Constructors Inheritance Basics Inheritance - Constructors Inheritance - Methods and Fields Polymorphism Abstract Classes and Methods Interfaces Static Class Members Class Composition Final Classes and Class Members Generic Classes
Chapter 6 - Base Object Behaviors
Introduction Type Comparison Type Casting Object Equality - The Contract Object Equality - Common Pitfalls Object String Representation Garbage Collection Object Comparison Primitive Wrappers and Autoboxing
Chapter 7 - Data Structures
Introduction Arrays - Declaration and Creation Arrays - Basic Operations Core Collection Interfaces List and ArrayList - Basic Operations ArrayList Internals Introduction to Hash Tables Map and HashMap - Basic Operations Set and HashSet - Basic Operations
Chapter 8 - Anonymous classes and lambdas
Introduction Filtering a List Anonymous Classes Lambdas Built-in Functional Interfaces
Chapter 9 - Streams
Introduction Creating Streams Intermediate Operations Terminal Operations