Search This Blog

Saturday, July 18, 2020

Primitive Type Challange.


Program:

package primitiveTypeChallange;

public class PromitiveTypeChallange {

public static void main(String[] args) {

byte byteNumber = 123;
short shortNumber = 32767;
int intNumber = 867656;

long longNumber = 50000L * 10L*(byteNumber + shortNumber+intNumber);

System.out.println(longNumber);

short  shortTotal=(short)(100+10*byteNumber+shortNumber+intNumber);
System.out.println(shortTotal);

}
}


Floating point number:

floating point numbers have fractional parts that we express with a decimal point . 3.14159
is an example.


Floating point numbers are also known as real numbers. We use a floating point number when we need more precision in calculation.

Note: -  There are two  primitive  types in java for expressing floating point numbers 

1. float - float is single precision number.
2. double- double is a double precision number.

What is precision ?

precision refers to the format and amount of space occupied by the type.
Single precision occupies 32 bits(width of 32) and Double occupies 64 bits(thus has width of 64).

As a result the float has a range from 1.4E-45  to 3.4028235+38 and the double  is much more precise with a range from 4.9E-324 to 1.7976931348623157E+308.

program:

package floatAnddoublePrimitivetype;

public class FloatAndDouble {

public static void main(String[] args) {

float minValue = Float.MIN_VALUE;

float maxValue = Float.MAX_VALUE;
System.out.println("Float min value = " + minValue);
System.out.println("Float max value = " + maxValue);

double duobleminValue = Double.MIN_VALUE;

double doublemaxValue = Double.MAX_VALUE;
System.out.println("Double min value = " + duobleminValue);
System.out.println("Double max value = " + doublemaxValue);
}
}


Homework:

package floatAnddoublePrimitivetype;

public class FloatAndDouble {

public static void main(String[] args) {

float minValue = Float.MIN_VALUE;

float maxValue = Float.MAX_VALUE;
System.out.println("Float min value = " + minValue);
System.out.println("Float max value = " + maxValue);

double duobleminValue = Double.MIN_VALUE;

double doublemaxValue = Double.MAX_VALUE;
System.out.println("Double min value = " + duobleminValue);
System.out.println("Double max value = " + doublemaxValue);
// Exercise 
float floatNum = 453.90;// compile time error will be thrown
// Q. how do you fox this error for variable floatNum
// Solution
float floatNumSol1 = 453.90f;
float floatNumsol2 = (float) 453.90;
double doubleNumber = 7865.67567;
}
}








No comments:

Post a Comment