Slide Set Two - Operators, Expressions, and Casting
Slide Set Two - Operators, Expressions, and Casting
INFO6066
INFO6066
Software and Information Systems Testing
INFO6066
Software and Information Systems Testing
Recap: Primitive Data Types in Java
• Primitive data types are the only things in Java that are not
objects (T!)
• primitives don’t have any methods associated with them.
Object data types like Strings do have methods.
• Are eight primitive data types in Java(T!).
• See if you can list them all before looking at the next slide!
INFO6066
Software and Information Systems Testing
Primitives Data Types in Java (T!)
INFO6066
Software and Information Systems Testing
INFO6066
Software and Information Systems Testing
Arithmetic Operators
• +, -, *, /, and %.
• Are called binary operators because they appear between two operands
(values).
• Example: myValue = 3 + 6;
• “add 3 to 6 and assign the result to the variable myValue”.
• Q. What is ‘%’ used for?
• A. it is the modulus division operator. This returns the remainder from a
division operation, rather than the quotient.
• Example : 5 / 2 returns a value of 2 (remember, integer division in Java
truncates the decimal portion!), with 1 as the remainder
• 5 % 2 (read as “5 mod 2” )will return a value of 1, which is the remainder
of the division operation.
INFO6066
Software and Information Systems Testing
Operator Precedence
• Q: What value does result get? int result = 14 – 10 / 2;
• A: 9 Were you expecting 2?
• The above expression contains three operators: =, -, /
• Q: How does Java decide which operation to perform 1st, 2nd, etc.?
• A: Operator precedence
Operator Precedence
• Similar to BEDMAS rule in arithmetic:
() Highest
– Brackets, Exponents, (Division, Multiplication),
(Addition, Subtraction) */%
INFO6066
Software and Information Systems Testing
INFO6066
Software and Information Systems Testing
INFO6066
Software and Information Systems Testing
Unary Operators:
Increment ++ and Decrement --
INFO6066
Software and Information Systems Testing
Increment ++ and
Decrement – continued…
INFO6066
Software and Information Systems Testing
Increment Operator…
Using pre-increment mode
• Same example from previous slide, but now we do the increment operation
in pre-increment mode.
• When using pre-increment mode, the increment operation is done first,
before the assignment operation is carried out.
• Example: int number1 = 5;
int number2 = ++number1 ;
INFO6066
Software and Information Systems Testing
INFO6066
Software and Information Systems Testing
INFO6066
Software and Information Systems Testing
INFO6066
Software and Information Systems Testing
INFO6066
Software and Information Systems Testing
Type Casting
• Converts a value of one data type into a value of another data type (T!)
• NOTE: it is the value stored in the variable that gets converted, and not the variable
itself.
• You can cast from larger capacity to smaller capacity data types, but you have to
tell Java explicitly to do this.
• Ex: double money = 20.35;
int change = (int) money; //put the data type you want to cast it to
//in brackets in front of the variable.
• Q. Now, if your are converting a double to an int, what value gets stored in variable
change?
• A. the value stored in change will be 20. The decimal portion will be truncated by
the casting operation.
• Casting a value to a lower capacity data type can result in a loss of precision (i.e.
truncated decimal portion), or it can result in an overflow error if you exceed the
maximum value of the lower capacity data type.
INFO6066
Software and Information Systems Testing
When is a Explicit Casting Required?
Cast from: double float long int short char byte
to double -
Decreasing Precision
to float (float) -
INFO6066
Software and Information Systems Testing
INFO6066
Software and Information Systems Testing
Useful Application of Casting: truncating to two decimals.
• When doing currency calculations using doubles, such as calculating sales
tax amounts, our final answers often have an excessive number of decimal
places.
• Ex. double price holds a value of 99.95345322356.
• We want to have it hold just 99.95.
• Step 1) multiply it by 100 to get 9995.345322356
double tempPrice = price * 100;
• Step 2) now cast this to an int and store it in a temp variable…
int tempPrice2 = (int)tempPrice; //this cuts off the decimals
//and assigns 9995
• Step 3) divide the int tempPrice2 by 100.0 (Q. why use 100.0 and not just
100?) and assign it back to the variable price
price = tempPrice2 /100.0; //this stores 99.95 into price
INFO6066