Data Numeric
Data Numeric
---
Date: [12-12-2024]
---
and floating-point types (float, double) for decimals. For large or precise numbers, use Big Integer or Big
Decimal.
---
---
Types:
---
Types:
---
Example:
int a = 10, b = 5;
int sum = a + b; // 15
---
Example:
---
Explicit Casting (Narrowing): Requires manual casting when converting from a larger to a smaller type.
double pi = 3.14;
---
Precision loss in casting occurs when converting a data type with higher precision or range into one with
]=lower precision or range. This often happens during narrowing conversion, where some information
might be lost or truncated.
Example:
double d = 9.876;
---
Example:
import java.math.BigInteger;
---
Example:
import java.math.BigDecimal;
---
What is Overflow?
In Java, every numeric type has a fixed range of values it can hold. For example, int is a 32-bit signed
integer, so its range is:
When you try to go beyond these limits, the value wraps around to the opposite end of the range. This
is called overflow.
---
Example:
System.out.println(Integer.MAX_VALUE); // 2147483647
---
---
Example:
import java.text.DecimalFormat;
System.out.println(df.format(12345.678)); // 12,345.68
---
---
Useful methods:
Example:
System.out.println(Math.pow(2, 3)); // 8
---
---
Key points:
Use Java features like constants, Big Decimal, and formatting for precision and efficiency.