- Forward


The Assignment Operator
An Introduction with Examples in Java


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Review
Back Forward
  • Variable:
    • A named space for holding data/information
    • The name is often referred to as the identifier
  • Literal:
    • A representation of a datum
Review (cont.)
Back Forward
  • Operator:
    • A symbol indicating that an operation is to be performed (on one or more operands)
  • Expression:
    • A combination of variables, operators, and literals that represent a single value
  • Statement:
    • The smallest syntactically valid construct that conveys a complete command
Assignment
Back Forward
  • 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)
Assignment in Java
Back Forward
  • Example Expressions (Assuming the Variables Have Been Declared):
    • age = 21
    • initial = 'H'
    • ok = false
  • Example Statements (Assuming the Variables Have Been Declared):
    • age = 21;
    • initial = 'H';
    • ok = false;
Assignment in Java (cont.)
Back Forward
  • An Observation:
    • A variable can only contain one value at a time
  • A Java Example:
    • int i; i = 5; // i contains the value 5 i = 27; // i now contains the value 27
Assignment in Java (cont.)
Back Forward
  • A Common Confusion:
    • Confusing the assignment operator with the notion of "equality"
  • A Result of this Confusion:
    • Attempting to use expressions like 21 = age (which has an inappropriate left-side operand)
  • An Important Detail:
    • The assignment operation evaluates to the value of the right-side operand (so, is an expression)
A Practice to Avoid
Back Forward
  • An Example:
    • int i, j; i = j = 5;
  • Why it Should be Avoided:
    • It can cause confusion in more complicated statements, especially those involving arithmetic operators and relational operators
An Interesting Issue
Back Forward
  • A Question:
    • In the previous example, which assignment is performed first?
  • The General Concept:
    • Associativity - determines whether (in the absence of other determinants) an expression is evaluated from left to right or right to left
  • Associativity of the Assignment Operator in Java:
    • The assignment operator has right to left associativity, so i = j = 5 is equivalent to i = (j = 5)
Type Checking During Assignment
Back Forward
  • Strong Type Checking:
    • Ensuring that the type of the right-side operand is the same as the type of the left-side operand
  • Conversion in Java:
    • Java does not allow implicit losses of precision
    • Java does allow widenings (though you should refrain from using them)
  • An Example:
    • 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 Checking During Assignment (cont.)
Back Forward
  • Typecasting:
    • (type)expression
  • Examples:
      double real; int small; long large; // ... small = (int)large; // ... small = (int)real;
  • Why Typecasting Should be Used Sparingly:
    • Loss of precision
    • Possible loss of magnitude
    • Possible change of sign
The final Modifier in Java
Back Forward
  • Purpose:
    • Indicate that a value can only be assigned to a variable once
  • Syntax: Click here for information.
    • final type variable [, variable]... ;
  • Examples:
    • final boolean CORRECT;
    • final double BUDGET;
    • final int CAPACITY;
The final Modifier in Java (cont.)
Back Forward
  • Name/Identifier Restrictions:
    • Same as for other variables
  • Course Style Guide Requirements:
    • Must be all uppercase
    • Underscores between "words" improve readability
  • Compile-Time Errors:
    • 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
  • A Common Convention:
    • Include the assignment in the declaration
Assignment of Primitive and Reference Types
Back Forward
  • Atomic/Primitive/Fundamental Types:
    • The value of the right-side operand is assigned to the left-side operand
  • Reference Types:
    • The address of the right-side operand is assigned to the left-side operand
There's Always More to Learn
Back -