Double and int

Different Data types are used in programming languages to store a specific type of data, such as char can store only alphabetic characters as well as float used to store decimal values. In this article, …

Double and int

Different Data types are used in programming languages to store a specific type of data, such as char can store only alphabetic characters as well as float used to store decimal values. In this article, differentiating between two primitive data types is also counted in fundamental data types.

Double

Double is IEEE  standard advanced type of float data type, which stores fractional values inside it. It is called “double” due to its double-precision than float as it is 64-bits long, stores 15-16 digits after the decimal point. It gives results more accurate in comparison to float.

Programmers used double instead of float if output concern with accuracy. It is expensive to use in old versions of the computer where RAM is a big issue but compatible for new generation PC’s.

Code

double d = 3.9374865;
      System.out.println(d);

Output

$java -Xmx128m -Xmx16M HelloWorld
3.9374865

The data type of initialized value is double, which shows all values after the decimal point.  

int (Integer)

Int is used to declare Integers or numbers without the decimal point. It may contain sign or unsigned values. Integers are the whole number without fractional values. It acts as a binary representer of an integer. Integer allocates the same size of RAM to declare the value of the high range.

The division between two values in int, the result will be truncated because of fractional values which are not handled by int data type.

Code

     int d = 3.9374865;
      System.out.println(d); 
 }

Output

HelloWorld.java:4: error: incompatible types: possible lossy conversion from double to int
     int d = 3.9374865;
             ^
1 error
             

In the above program, the output of decimal value shows the error because the above program declared int as a data type that only works on real numbers.

Double  Vs Int

IntDouble
It is used to store only whole numbers  It is used to store decimal numbers
32-bit size data type represents a binary integer64-bit size data type with 2x precision
Assigned 4 bytes of memory to store dataAssigned 8 bytes of memory to store data
Arithmetic operations work fasterArithmetic operations work slower
As long size of memory, it is an expensive way to store variablesAllocate the small size of memory is a cheap way to store variables.

Why we use them?

To perform simple arithmetic operations that output probably integers, programmers can use int data type, but if working on fractional data, where int cannot works and double data type use to get output with accuracy.

Conclusion

In the above article, differentiating between int and double data type. Int is 2’s complement representation of the integer, which only stores whole numbers, and double is a primitive data type that is used to calculate fractional values with double precision.

Categories PHP

Leave a Comment