//
// file: PrimConv.java
// demonstrate conversion and casting with java primitives
//
public class PrimConv {	public static void main(String args[]) {
	PrimConv pc = new PrimConv();		
/* * METHOD CALL CONVERSION		 
   * In the package java.lang, class Math, the
   * method sqrt is declared as follows:
   * public static double sqrt(double a)		 
   * We pass a float. The float is converted to a double
   * We pass an int. The int is converted to a double		 
*/		 
	float f = 2.0F;
	int i = 2; 
	double d;		 
	d = Math.sqrt(f);		 
	System.out.println("d is :"+d);
		 
	d = Math.sqrt(i);	
	System.out.println("d is :"+d);
      // This code will not compile because compiler
      // cannot convert float to int (narrowing conversion)
		 
      // d = myMethod(f);
      // This will compile		 
      System.out.println("d is :"+ pc.myMethod(i));		 

/* * ARITHMETIC PROMOTION * */		  
      // b is promoted to int for increment operation		  
      // but converted back to it's original type when output
      byte b =  127;
      System.out.println("b++ is: "  + b++ + " ++b is: "+ ++b);
      // Will not compile		  
      // "Explicit cast needed to convert int to byte"		  
      // What int ?		  

/* * byte b3 = ~b2; */
		   		  
      // but this does compile
	byte b3 = ~127;		  
	System.out.println("b3 is :"+b3);		  
	// b2 quietly overflows without a peep		  
	byte b2 = 127;		  
	int i2 = (++b2 * 10);		  
	System.out.println("i2 is :"+i2);		  
	short s = 255;		  
	// Will not compile		  
	// "Explicit cast needed to convert int to short"
	// What int ?		  

/* * short s1 = +s; */		  

	//For binary operands, both operands are converted to	
	// at least int		  
	byte b4 = 10;		  
	byte b5 = 20;		  
	// byte b6 = b4 + b5; will not compile		  
	byte b6 = (byte)(b4 + b5);		  
	System.out.println("b4 + b5 is:"+ (b4+b5));		  
	System.out.println("b6 is :"+b6);		  
	
	// If one of the operands is a long, the other is 		  
	// converted to a long		  
	long longvar = 1; 		  
	int ivar = 1; 		  		  
	// Will not compile		  
	// "Explicit cast needed to convert long to int		  

/* * int ivar2 = ivar + longvar; */
		   		  
	// if one of the operands is a double, both
	// are converted to double
	float fvar = 0f;
	double dvar = 0D; 		  
	// Will not compile		  
	//"explicit cast needed to convert double to float 		  

/* * float fvar2 = fvar + dvar; */						  

/* * PRIMITIVE  CASTING */		  

	// Use a cast to allow a narrowing conversion		  
	short s2 = 100;		  
	byte b7 = (byte)s2;		  
	System.out.println("b7 is: "+b7);		  
	// but watch out for ...		  
	short s3 = 128;		  
	byte b8 = (byte)s3;		  
	System.out.println("b8 is: "+b8);	      
	// a bit weird but legal	      
	char ch = 'A';	      
	short s4 = (short)ch;	      
	System.out.println("s4 is :"+s4);	
}	
public double myMethod(int arg) {		
	// sqrt expects a double, the int		
	// is automatically converted 		
	return (Math.sqrt(arg));	}}
