class ConvertAndCast {
	/*
	 * x=y is converted automatically if y IS-AN x
	 * In other words...conversion between primitive types
	 * is automatic provided it does not cause a loss of information
	 * Otherwise, you must explicitly CAST (provided the conversion makes sense)
	 */

	public static void main(String [] args){
		int i; double d; char c; boolean b;
		i=7; d=12.2; c='A'; b=true;

		// Allowed (java does the conversion for you)
		i=c; d=i; 

		// Not allowed:
		// i=d; b=i; c=d; c=i; i=b;

		// Conversion is done from left to right
		// only as much as necessary. 
		// Expression is evaluated first!
		double d1=(1/2)+(1/2);
		double d2=(1/2)+(1/2.0);
		double d3=(1/2.0)+(1/2.0);
		System.out.println(d1+" "+d2+" "+d3);

		// If you REALLY want to do the conversion and it
		// will cause a loss of information but the conversion
		// makes sense, you must tell Java that you recognize the fact.
		// you do this by CASTING.

		i=(int)d; // cast d to an integer
		c=(char)d; // cast d to a char
		c=(char)i; // cast i to a char

		// i=(int)b; // Java does NOT allow this cast!

		d=3.99; 
		i=(int)d; // Cast required, info lost!!
		System.out.println("i="+i);

		d=i; // No cast required here!
		System.out.println("d="+d);

		int i1=(int)((1/2)+(1/2));
		int i2=(int)((1/2)+(1/2.0));
		int i3=(int)((1/2.0)+(1/2.0));
		System.out.println(i1+" "+i2+" "+i3);

		// Note: every character corresponds to an integer 
		// but not every integer corresponds to a character.

		c='A'; i=c; // Don't need to cast here!
		System.out.println("i="+i);
		c='B'; i=c;
		System.out.println("i="+i);
		i=i+12; c=(char)i; // Need to cast here!
		System.out.println("c="+c);

		// For an expression, each participant is converted to 
		// the type of most general participant (in left to right
		// order!).

		System.out.println("(i+c+d)="+(i+c+d)); // The most general type is double
		System.out.println("(i+c)="+(i+c)); // ?
	}
}
