Yingcai Xiao Interactive Visualization with NUI and Game Engines.
COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa...
-
Upload
preston-page -
Category
Documents
-
view
217 -
download
0
Transcript of COM S 207 Literal, Operator, and Expression Instructor: Ying Cai Department of Computer Science Iowa...
COM S 207Literal, Operator, and Expression
Instructor: Ying Cai
Department of Computer ScienceIowa State [email protected]
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
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
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
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
String literals
A String literal needs double quotes to surround them OK
“hello all” “1234 hello 2323223” “a” “a\””’
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
Relational OperatorsResult type is boolean. Operands can be any primitive data type except boolean.
Boolean Operators
Operands must be boolean. Result is of type boolean AND && OR (||) Reverse (!)
Assignment Operators
“= “ is simple assignment operator It is different from “==“ which is a relational
operator
++ 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
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)
Compound Expression
Compound expressions are built from simple expressions
(i >= 3) && (j/3 != 0) && (k < 5)
i = j = 3
expression 1
expression 2
expressions
Precedence
The order in which operations are done in an expression is defined by precedence of operators
To avoid errors/confusing, using brackets ”()”
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)
Storage per Type (in bytes)
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)
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
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
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