Java Code Example: convert specific datatype to int

In Java it is possible to convert data types into other data types. For example, if you want to convert the data type long to int, a type conversion is performed. When converting string to int the parseInt() method of the Integer wrapper class is called.

The parseInt() method parses the string argument as a signed decimal integer. The characters in the string must all be decimal digits, except that the first character may be an ASCII minus sign ‘-‘ (‘\u005Cu002D’) to indicate a negative value or an ASCII plus sign ‘+’ (‘\u005Cu002B’) to indicate a positive value.

public class ConversionToInt {
  public static void main(String args[]){  
    double doubleNum = 1.6;
    String str =  "123";
    char ch = '4';
    long longInt = 200000;

    int intNum =(int)doubleNum;  
    int intStr = Integer.parseInt(str);
    int intChar = ch;
    int intLong = (int)longInt;

    System.out.println(intNum);  
    System.out.println(intStr);  
    System.out.println(intChar);  
    System.out.println(intLong);  
  }
}
Output
1
123
52
200000