Presentation is loading. Please wait.

Presentation is loading. Please wait.

COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University

Similar presentations


Presentation on theme: "COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University"— Presentation transcript:

1 COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University yingcai@iastate.edu

2 Literals Literals are VALUES in a program int i = 10; char c = ‘A’; String s = “Hello”; In the above program, 10, ‘A’, and “Hello” are literals

3 datatype or type of a literal When Java sees a value, it DECIDES on a type for the literal 1234  Java will decide that this is an int type ‘a’  Java will decide that this is a char type true  Java will decide that this is a boolean type 2.3  Java will decide that this is a double type “Hi”  Java will decide that this is a String type The values will always be taken to be one of these types

4 Values must follow rules to be recognized int OK  1234  -123345  793450 Not OK  1,234 (comma not allowed)  $1234 ($ not allowed) long int literal ending with l or L  12345l or 12345L double OK  0.354  1.234e25 (=1.234*10 25 )  -100.23E-24 Not OK  1,234.34  1.23-e25 float double literals ending with f or F  0.23f or 0.23F

5 Values must follow rules to be recognized char single quote around a character  ‘a’, ‘A’, ‘@’ Not OK  ‘AB’ boolean only two valid value  true  false Not OK  TRUE  False  Yes  No  True  FALSE

6 String literals A String literal needs double quotes to surround them OK  “hello all”  “1234 hello 2323223”  “a”  “a\””’

7 Arithmetic Operators These work with all primitive types except boolean + additive operator - subtraction operator * multiplication operator / division operator % remainder operator e.g., 5%2 = 1; 10%4 = 2; 10%6 =4 Result type depends on the type of the operand

8 Relational Operators Result type is boolean. Operands can be any primitive data type except boolean.

9 Boolean Operators Operands must be boolean. Result is of type boolean AND && OR (||) Reverse (!)

10 Assignment Operators “=“ is simple assignment operator It is different from “==“ which is a relational operator

11 ++ Unary Operator ++i (pre increment) result value is new value side effect: 1 is added to value of I int i = 10; int k = ++i; // results: k=11 and i=11 i++ (post increment) result value is old value of I side effect: 1 is added to value of I int i = 10; int k = i++; // results: k=10 and i=11

12 Expressions Expressions are like clauses in sentences in English Like operators, expressions will typically have a result, the result will have a type, and there may be a side effect Simple expressions single operator and operands.  5/3, 5%3 method called  s.substring(0, 9)

13 Compound Expression Compound expressions are built from simple expressions (i >= 3) && (j/3 != 0) && (k < 5) i = j = 3 expression 1 expression 2 expressions

14 Precedence The order in which operations are done in an expression is defined by precedence of operators To avoid errors/confusing, using brackets ”()”

15 Associativity If we have operators with same precedence, which operation will be done first? This is known as “associativity” Associativity can be Right to Left (= operator) Left to Right (+ operator)

16

17 Storage per Type (in bytes)

18 Overflow Overflow occurs when the storage for a variable cannot hold the result int oneThousand = 1000; int oneMillion = 1000 * oneThousand; int oneBillion = 1000 * oneMillion; System.out.println(3 * oneBillion); will print out -1294976296 why? the result (3 billion) overflows int capacity maximum value for an int is +2,147,483,647 Use a long instead of an int (or use a double)

19 Data Conversion Converting one data type into another Widening conversion: no problem more space is available in the new type no data loss Narrowing conversion: problematic Less space is available in the new type data loss possible

20 Conversions Assignment for widening conversions int count; short pastCount; count = pastCount; //allowed and conversion done Promotion double result, sum; int count; sum = 24.32; count = 4; result = sum/count; // count promoted to double Casting: used for narrowing conversions double money = 2000.234 int handOver = (int) money; //thows away fraction

21 Special Conversion to String If one of the operands to the + operator is a String, the other operator is converted to a String and the two string are concatenated “hello” + 1  “Hello1” 1 + “hello”  “1Hello” This works for all types


Download ppt "COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University"

Similar presentations


Ads by Google