Lesson 3: Java Assignments and Type Casting

Java programming made easy


Java makes use of the = sign to change the values associated with primitive data types. Remember that there are 8 of these primitive data types namely boolean, byte, char, short, int, float, long and double. Every time you need to make changes to a variable of primitive data type, remember to use "=" using a statement that looks like: variable = expression.

Here are a few examples
myAge = 20;
age = 21;
legalAge = age;
PI = 3.14159265359;
areaCircle = PI * radius * radius;
speed = distance/time;
The name of the variable where changes are being made is always placed on the left and the expression follows after the equal sign. An expression can consist of a mixture of arithmetic operators, other variables and constants.

There are times when you do not want to write the full assignment statement because it makes the code look weird. In situations like that, you make use of what is known as short hand assignments and this can be used on any arithmetic operator.  Here are a few short hand assignment examples.


x += 2; //same as x = x + 2
x++; // same as x = x + 1
x += y; // same as x = x + y;
rem %= 2; //same as rem = rem % 2
d /= 2; //same as d = d / 2
y *= x; //same as y = y * x
s *= (3 * x); //same as s = s * (3 * x)
f -= 100; //same as f = f -100;
b--; //same as b = b - 1;
There are times when you intentionally want to assign a value of one primitive type to a variable of a different type without generating a type mismatch error. Java will allow you to do this and it is known as type casting which is simple defined as the process of converting a value to match the data type of the variable you intend to store it in. Take a look at this type casting example.

int length = 5.45; //length cannot store double values because it is an integer data type
int length = (int)5.45; //no type mismatch error because 5.45 was converted to 5
double sum = 4; //no mismatch error because 4 is converted to 4.00
double sum = double(4); // is the same as above
The reason why its was legal to assign an integer value to a variable of double data type without casting it is because Java automatically did the casting and hence, there was no need for us to explicitly do the casting from int to double. This is known as automatic type casting and it is only done in the right direction whilst explicit type casting is done when moving from right to left. The direction is determined by the following assignment compatibility sequence.
byte —> short —> int —> long —> float —> double
Type coercion direction (—>)
 
byte <— short <— int <— long <— float <— double
Explicit type casting direction (<—)

You might be wondering why type compatibility is arranged in that way. In order to understand this, you need to take a look at the primitive data type table in the previous post and you would notice a few things right away.
  • First thing you will notice is the size of the primitive data type. The compatibility assignment sequence is arranged in ascending order with byte being the smallest with a size of 1 byte and double being the largest with a size of 8 bytes.
  • Secondly take note of the range of numbers each primitive data type can hold. A byte has a range of a 256 integers from -128 to 127. An int has a range of 4.29 billion integers ranging from -2147483648 to 2147483647. A double has the biggest range of all primitive data types.
Now that you are aware of the size and range of primitive data types, you would agree that a double is big enough to hold a float number or any number to its left in the assignment compatibility sequence. Likewise, an int is big  enough to hold a byte or short number because they are on the left of int but not big enough to store a float or double because they won't necessarily fit. 

You should however take note that you can use explicit type casting to store a double number to an int data type at the expense of losing some data such as the fraction. This means explicit type casting is done at the risk of losing data.

Share your thoughts