The assignment expressions. The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value.

Slides:



Advertisements
Similar presentations
METHOD OVERRIDING Sub class can override the methods defined by the super class. Overridden Methods in the sub classes should have same name, same signature.
Advertisements

Classes  All code in a Java program is part of a class  A class has two purposes  Provide functions to do work for the programmer  Represent data.
Numeric literals and named constants. Numeric literals Numeric literal: Example: A numeric literal is a constant value that appears in a Java program.
Java Intro. A First Java Program //The Hello, World! program in Java public class Hello { public static void main(String[] args) { System.out.println("Hello,
Operator. Assignation (=). The assignation operator serves to assign a value to a variable. a = 5; It is necessary to emphasize that the assignation operation.
Chapter 2: Java Fundamentals Java Program Structure.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Nested conditional statements. Previously discussed Conditional statements discussed so far: Syntax of the if-statement: if-statement if-else-statement.
The switch statement: an N-way selection statement.
Writing algorithms using the while-statement. Previously discussed Syntax of while-statement:
Writing algorithms using the for-statement. Programming example 1: find all divisors of a number We have seen a program using a while-statement to solve.
Programming a computer. What does programming a computer mean ? Programming a computer: Since a computer can only execute machine instructions (encoded.
The break and continue statements. Introduction There are 2 special statements that can affect the execution of loop statements (such as a while-statement)
Shorthand operators.
The character data type char
The string data type String. String (in general) A string is a sequence of characters enclosed between the double quotes "..." Example: Each character.
The Rectangle Method. Introduction Definite integral (High School material): A definite integral a ∫ b f(x) dx is the integral of a function f(x) with.
Parameter passing mechanism: pass-by-value. Introduction In the last webpage, we discussed how to pass information to a method I have kept it (deliberately)
The dangling-else ambiguity. Previously discussed The Java compiler (translator) consider white space characters (i.e., SPACE, TAB and New line) as insignificant.
Floating point numerical information. Previously discussed Recall that: A byte is a memory cell consisting of 8 switches and can store a binary number.
The scope of local variables. Murphy's Law The famous Murphy's Law says: Anything that can possibly go wrong, does. (Wikipedia page on Murphy's Law:
Boolean expressions, part 2: Logical operators. Previously discussed Recall that there are 2 types of operators that return a boolean result (true or.
Integer numerical data types. The integer data types The integer data types use the binary number system as encoding method There are a number of different.
1 Variables. 2 Receipt example What's bad about the following code? public class Receipt { public static void main(String[] args) { // Calculate total.
Working with arrays (we will use an array of double as example)
Introduction to programming in the Java programming language.
Assignment statements using the same variable in LHS and RHS.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
1 BUILDING JAVA PROGRAMS CHAPTER 2 PRIMITIVE DATA AND DEFINITE LOOPS.
Mixing integer and floating point numbers in an arithmetic operation.
VARIABLES Programmes work by manipulating data placed in memory. The data can be numbers, text, objects, pointers to other memory areas, and more besides.
Introduction to Methods. Previously discussed There are similarities in make up of that can help you remember the construct of a class a class in the.
The Bisection Method. Introduction Bisection Method: Bisection Method = a numerical method in Mathematics to find a root of a given function.
Integer numerical data types. The integer data types (multiple !) The integer data types use the binary number system as encoding method There are a number.
Using the while-statement to process data files. General procedure to access a data file General procedure in computer programming to read data from a.
Chapter 2 topics Concept # on Java Subset Required for AP Exam print and println10. Testing of output is restricted to System.out.print and System.out.println.
Copyright 2010 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
This will all add up in the end. Assignment operator =Simple Assignment operator Arithmetic Operators +Additive operator – Subtraction operator * Multiplication.
The while-statement. The loop statements in Java What is a loop-statement: A loop-statement is a statement that repeatedly executes statements contained.
Arithmetic expressions containing Mathematical functions.
Building java programs, chapter 3 Parameters, Methods and Objects.
Reading input from the console input. Java's console input The console is the terminal window that is running the Java program I.e., that's the terminal.
Introduction to array: why use arrays ?. Motivational example Problem: Write a program that reads in and stores away 5 double numbers After reading in.
Creating Web Services Presented by Ashraf Memon Presented by Ashraf Memon.
Working with floating point expressions. Arithmetic expressions Using the arithmetic operators: and brackets (... ), we can construct arithmetic expression.
Boolean expressions, part 1: Compare operators. Compare operators Compare operators compare 2 numerical values and return a Boolean (logical) value A.
Simple algorithms on an array - compute sum and min.
The ++ and -- expressions. The ++ and -- operators You guessed it: The ++ and -- are operators that return a value.
Methods.
AP Computer Science A – Healdsburg High School 1 Unit 9 - Parameter Passing in Java.
Computer Science A 1. Course plan Introduction to programming Basic concepts of typical programming languages. Tools: compiler, editor, integrated editor,
Methods What is a method? Main Method the main method is where a stand alone Java program normally begins execution common compile error, trying.
Invoking methods in the Java library. Jargon: method invocation Terminology: Invoking a method = executing a method Other phrases with exactly the same.
Programming Principles Operators and Expressions.
The if-else statement. Introducing the if-else statement Programming problem: Re-write the a,b,c-formula program to solve for complex number solutions.
 Array ◦ Single & Multi-dimensional  Java Operators ◦ Assignment ◦ Arithmetic ◦ Relational ◦ Logical ◦ Bitwise & other.
August 6, Operators. August 6, Arithmetic Operators.
Primitive Data, Variables, Loops (Maybe)
Operators and Expressions
OPERATORS (1) CSC 111.
Computing Adjusted Quiz Total Score
An Introduction to Java – Part I, language basics
The Boolean (logical) data type boolean
Java Intro.
class PrintOnetoTen { public static void main(String args[]) {
Chapter 2: Java Fundamentals
The for-statement.
Review of Previous Lesson
Methods/Functions.
Presentation transcript:

The assignment expressions

The assignment operator in an assignment statement We have seen the assignment statement: Effect: var = expr; Stores the value of expression expr in the variable named var

The assignment expression Consider the following arithmetic expression: int a = 3; int b = 4; Arithmetic expression: a + b Returns the result: 7

The assignment expression (cont.) Definition: assignment expression: Result returned by an assignment expression: var = expr Result of var = expr is equal to the value of expr

The assignment expression (cont.) Note: The assignment operator will (still) update the value stored inside the variable var In addition to updating the variable. the assignment operator also returns a value

The assignment expression (cont.) Example: public class AssignExpr01 { public static void main(String[] args) { int a; a = 0; System.out.println(a); System.out.println(a = 2); // Prints 2 System.out.println(a); // Prints 2 }

The assignment expression (cont.) Explanation: System.out.println(a = 2) will print the value between its brackets The expression between the brackets is: which is an assignment expression The assignment expression returns the value 2 Therefore, System.out.println will print the value 2 a = 2

The assignment expression (cont.) Example Program: (Demo above code) –Prog file: AssignExpr01.java How to run the program: Right click on link and save in a scratch directory To compile: javac AssignExpr01.java To run: java AssignExpr01

The result returned by the assignment operators We have seen many different assignment operators: = += -= *= /= %=

The result returned by the assignment operators (cont.) Result returned by the assignment operators: Operator Example Returned value =a = 22 +=a += 2a + 2 -= a -= 2a - 2 *=a *= 2a * 2 /= a /= 2a / 2 %=a %= 2a % 2

The result returned by the assignment operators (cont.) Example: public class AssignExpr02 { public static void main(String[] args) { int a; a = 5; System.out.println(a = 2); a = 5; System.out.println(a += 2); a = 5; System.out.println(a -= 2); a = 5; System.out.println(a *= 2); a = 5; System.out.println(a /= 2); a = 5; System.out.println(a %= 2); }

The result returned by the assignment operators (cont.) Output:

The result returned by the assignment operators (cont.) Example Program: (Demo above code) –Prog file: AssignExpr02.java How to run the program: Right click on link and save in a scratch directory To compile: javac AssignExpr02.java To run: java AssignExpr02

Priority and associativity of the assignment operators Priority of the assignment operators: All of the assignment operators have the lowest priority among all operators in Java

Priority and associativity of the assignment operators Associativity of the assignment operators: All of the assignment operators are right associative Meaning: When there are multiple assignment operators in an expression, the expression is evaluate from right to left

Priority and associativity of the assignment operators (cont.) Example: cascaded assignment public class AssignExpr03 { public static void main(String[] args) { int a, b, c; c = b = a = 4 + 2; // Cascade assignment System.out.println(a); // Prints 6 System.out.println(b); // Prints 6 System.out.println(c); // Prints 6 }

Priority and associativity of the assignment operators (cont.) Explanation: c = b = a = 4 + 2; is evaluated as follows: c = b = a = 4 + 2; higher priority ^^^^^ Reduces to: c = b = a = 6; from right to left ^^^^^ assigns 6 to variable a and returns 6 Reduces to: c = b = 6; from right to left ^^^^^ assigns 6 to variable b and returns 6 Reduces to: c = 6; assigns 6 to variable c (and returns 6) ^^^^^

Priority and associativity of the assignment operators (cont.) Example Program: (Demo above code) –Prog file: AssignExpr03.java How to run the program: Right click on link and save in a scratch directory To compile: javac AssignExpr03.java To run: java AssignExpr03

Exercise What will the following Java program print: public class AssignExpr04 { public static void main(String[] args) { int a, b, c; a = 1; b = 1; c = 2; c *= b -= a += 1 + 2; System.out.println(a); System.out.println(b); System.out.println(c); }

Exercise (cont.) Answer:

Priority and associativity of the assignment operators (cont.) Example Program: (Verify for yourself...) –Prog file: AssignExpr04.java How to run the program: Right click on link and save in a scratch directory To compile: javac AssignExpr04.java To run: java AssignExpr04