4.1 Introduction

This section opens with four foundational concepts of Java: expressions, declarations, code blocks and methods. These four ideas are really the key to writing Java, and the goal of the lesson is to make sure you understand each one before we move on.

Keywords first

Java has roughly 50 reserved keywords. We already met a few of them like int, class or String. You cannot use a keyword to name a variable, a class, a package or a method. For example, declaring int m = 1000; is fine, but trying to declare a variable whose name is itself a keyword is rejected by the compiler.

Expressions are everywhere in Java. They are built from values, variables, operators and methods. Going back to our earlier example, int m = 1000; contains the expression m = 1000: it starts at the variable name and is terminated by the semicolon. Another example is int y = 1000 * m; where 1000 * m is an arithmetic operation embedded in a declaration.

  • Expression: a combination of values, variables, operators and methods that produces a value.
  • Keyword: a reserved name like if, int or class.
  • Method call: an expression that returns a value when invoked, for example System.out.println(...).

We can also nest expressions in control statements. In if (m == 1000) System.out.println("true");, the parentheses host the boolean expression and println is itself a method whose argument is another expression. The next lesson moves on to declarations, whitespace and indentation.

Summary

This lesson introduces four fundamental Java concepts: keywords (reserved words that cannot be used as identifiers), variable declarations, expressions, and statements. Through practical examples, you'll learn how keywords are used to declare variables with specific types like int, and how expressions are constructed from values, variables, operations, and method calls. These core building blocks form the foundation for all Java programming.

Key points

  • Keywords are reserved words in Java that cannot be used as variable names or identifiers—examples include int, class, and String
  • Variable declarations combine a type keyword (like int) with a variable name and optionally an initial value using the assignment operator
  • Expressions are constructed from values, variables, operations (like arithmetic or comparison), and method calls, bound by statement delimiters
  • Statements extend expressions by adding control flow logic, such as if conditions that evaluate expressions and execute code based on the result
  • The syntax pattern for declarations follows: type name = value; where the expression starts with the variable name and ends at the semicolon
  • Methods can be invoked within expressions to perform operations and return values that participate in larger computations

FAQ

What are Java keywords and why can't we use them as variable names?

Keywords are reserved words in Java with special meaning in the language syntax. Since they're used for language constructs like variable types (int, String) and control flow (if, class), using them as variable names would create ambiguity and break the parser. The Java language reserves approximately 50 keywords for its core functionality.

What is the difference between a declaration and an expression in Java?

A declaration introduces a new variable or entity into the program with a specified type and name (for example: int m). An expression combines values, variables, and operations to produce a result (for example: m * 1000 or if m > 4000). A declaration can include an expression as its initial value.

Can methods be used inside expressions?

Yes, methods are an important part of expressions. You can invoke a method within an expression, and its return value becomes part of the larger expression or assignment. This allows you to perform complex operations by combining multiple method calls and operations into a single statement.