Numeric Data Types and Methods in Java
1. Converting a string to an integer using Integer.parseInt
The parseInt method converts a String representing an integer value into an actual int data type.
String rollNoString = “123”;
int rollNo = Integer.parseInt(rollNoString); // rollNo is 123
rollNoString: A string that holds the value “123”.
Integer.parseInt(rollNoString): Converts the string “123” into the integer 123.
The value is stored in the variable rollNo.
2. Converting a string to a float using Float.parseFloat
The parseFloat method converts a String representing a floating-point number into a float data type.
String weightString = “55.5”;
float weight = Float.parseFloat(weightString); // weight is 55.5f
weightString: A string that holds the value “55.5”.
Float.parseFloat(weightString): Converts the string “55.5” into the float 55.5f.
The value is stored in the variable weight.
3. Converting a string to a double using Double.parseDouble
The parseDouble method converts a String representing a floating-point number into a double data type.
String piString = “3.14”;
double piValue = Double.parseDouble(piString); // piValue is 3.14
piString: A string that holds the value “3.14”.
Double.parseDouble(piString): Converts the string “3.14” into the double 3.14.
The value is stored in the variable piValue.
4. Converting a string to a BigDecimal
BigDecimal is a class in Java used for precise calculations, especially with financial or large-scale numerical data. We use BigDecimal when we need better precision.
String balanceString = “19.99999”;
BigDecimal balance = new BigDecimal(balanceString); // balance is 19.99999
balanceString: A string that holds the value “19.99999”.
new BigDecimal(balanceString): Creates a BigDecimal object with the value 19.99999.
The value is stored in the variable balance.
5. Converting a double to a string using Double.toString
The toString() method converts a numeric type to its String representation.
double piValue = 3.14159;
String piStringValue = Double.toString(piValue);
System.out.println(“toString() output: ” + piStringValue); // Output: “toString() output: 3.14159”
piValue: A double with the value 3.14159.
Double.toString(piValue): Converts the double 3.14159 to the string “3.14159”.
The string is stored in the variable piStringValue.
Output: The string “3.14159” is printed.
6. Getting the class of an object using getClass()
The getClass() method returns the runtime class of an object. Here, we cast the primitive double to Object to call this method.
double piValue = 3.14159;
Class<?> doubleType = ((Object) piValue).getClass();
System.out.println(“getClass() output: ” + doubleType); // Output: “getClass() output: class java.lang.Double”
piValue: A double with the value 3.14159.
((Object) piValue).getClass(): Converts the primitive double into an object and returns its class type, which is java.lang.Double.
Output: The class name is printed as class java.lang.Double.
7. Checking if two double values are equal using equals()
The equals() method compares two Double objects for equality.
String weightString = “3.14”;
double weight1 = Double.parseDouble(weightString);
double weight2 = 3.14;
boolean isEqual = Double.valueOf(weight1).equals(weight2);
System.out.println(“equals() output: ” + isEqual); // Output: “equals() output: True”
weight1: A double value converted from the string “3.14”.
weight2: A double value 3.14.
Double.valueOf(weight1).equals(weight2): Converts weight1 into a Double object and compares it with weight2. Returns true if they are equal.
Output: “True” is printed because both values are equal.
8. Comparing two double values using compareTo()
The compareTo() method compares two Double values and returns:
-1 if the first value is less than the second,
0 if they are equal,
1 if the first value is greater than the second.
double height1 = 1.14;
double height2 = 2.71;
int result = Double.compare(height1, height2);
System.out.println(“compareTo() output: ” + result); // Output: “compareTo() output: -1”
height1: A double value 1.14.
height2: A double value 2.71.
Double.compare(height1, height2): Compares height1 and height2. In this case, 1.14 < 2.71, so it returns -1.
Output: “-1” is printed, indicating that height1 is less than height2.
Mathematical Operations
int a = 10;
int b = 5;
Mathematical Operation | syntax | Output |
---|---|---|
Addition | sum = a + b | 15 |
Subtraction | difference = a – b | 5 |
Multiplication | product = a * b | 50 |
Division | quotient = a / b | 2 |
Modulus | remainder = a % b | 0 |
Tasks:
Write a program to convert a string to an integer using Integer.parseInt.
Write a program to convert a string to a float using Float.parseFloat.
Write a program to convert a string to a double using Double.parseDouble.
Write a program to convert a string to a decimal using BigDecimal.
Write a program to convert any double value to a string using the Double.toString method and print the result.
Write a program to get and print the type of any variable using getClass().getSimpleName.
Write a program to check whether two double values are equal using the equals method and print the result.
Write a program to compare two double values using the compareTo method and print the result.
Write a program to perform subtraction, addition, multiplication, and division on two integers and print the results.