link one

link one

link one

link one

link one

link one - Cast Operator ()

The meaning of the cast, is converting from one data type to other. Here we will only cover only the conversion between the data types that we have introduced in previous sections i.e. the conversions between primitive types.

Java compiler has a capability to compensate some compatiable primitive data types in the assignment statements where the type of the variable whose a value is to be assigned is compatible but different of the type of the value. In some situations however, Java compiler can not accomplish the type conversion from himself and the use of the casting operator becomes necessary.

Although some some Java textbooks or tutorials present, Java's ability to transform between compatible data types as a normal action of the Java programming language even consider automatic type conversions of the Java compiler as gift, in reality it is much a nuisance and something to avoid at any cost.

Inadvertently converted data types are potential error sources in subsequent steps of the program. Programmers should give extreme importance to the types they using in the programs steps and keep track of sneaky type canges during the execution of the programs. If any type compatibly is encountered and implicit type conversions or explicit type castings should be made, this the time where alarming bells are ringing. We should immediately inspect our program up to the deepest parts detect the causes bothersome type conversions or type casts find the ways to eliminate them and rephrase the whole program.

Sometimes, the cause to render the type conversion or a type casting may not be eliminated. Data may have read from an external device and further processed by the program. In that case, program design should be made very carefully as to realize type conversions without causing any problem immediately or later in forthcoming program steps.

Java compiler has the ability to perform type conversions between compatible types when the conditions permit. From the primitive data types,

In type conversions between compatible primitive types, the negotiation is between the amount of memory delivering from data types. The data values to be converted from the types affording lesser amount of memory to a data types affording more amount of memory are named as widening primitive conversions. - Widening Primitive Conversions

Widening primitive conversions may be summarized as follows:

Widening conversions occur automatically, no explicit cast is necessary. No runtime exception is generated by a widening conversions. No data oveflow is encountrered because the range of the narrower data type is within the range of the wider data type.

Widening conversions between integer primitive types occur without a problem. No information is lost since, the information may be fit to a narrower memory place may be fit smoothly from a wider memory area. An example of the widening conversions between primitive integral types is shown below:

package preliminary;

public class WideningIntegers {

public static void main(String[] args){

 byte byteValue = 12;
 short shortValue = 0;
 int intValue =0;
 long longValue = 0;

shortValue  = byteValue; // Widening Conversion from byte to short !
intValue = shortValue; // Widening Conversion from short to int !
longValue = intValue; // Widening Conversion from int to long !
System.out.println("longValue = " + longValue);
 } // end main

} //end class WideningIntegers 
      The result of this program :
      LongValue = 12
      The result of this program :
      LongValue = 12

From the results of the program above, we can see that no problem is experienced with the widening conversion of the integral primitive data types. We may thrust this conversion type and use in our programs without any restriction.

The widening conversion between float and double may result some loss information about the overall magnitude in the resulting value, except in strictfp expressions. The loss in the result may not be sensed at once, but it may or may not cause detrimental effects in the next program steps. We will introduce strictfp later but we can agree once again that programmers are better off when stay away from the float data type. One example is given below:

package preliminary;

public class FloatToDouble {

public static void main(String[] args){

 float floatValue = 2.589453377E-44F;
 double doubleValue = 0;

doubleValue = floatValue; // Widening Conversion from float to double !

System.out.println("doubleValue = " + doubleValue);
 } // end main

} //end class FloatToDouble
      The result of this program :
      doubleValue = 2.5223372357846707E-44

The results of this program clearly indicates that the converted value is wrong!. This erroneous value is assigned without issuing any warning. This is inacceptable in termes of programming concept and we should absolutely avoid float to double conversions.

<< Index

Polyglott HTML5(XHTML5 compliant HTML5 code)