21
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa State University [email protected]

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

Embed Size (px)

Citation preview

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

COM S 207Literal, Operator, and Expression

Instructor: Ying Cai

Department of Computer ScienceIowa State [email protected]

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

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

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

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

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

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*1025) -100.23E-24

Not OK 1,234.34 1.23-e25

float double literals ending

with f or F 0.23f or 0.23F

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

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

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

String literals

A String literal needs double quotes to surround them OK

“hello all” “1234 hello 2323223” “a” “a\””’

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

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

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

Relational OperatorsResult type is boolean. Operands can be any primitive data type except boolean.

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

Boolean Operators

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

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

Assignment Operators

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

operator

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

++ 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

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

Expressions

Expressions are like clauses in sentences in EnglishLike operators, expressions will typically have a result, the result will have a type, and there may be a side effectSimple expressions• single operator and operands.

5/3, 5%3• method called

s.substring(0, 9)

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

Compound Expression

Compound expressions are built from simple expressions

(i >= 3) && (j/3 != 0) && (k < 5)

i = j = 3

expression 1

expression 2

expressions

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

Precedence

The order in which operations are done in an expression is defined by precedence of operators

To avoid errors/confusing, using brackets ”()”

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

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)

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

Storage per Type (in bytes)

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

Overflow

Overflow occurs when the storage for a variable cannot hold the resultint oneThousand = 1000;

int oneMillion = 1000 * oneThousand;

int oneBillion = 1000 * oneMillion;

System.out.println(3 * oneBillion);

will print out -1294976296why?

the result (3 billion) overflows int capacitymaximum value for an int is +2,147,483,647

Use a long instead of an int (or use a double)

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

Data Conversion

Converting one data type into anotherWidening 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

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

Conversions

Assignment for widening conversionsint count;

short pastCount;

count = pastCount; //allowed and conversion done

Promotiondouble result, sum;

int count;

sum = 24.32; count = 4;

result = sum/count; // count promoted to double

Casting: used for narrowing conversionsdouble money = 2000.234

int handOver = (int) money; //thows away fraction

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

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