The following examples shows you how to convert data between int and other data types i.e., int to string , int to float , float to int etc....
Code
int to String :
int a = 10;
String str = Integer.toString(a);
System.out.println(str);
Using wrapper class Integer of int data type.
Integer a = 10;
String str = a.toString();
System.out.println(str);
String to int :
Note: String data type can hold integer value as well.
String number = "4321";
int a = Integer.parseInt(number);
System.out.println(a);
int to float :
int a = 2;
float f = a;//upcasting from smaller data type to bigger data type
System.out.println(f);
float to int :
float f = 1.6f;
int a = (int)f;//but here point value will be lost due to downcasting
System.out.println(a);
0 comments:
Post a Comment