Java Program to Convert int into double
What is int in Java ?
“int” is a primitive data type in Java that is used to represent integers (whole numbers) within a certain range of values. The range of values that can be stored by an “int” is from -2^31 to 2^31-1. The “int” data type is commonly used in mathematical operations and to represent arrays of integers.
What is char in Java ?
“double” is a primitive data type in Java used to represent a floating-point number (a number with a decimal point) with double precision, which means that it can store a wider range of values with more accuracy than a float. The double data type takes up 8 bytes of memory and has a range of values from approximately 4.9 x 10^-324 to 1.8 x 10^308. In Java, double values are written with a decimal point, such as “1.23” or “0.0”.
Ways to Convert int into double :
Example 1 :
public class Main { public static void main(String[] args) { int myInt = 42; double myDouble; // Typecast operator myDouble = (double) myInt; System.out.println("int value: " + myInt); System.out.println("double value: " + myDouble); } }
Output :
int value: 42 double value: 42.0
Example 2 :
public class Main { public static void main(String[] args) { int myInt = 42; double myDouble; // Double.valueOf() method myDouble = Double.valueOf(myInt).doubleValue(); System.out.println("int value: " + myInt); System.out.println("double value: " + myDouble); } }
Output :
int value: 42 double value: 42.0
Example :
public class Main { public static void main(String[] args) { int myInt = 42; double myDouble; // Arithmetic myDouble = myInt + 0.0; System.out.println("int value: " + myInt); System.out.println("double value: " + myDouble); } }
Output :
int value: 42 double value: 42.0
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment