>
Data types in Java: (Part 2)
- Floating point numbers:
- - Floating-point numbers, also known as real numbers, are used when evaluating expressions that require fractional precision. For example, calculations such as square root, or for calculating math functions such as sine and cosine, result in a value whose precision requires a floating-point type.
- Float:
- Use: The type float specifies a single-precision value that uses 32 bits of storage. Variables are declared as float when fractional component is needed.
- Declaration: float f = 10.1; or float f = 10.1F;
- Double:
- Use: The type double specifies a double-precision value that uses 64 bits of storage. Double precision is actually faster than single precision on some modern processors that have been optimized for high-speed mathematical calculations. All math functions, such as sin( ), cos( ), and sqrt( ), return double values. When accuracy is to be maintained over many iterative calculations, or manipulating large-valued numbers, double is used.
- Declaration: double d = 49.587; or double d = 49.578D;
- int:
- Use: useful in control loops as a control variable, also used to index arrays. If an integer expression is using bytes, shorts, ints, and literal numbers, the entire expression is promoted to int before the calculation is done. For number counting, integer math, and solving any arithmetic expression, mostly used data type is integer.
- Declaration: int i , j=5;
Comments
Post a Comment