The Java Tutorials have been written for JDK 8. Examples and practices described in this page don't take advantage of improvements introduced in later releases and might use technology no longer available. See Java Language Changes for a summary of updated language features in Java SE 9 and subsequent releases. See JDK Release Notes for information about new features, enhancements, and removed or deprecated options for all JDK releases.

Expressions, Statements, and Blocks

Now that you understand variables and operators, it's time to learn about expressions , statements , and blocks . Operators may be used in building expressions, which compute values; expressions are the core components of statements; statements may be grouped into blocks.

Expressions

An expression is a construct made up of variables, operators, and method invocations, which are constructed according to the syntax of the language, that evaluates to a single value. You've already seen examples of expressions, illustrated in bold below:

The data type of the value returned by an expression depends on the elements used in the expression. The expression cadence = 0 returns an int because the assignment operator returns a value of the same data type as its left-hand operand; in this case, cadence is an int . As you can see from the other expressions, an expression can return other types of values as well, such as boolean or String .

The Java programming language allows you to construct compound expressions from various smaller expressions as long as the data type required by one part of the expression matches the data type of the other. Here's an example of a compound expression:

In this particular example, the order in which the expression is evaluated is unimportant because the result of multiplication is independent of order; the outcome is always the same, no matter in which order you apply the multiplications. However, this is not true of all expressions. For example, the following expression gives different results, depending on whether you perform the addition or the division operation first:

You can specify exactly how an expression will be evaluated using balanced parenthesis: ( and ). For example, to make the previous expression unambiguous, you could write the following:

If you don't explicitly indicate the order for the operations to be performed, the order is determined by the precedence assigned to the operators in use within the expression. Operators that have a higher precedence get evaluated first. For example, the division operator has a higher precedence than does the addition operator. Therefore, the following two statements are equivalent:

When writing compound expressions, be explicit and indicate with parentheses which operators should be evaluated first. This practice makes code easier to read and to maintain.

Statements are roughly equivalent to sentences in natural languages. A statement forms a complete unit of execution. The following types of expressions can be made into a statement by terminating the expression with a semicolon ( ; ).

  • Assignment expressions
  • Any use of ++ or --
  • Method invocations
  • Object creation expressions

Such statements are called expression statements . Here are some examples of expression statements.

In addition to expression statements, there are two other kinds of statements: declaration statements and control flow statements . A declaration statement declares a variable. You've seen many examples of declaration statements already:

Finally, control flow statements regulate the order in which statements get executed. You'll learn about control flow statements in the next section, Control Flow Statements

A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed. The following example, BlockDemo , illustrates the use of blocks:

About Oracle | Contact Us | Legal Notices | Terms of Use | Your Privacy Rights

Copyright © 1995, 2022 Oracle and/or its affiliates. All rights reserved.

  • A named space for holding data/information
  • The name is often referred to as the identifier
  • A representation of a datum
  • A symbol indicating that an operation is to be performed (on one or more operands)
  • A combination of variables, operators, and literals that represent a single value
  • The smallest syntactically valid construct that conveys a complete command
  • Placing a value into the memory identified by a variable/constant
  • The assignment operator is a binary operator; the left-side operand is a variable/constant and the right-side operand is a literal or an expression (i.e., something that evaluates to a value)
  • initial = 'H'
  • initial = 'H';
  • ok = false;
  • A variable can only contain one value at a time
  • int i; i = 5; // i contains the value 5 i = 27; // i now contains the value 27
  • Confusing the assignment operator with the notion of "equality"
  • Attempting to use expressions like 21 = age (which has an inappropriate left-side operand)
  • The assignment operation evaluates to the value of the right-side operand (so, is an expression)
  • int i, j; i = j = 5;
  • It can cause confusion in more complicated statements, especially those involving arithmetic operators and relational operators
  • In the previous example, which assignment is performed first?
  • Associativity - determines whether (in the absence of other determinants) an expression is evaluated from left to right or right to left
  • The assignment operator has right to left associativity, so i = j = 5 is equivalent to i = (j = 5)
  • Ensuring that the type of the right-side operand is the same as the type of the left-side operand
  • Java does not allow implicit losses of precision
  • Java does allow widenings (though you should refrain from using them)
  • double weight; int age; age = 21.0; // Not allowed (will generate a compile-time error) weight = 155; // Allowed but not recommended (use 155. instead)
  • ( type ) expression
  • Examples: double real; int small; long large; // ... small = (int)large; // ... small = (int)real;
  • Loss of precision
  • Possible loss of magnitude
  • Possible change of sign
  • Indicate that a value can only be assigned to a variable once
  • final type variable [, variable ]... ;
  • final boolean CORRECT;
  • final double BUDGET;
  • final int CAPACITY;
  • Same as for other variables
  • Must be all uppercase
  • Underscores between "words" improve readability
  • An error will be generated if an identifier that is declared to be final is the left-side operand of more than one assignment operator
  • Include the assignment in the declaration
  • The value of the right-side operand is assigned to the left-side operand
  • The address of the right-side operand is assigned to the left-side operand

IMAGES

  1. Assignment Operators in Java with Examples

    insert assignment operator expression java

  2. The Assignment Operator in Java

    insert assignment operator expression java

  3. Java operators with examples

    insert assignment operator expression java

  4. Assignment Operators in Java

    insert assignment operator expression java

  5. Java operators with examples

    insert assignment operator expression java

  6. Assignment Operators in Java

    insert assignment operator expression java

VIDEO

  1. Assignment operators in java

  2. OPERATOR PRECEDENCE || JAVA

  3. assignment operator simple program in java..#java # assignment_operator

  4. Core Java

  5. Assignment operater java Java Programming

  6. Emacs-like VHDL stutter mode in VSCode

COMMENTS

  1. Expressions, Statements, and Blocks (The Java™ Tutorials

    The expression cadence = 0 returns an int because the assignment operator returns a value of the same data type as its left-hand operand; in this case, cadence is an int. As you can see from the other expressions, an expression can return other types of values as well, such as boolean or String .

  2. The Assignment Operator An Introduction with Examples in Java

    The Operation: Placing a value into the memory identified by a variable/constant; The Operator (in Java): = Operands: The assignment operator is a binary operator; the left-side operand is a variable/constant and the right-side operand is a literal or an expression (i.e., something that evaluates to a value)

  3. Java 8

    Assignment Operators Overview Top. The single equal sign = is used for assignment in Java and we have been using this throughout the lessons so far. This operator is fairly self explanatory and takes the form variable = expression; . A point to note here is that the type of variable must be compatible with the type of expression.