Lecture2 2020
Lecture2 2020
Lecture2 2020
type name;
type name = initial_value;
variable = expression
Num1 = 34;
Example:
int += 2
String Concatenation
Strings can be composed using the
concatenation operator (+), so that the code
(type) exp
Ordinary Casting
When casting from a double to an int, we may lose
precision.
This means that the resulting double value will be
rounded down.
But we can cast an into a double without this worry.
For example, consider the following:
doubled1 = 3.2;
doubled2 = 3.9999;
inti1 = 3;
inti2 = 6;
dresult = (double)i1 / (double)i2; // dresult has value 0.5
dresult = i1 / i2; // dresult has value 0.0
Implicit Casting and Auto
boxing/Unboxing
There are cases where Java will perform an implicit cast,
according to the type of the assignment variable, provided
there is no loss of precision.
For example:
int iresult, i = 3;
double dresult, d = 3.2;