Expliciete typecast

Getallen en rekenen

Een voorbeeld van een noodzakelijke expliciete typecast

Uitvoer (Application)

 
 quotientInt= 3.0
 quotientDouble= 3.2 

Programmacode

import java.io.*;

class TypecastExApp {
     static public void main(String args[] ) throws IOException {
	double quotientInt, quotientDouble,  rest; 
	int x = 16, y = 5; 
	// Stap1 zonder expliciete typecast
         quotientInt = x / y; 	         // quotientInt = 3
         // Stap2 met expliciete typecast
         quotientDouble= (double)x / y;   // quotientDouble = 3.2

 	System.out.println( "quotientInt= "    + quotientInt);   
 	System.out.println( "quotientDouble= " + quotientDouble);   
    }
}

Uitleg

In dit voorbeeld moet er in de deling een expliciete typecast uitgevoerd worden,

Stap1 zonder expliciete typecast

Stap2 met expliciete typecast

copyright ® Little World 1998