float type in java

Previous
Next
float typeSize in bytesLimits
float4±3.40282347E+38F
double8±1.79769313486231570E+308

Note: By default the decimal type is double. Hence we cannot assign a value directly to double(higher) type variable.

class Program 
{
	public static void main(String[] args) 
	{
		// float f = 2.3 ; /* Error : */
		double f = 2.3 ;
		System.out.println("f value : "+f);
	}
}

We can declare float variable with precision.

class Program 
{
	public static void main(String[] args) 
	{
		float f = 2.3f ; /* allowed */
		System.out.println("f value : "+f);
	}
}

Code: int/int results integer value only

class Pro 
{
	public static void main(String[] args) 
	{
		int a=5, b=2;
		double c=a/b;
		System.out.println("Result : " + c);
	}
}

Code: double/int results double value

class Pro 
{
	public static void main(String[] args) 
	{
		int a=5, b=2;
		double c=(double)a/b;
		System.out.println("Result : " + c);
	}
}
Previous
Next

Add Comment

Courses Enquiry Form