Skip to main content

Java Data conversion

Data conversion or type conversion is a way to interchange value of one data type into another data type. That is also known as Type casting. Generally there are two type of casting are supported in java programming. Implicit data type casting and Explicit data type casting.

Implicit casting : Implicit casting are not required any custom conversion. That is automatic works when assign the small data type value into relatvent higher data type values.

public class TypeCasting{

  public static void main(String[] args){ 
     //Implicit Type Casting
    int  intData = 100;
    long longData = intData;
    System.out.println("intData : "+intData);
    System.out.println("longData : "+longData);

  }
}
intData : 100
longData : 100

let's see an another example, when assign character data value to an integer in will automatic convert character value to integer value.

public class TypeCasting{

  public static void main(String[] args){ 
     //Implicit Type Casting
    
    int i = 'A';//assign char value
    System.out.println("int data : "+i);
  }
}
int data : 65

Explicit casting : Explicit casting operation are required when convert higher data type value to small data type value.In this casting before the converted data type value (variable or constant value) within the parenthesis define converted data type. For example.

//Convert long data to integer data type
int i = (int) 2123L; 
//Convert double data to float data type
float f = (float) 45.24; 

 //Convert float data to integer data type
int intData = (float) 45.24;  

(byte), (float), (int), (double), (long), (char), (short) are valid type formatted sequence. Let see an example.

public class TypeCasting{

  public static void main(String[] args){ 
     //Explicit Type Casting
    
    long longData = 10000L;
    int  intData = (int)longData;
    
    System.out.println("intData : "+intData);
    System.out.println("longData : "+longData);

  }
}
intData : 10000
longData : 10000

In this situation there are possible to lose actual data type value. For example when convert floating data value to an integer in this case precision are lost in this conversion.

public class TypeCasting{

  public static void main(String[] args){ 
     //Explicit Type Casting

    float f = 10.25f;
    int  i = (int)f;
    
    System.out.println("int data : "+i);
    System.out.println("flaot data : "+f);

  }
}
int data : 10
flaot data : 10.25

Boolean data type value are not converted to any other data type value and you'll cannot converted to other data type to boolean.

Standard methods of type casting

String conversion

In java programming toString() method are Convert numeric values into string. This method can applicable to convert numeric and arrays. See this example.

import java.util.Arrays; //For Arrays
public class TypeCasting{

  public static void main(String[] args){ 
   
    
    int intData = 10;
    //Convert integer to string
    String int_str = Integer.toString(intData);

    float floatData = 178.10f;
    //Convert float to string
    String float_str = Float.toString(floatData);

    double doubleData = 178.19;
    //Convert double to string
    String double_str = Double.toString(doubleData);

    int [] arrayData  = new int[]{1,2,3,4};
    //Convert array to string
    String array_str = Arrays.toString(arrayData);

    System.out.println(int_str);
    System.out.println(float_str);
    System.out.println(double_str);
    System.out.println(array_str);
  }
}
10
178.1
178.19
[1, 2, 3, 4]

There are many other ways are possible to formatting an converting string values.

Example 1

import java.text.DecimalFormat; //for DecimalFormat

public class TypeCasting{

  public static void main(String[] args){ 
   
    int intData = 20510;

    DecimalFormat simpleDecimalFormat = new DecimalFormat("#");
    DecimalFormat specialDecimalFormat = new DecimalFormat("#0,000");

    String simple  = simpleDecimalFormat.format(intData);
    String special = specialDecimalFormat.format(intData);
    
    System.out.println(simple);
    System.out.println(special);
  }
}
20510
20,510

Example 2

public class TypeCasting{

  public static void main(String[] args){ 
   
    int intData = 1234;
    String stringInt = String.format ("%s", intData);
    System.out.println(stringInt);
  }
}
1234

Example 3

public class TypeCasting{

  public static void main(String[] args){ 
   
    int intData = 123;
    String stringInt = String.valueOf(intData);
    System.out.println(stringInt);
  }
}
123

Double Data conversion

import java.lang.Integer; //for  doubleValue()

public class TypeCasting{

  public static void main(String[] args){ 
    //Integer objects
    Integer intData =new Integer(10);

    //Convert Intger obj data to double
    double doubleInt = intData.doubleValue();

    String str = "12.34";
    //Convert string data to double
    double doubleStr = Double.parseDouble(str);
  
    System.out.println(intData);
    System.out.println(doubleStr);

  }
}
10
12.34

Float Data conversion

public class TypeCasting{

  public static void main(String[] args){ 
   
    String str = "12.34";
    //Convert string data to float
    float floatStr = Float.parseFloat(str);
  
    System.out.println(floatStr);

  }
}
12.34




Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment