The Assignment Operator The Assignment Operator An Introduction with Examples in Java Prof. David Bernstein James Madison University Computer Science Department bernstdh@jmu.edu Review 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.) 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 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) Java vs. Python - Important Differences Statements: In Python, statements are terminated with the end-of-line character In Java, statements are terminated with a ; character White Space: In Python, white space is signficant In Java, extra white space is insignificant (e.g., the statement ends with the ;, wherever it is, even on another line) Comments: In Python, everything after a # is an in-line comment In Java, everything after a // is an in-line comments and everything between a /* and a */ is a block comment Assignment in Java 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.) Some Observations: A variable can only contain one value at a time A variable can only contain a value of the same type A Java Example:
int i;
i = 5; // i contains the value 5
i = 27; // i now contains the value 27
Assignment in Java (cont.) 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 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 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 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.) 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 Purpose: Indicate that a value can only be assigned to a variable once Syntax: final type variable [, variable]... ; Examples: final boolean CORRECT; final double BUDGET; final int CAPACITY; The final Modifier in Java (cont.) 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 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