Download presentation
Presentation is loading. Please wait.
1
Assignments, Expressions & Operators
Joe McCarthy CSS 161: Fundamentals of Computing
2
CSS 161: Fundamentals of Computing
Recap: Intro to Java Class Method main() Identifier Reserved word Variable Declaration Assignment Operator System.out.println() Output: = 30 CSS 161: Fundamentals of Computing
3
CSS 161: Fundamentals of Computing
Memory organization CSS 161: Fundamentals of Computing
4
CSS 161: Fundamentals of Computing
5
Assignments & Expressions
Syntax: variable = expression Semantics: Evaluate expression, assign value to variable Expression: Constant (1, 1.23, 3.45e6, ‘a’, “css161”, true, false) final static int COURSENUMBER = 161; Variable (totalScore) expression operator expression Operators: *, /, % +, - … All lower case Convention: ALL CAPS for constants Convention: camelCase for variables CSS 161: Fundamentals of Computing
6
Operators & Precedence
Can use parentheses to change precedence 1 + 2 * 3 (1 + 2) * 3 CSS 161: Fundamentals of Computing
7
CSS 161: Fundamentals of Computing
8
Shorthand assignments
Example: Equivalent To: count += 2; count = count + 2; sum -= discount; sum = sum – discount; bonus *= 2; bonus = bonus * 2; time /= rushFactor; time = time / rushFactor; change %= 100; change = change % 100; amount *= count1 + count2; amount = amount * (count1 + count2); CSS 161: Fundamentals of Computing
9
Increment & Decrement Operators
Syntax: variable++ | ++variable | variable-- | --variable Semantics: Set variable to next / previous value Examples int num1 = 1; char char1 = ‘B’; num1++; char1--; Precedence: increment/decrement before/after use 2*(num1++) 2 2*(++num1) 4 CSS 161: Fundamentals of Computing
10
Assignment Compatibility
byteshortintlongfloatdouble char CSS 161: Fundamentals of Computing
11
Type casting & coercion
Syntax: variable = (type) expression Semantics Cast (convert) expression to type, assign value to variable Only allowed if expression type is compatible with variable type Type coercion: Automatic type casting: int float double Examples short num3 = ‘A’; // 65 = ASCII code for ‘A’ int num4 = (int) 1.9; double num5 = 123; // coercion int num6 = 1.23; // error CSS 161: Fundamentals of Computing
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.