Casting primitive data types allows us to convert a piece of data from one type to another. When converting a piece of data from one type to another you are either "widening" or "narrowing".
Widening
Widening means converting to a data type with either more precision or more space.
An example of widening would be storing an int data type as a double. Consider the example below.
int j = 105;
double d = j; //int j is widened to 105.0
double d = 5;//widens 5 to 5.0
In the below example, we cast an int to a double. Although the operation seems pointless here, we will see an example shortly where this may be necessary.
int i = 11;
double d = (double)i;//widens 11 to 11.0
Narrowing
Narrowing means converting to a data type with either less precision or less space.
Recall, that an important principle to remember in Java is that Java will not lose stored information. The following example will not compile since i is an integer and it would have to chop-off the .78 and store just 29 in i.... thus, information would be lost.
double d = 29.78;
int i = d; //will not compile because information is lost
In order for the above code to work we need to cast d as an integer. With casting, we can force compilation and force 29.78 to be stored as just 29. The syntax for doing this is illustrated below,
double d = 29.78; int i = (int)d; //(int) casts d as an integer and narrows d to an int.
Recall that when performing division with int data types, the decimal portion of the result is "cut off".
Consider the following example,
System.out.println(5/3);//prints 1
int i = 20;
int j = 9;
System.out.println(i/j);//prints 2
If however, one or more of the values in the numeric operation is a double, the result will be treated as a double. This is illustrated below,
System.out.println(5.0/3);//prints 1.6666666666666667
double i = 20.0;
int j = 9;
System.out.println(i/j);//prints 2.2222222222222222
double d = (double)5/4 // same as 5.0/4... (double) only applies to the 5
System.out.println(d); //the out put is 1.25
System.out.println(5.0*3);//prints 15.0
double i = 20.0;
int j = 9;
System.out.println(i*j);//prints 180.0
System.out.println(5.0 - 5);//prints 0.0
Predicting the output of mixed data type operations can be tricky. In the example below, the result is "0.0". This is because the result is cast to a double after the division.
int j = 4;
int k = 5;
System.out.println(double)(j / k); //0.0 is printed
Dividing by zero behaves differently with int and double variable types.
For example, if we divide an int variable type by zero, we get the following error,
Exception in thread "main" java.lang.ArithmeticException: / by zero
On the other hand, if we are using double division, we can divide by zero. The output will depend on the numbers we are dividing. Consider the examples below,
System.out.println(5/0.0);//Prints "Infinity"
System.out.println(-5/0.0);//Prints "-Infinity"
If both the numerator and the denominator are both 0.0, the result is NaN which stands for "Not a Number".
System.out.println(0.0/0.0);//Prints "NaN"
Constants follow all the rules of variables; however, once initialized, they cannot be changed. Use the keyword final to indicate a constant. Conventionally, constant names have all capital letters. The rules for legal constant names are the same as for variable names. The following is an example of a constant,
final double PI = 3.14159;
The following illustrates that constants cannot be changed,
final double PI = 3.14159;
PI = 3.7789; //illegal assignment
Constants can be first declared, then later initialized as follows,
final double PI;
PI = 3.7789; //legal assignment
Constants can also be of type String, int, and other types,
final String NAME = “Donald Trump”;
final int DAYS_TO_SUMMER = 259;