1 / 28

Java Operators

Packages - Collection of classes (Programs). Classes - Collections of data and methods that operate on data. Methods - Collections of statements that operate on data. Variables - Store data Constants - Store data that doesn’t change Literals - Explicit data like “Hello World”.

kamilah
Download Presentation

Java Operators

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Packages - Collection of classes (Programs) Classes - Collections of data and methods that operate on data Methods - Collections of statements that operate on data Variables - Store dataConstants - Store data that doesn’t change Literals - Explicit data like “Hello World” ***** SWTJC STEM ***** Java Operators Recall Java’s programming components: Chapter 2-3 cg 29

  2. ***** SWTJC STEM ***** Expressions and Operators • Expressions are segments of code that perform computations and return values. They are combinations of literals, variables, constants, and operators. • An operator performs an action on variables, constants, and literals. • Associated with an operator are one or two operands that receive the action of the operator. • Unary operators act on only one operand. • Example: -12, negation operator “-” • Binary operators act on two operands. • Example: 8 + 15, addition operator “+” Chapter 2-3 cg 29

  3. ***** SWTJC STEM ***** Unary Operators • Unary operators support either prefix or postfix notation. • Prefix notation means that the operator appears before its operand: • operator operand • Example: • - i, negation operator “-” • ++ i, shortcut increment operator “++“ • Postfix notation means that the operator appears after its operand: • operand operator • Example: • i ++, shortcut increment operator “++“ Chapter 2-3 cg 29

  4. ***** SWTJC STEM ***** Binary Operators • Binary operators use infix notation, which means that the operator appears between its operands: • operand1 operator operand2 • Examples: • i + j • a / b • x += 10 Chapter 2-3 cg 29

  5. Operator Operator Use Use Description Description + + op1 + op2 +op Promotes op to int if it's a byte, short, or char Adds op1 and op2 Unary Operators (op is an operand) - - op1 - op2 -op Arithmetically negates op Subtracts op2 from op1 * op1 * op2 Multiplies op1 by op2 / op1 / op2 Divides op1 by op2 % op1 % op2 Computes the remainder of dividing op1 by op2 Binary Operators ***** SWTJC STEM ***** Basic Numeric Operators Chapter 2-3 cg 29

  6. ***** SWTJC STEM ***** • Declare Variables... • int i = 37; • int j = 42; • double x = 27.475; • double y = 7.22; • Adding... • i + j is 79 • x + y is 34.695 • Subtracting... • i - j is -5 • x - y is 20.255 • x - (-y) is 34.695 • Multiplying... • i * j is 1554 • x * y is 198.3695 • Dividing... • i / j is 0 (integer division implied) • x / y is 3.8054... • Computing the remainder... • i % j is 37 , j % i is 5 • x % y is 5.815 • Mixing types...converts to double... • j + y is 49.22 • i * x is 1016.58 Numeric Operator Examples Chapter 2-3 cg 30

  7. ***** SWTJC STEM ***** Coding an expression - Examples Example: Coding expressions • int thisDecade = 2000; byte decadeLength = 10; int nextDecade = thisDecade + decadeLength; • byte decadeLength = 10;assigns the value 10 to a 8-bit byte variable named decadeLength. • int thisDecade = 2000;assigns the value 2000 to a 32-bit integer variable named thisDecade. • int nextDecade = thisDecade + decadeLength;assigns the value 2010 to a 32-bit integer variable named nextDecade. Chapter 2-3 cg 30

  8. ***** SWTJC STEM ***** Coding an expression - Examples • Examples: Expression coding continued . . . • float radius = 2.0f; float area = 3.14159f * radius * radius; • float radius = 2.0f;assigns the value 2.0 to a 32-bit (single precision) floating point variable named radius. • float area = 3.14159f * radius * radius;assigns the computed expression to a 32-bit (single precision) floating point variable named area. Chapter 2-3 cg 30

  9. Operator Use Description ++ (incr) op++ Increments operand op by 1; evaluates to the value of opbefore incrementing op ++ (incr) ++op Increments operand op by 1; evaluates to the value of op after incrementing op -- (decr) op-- Decrements operand op by 1; evaluates to the value of opbefore decrementing op -- (decr) --op Decrements operand op by 1; evaluates to the value of opafter decrementing op ***** SWTJC STEM ***** Shortcut Incr/Decr Operators Shortcut Increment/Decrement Operators Chapter 2-3 cg 29

  10. ***** SWTJC STEM ***** Postfix Operator Code... int i = 10;int newNum = 10 * (i++); Prefix Operator Code... int i = 10;int newNum = 10 * (++i); Shortcut Incr/Decr Examples iincrements after *. iincrements before *. • Note: --i and i– (decrement) work similarly. Chapter 2-3 cg 29

  11. Operator Use Equivalent to += op1 += op2 op1 = op1 + op2 -= op1 -= op2 op1 = op1 - op2 *= op1 *= op2 op1 = op1 * op2 /= op1 /= op2 op1 = op1 / op2 %= op1 %= op2 op1 = op1 % op2 ***** SWTJC STEM ***** Shortcut Assignments Operators Examples . . . • iValue += 8; is the same as iValue = iValue + 8; • jValue %= 5; is the same as jValue = jValue % 5; Chapter 2-3 cg 29

  12. ***** SWTJC STEM ***** Shortcut Assignment Examples • Assignment Addition Code: • int i = 10;int newNum = 10; • newNum += i; • Same asnewNum = newNum + i; • newNum becomes 20. i remains 10. • Assignment Division Code: • int i = 10; int newNum = 10; • newNum /= i; • Same asnewNum = newNum / i; • newNum becomes 1. i remains 10. Chapter 2-3 cg 29

  13. Operator Use Description + char1 + char2 string1 + string 2 Concatenates characters and strings Binary Operator ***** SWTJC STEM ***** Character Operator A char/string variable holds character/string data, respectively. Char literals use apostrophes ‘s‘and are single values. String literals use quotation marks “red leaf” and can hold more than one character (i.e., text). Concatenation “ties together” char/string variables and literals. Chapter 2-3 cg 29

  14. ***** SWTJC STEM ***** Character Operator Examples Examples . . . • char cValue = ‘s’;Declares a character variable cValue and assigns character s to it. • String c1Value = “red”, c2Value = “bird”;Declares string variables c1Value and c2Value assigning strings “red” and “bird” to them respectively. • System.out.println(c1Value + c2Value + cValue);Outputs string redbirdsto the PC monitor, which isthe concatenation of all three variables. Chapter 2-3 cg 29

  15. ***** SWTJC STEM ***** Creating Complex Assignments • When creating complex assignment expressions, • How is the order of operations determined? • What if we mix data and variable types? • Examples: • int result = 14 + 8 / 2; • Is theresult 11 (+ first)? or 18 (/ first)? • int result = 12 / 2 * 3; • Is theresult 18 (/ first)? or 2 (* first)? • float total = 100.46f;int count = 10;float result = total / count; // Mixed types? • Isresult 10(integer /)?or10.046(float /)? Chapter 2-3 cg 29-31

  16. ***** SWTJC STEM ***** Order of Operations - Precedence • Expressions are evaluated according to operator precedence. • When operators of equal precedence occur together in an expression, association governs the order. • Left-associative means operations are performed left-to-right. • Binary operators, except the assignment operator, are left-associative. • Right-associative means operations are performed right-to-left. • Unary and assignment operators are right-associative. Chapter 2-3 cg 29-31

  17. ***** SWTJC STEM ***** Order of Operations - Precedence Chapter 2-3 cg 29-31

  18. ***** SWTJC STEM ***** Order of Operations - Examples • Given differing orders of precedence. • result = 14 + 8 / 2; // Divide first • / higher precedence than + and result is 18. • Precedence can be forced using parentheses. • result = (14 + 8) / 2; // Add first • + is forced first by parentheses and result is 11. • Given the same order of precedence. • result = 12 / 2 * 3; // Divide first • / is first (L to R), then * andresult is 18. • Adding a unary operator -. • result = 12 / -(-3 + 1) * 3; // Negation first • - is first (R to L), then / (L to R), then * and result is 18. Chapter 2-3 cg 29-31

  19. ***** SWTJC STEM ***** Order of Operations - Assignment • Given assignment operators. Assumeint a = 1. • result = a + (a = 3); • Proceed L to R a = 1,then1 + (a = 3). • Now do (a = 3) then 1 + 3 result = 4. • result = (a = 3) + a; • Proceed L to R (a = 3)first, soais now3. • Now do a + a = 3 + 3 so resultis 6. • result = (a += 2 * a) + a • (a = 1 + 2 * 1) + a. • (a = 3) + a = a + a = 3 + 3 = 6. Chapter 2-3 cg 29-31

  20. ***** SWTJC STEM ***** Order of Operations - Incr/Decr • Given increment/decrement operators. Assumeint a = 5. • result = a + (--a) + a • Proceed L to R a = 5,then5 + (--a) + a. • Now do a = a - 1= 4 a, then 5 + 4 + a 9 + a. • Finally, a = 4 and 9 + 4result is 13. • result = a + (a--) + a • Proceed L to R a = 5,then5 + (a--) + a. • Now do 5 + 5 + a = 10 + a, then do a = a - 1 = 4 a. • Finally 10 + a = 10 + 4result is 14. Chapter 2-3 cg 29-31

  21. ***** SWTJC STEM ***** Mixed Data/Variable Types • Java is a strongtyped language, meaning that all data and variables must be explicitly typed. • Sometimes conversion is necessary. • Recallfloat total = 100.46f;int count = 10;float result = total / count; // Mixed types? • Care must be exercised or information can be lost. • byte a = 20; // 1 byte in sizeint b = 2 * a ; // 4 bytes in sizebcan holda; (1 byte) will fit in (4 bytes). • int a = 2000; // 4 bytes in sizebyte b = 2 * a ; // 1 byte in sizebcannot storea. ; (4 bytes) will not fit in (1 byte)! Chapter 2-3 cg 32

  22. From To byte short, int, long, float, double short int, long, float, double char int, long, float, double int long, float, double long* float, double float double * Some precision (significant digits) could be lost! ***** SWTJC STEM ***** Mixed Data/Variable Types • Conversions are of two types: • Widening conversions. • Safest, does not lose information. • Data conversion involves smaller to larger storage. Chapter 2-3 cg 32

  23. From To byte char short byte, char char byte, short int byte, short, char long byte, short, char, int float byte, short, char, int, long double byte, short, char, int, long, float ***** SWTJC STEM ***** Mixed Data/Variable Types • Conversions are of two types (continued): • Narrowing conversions. • Likely to lose information. Avoid! • Data conversion involves larger to smaller storage. Chapter 2-3 cg 32

  24. ***** SWTJC STEM ***** Assignment Conversion • Java conversions occur in three ways: • Assignment conversion • Occurs when one type is assigned to another. • Only widening conversions allowed. • Is automatic. • double money;int dollars = 1000;money = dollars; // int will fit in doubleOK - Widening conversion, done. • double money = 1000;byte dollars;dollars = money; // double will not fit in int (max 255)!Not OK - Narrowing conversion, error! Chapter 2-3 cg 32

  25. ***** SWTJC STEM ***** Arithmetic Promotion • Java conversion occur in three ways (cont.): • Arithmetic promotion • Occurs in expressions when operators need to widen (promote) data to complete. • Only widening conversions allowed. • Promotion is automatic when possible. • float total = 100.46f, result;int count = 10;result = total / count; countpromoted to floatautomatically.Floating point division used to calculate result.resultis1.0046. Chapter 2-3 cg 32

  26. ***** SWTJC STEM ***** Casting - Forced Conversion • Java conversion can occur in three ways: • Casting - Forcing the conversion • Most general form of conversion. • Unary operator specified by a data type in parentheses, (type) operand. • Is not automatic. Programmer must specify. • Example:float money = 8.75f;int dollars;dollars = (int) money;Casts money (float) to dollars (int).dollarsis8money truncated (decimal portion dropped) Chapter 2-3 cg 32

  27. ***** SWTJC STEM ***** Casting Continued • Java conversion can occur in three ways: • (cont) Casting examples • double result;int count = 10, total = 20;result = (double) total / count; totalcast to double.count automatically promoted to double.Floating point division used.Quotient 2.0 stored in result as adouble. Chapter 2-3 cg 32

  28. ***** SWTJC STEM ***** Casting Continued • Java conversion can occur in three ways: • (cont) Casting char variables • char c = 'A'; // upper case Aint ic;ic = (int) c;char cis cast as anint ic is 65. • ic = 97;c = (char) ic;int icis cast aschar c = ‘a’, lower case a. Chapter 2-3 cg 32

More Related