Download notes - Department of Computer Science

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
COM S 207
Literal, Operator, and Expression
Instructor: Ying Cai
Department of Computer Science
Iowa State University
[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
double
int

OK

 0.354
 1234
 1.234e25 (=1.234*1025)
 -123345
 -100.23E-24
 793450

Not OK

 1.23-e25
 $1234 ($ not allowed)

int literal ending with l or L
 12345l or 12345L
Not OK
 1,234.34
 1,234 (comma not allowed)
long
OK
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
boolean

 true
 false
 ‘a’, ‘A’, ‘@’

Not OK
 ‘AB’
only two valid value

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 Operators
Result 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
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)
Compound Expression
Compound expressions are built from simple
expressions
i=j=3
expression 1
expression 2
(i >= 3) && (j/3 != 0) && (k < 5)
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 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)
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
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
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