Datatype conversion in Java (Typecasting)

Sharing is Caring

This post is for beginners, See how to do basic datatype conversion in Java

  •     Convert Integer to String
  •     BigDecimal to Integer
  •     Integer to BigDecimal
  •     Double to Integer
  •     Integer to double

package practiceJava;

import java.math.BigDecimal;

public class TypeConversion {
public static void main(String[] args) {
/*Convert String to Integer*/
String name = "1234";
Integer nm = Integer.parseInt(name);
System.out.println("Name in Integer-->" + nm);
}

/*Convert Integer to String*/

Integer a = 23;
String num = a.toString();

    /*BigDecimal to Integer*/    BigDecimal nom = new BigDecimal(450.9);
    Integer nomInt = nom.intValue();

/*Integer to BigDecimal*/

Integer cm = 45;
BigDecimal cmBd = new BigDecimal(cm);

/*Double to Integer*/

double sys = 3;
Integer sysInt = new Integer((int)sys);

   /*Integer to double*/    Integer sd = 456;
    double sdDb = sd.doubleValue();

}

 

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *