Type Casting in Java ?

Type casting is used when you assign a value of one primitive data type to another type. 4 types of type casting are used in java. Here is how to use.

1. Integer.parseInt(String)

The Integer.parseInt(String) is used to convert string data type to Integer data type .

class abc
{
public static void main(String arg[])
{
String s=”94″;
int b=Integer.parseInt(s);
System.out.println(b);
}
}

2. Double.parseDouble(String)

The Double.parseDouble(String) is used to convert String data type to double data type .

class abc
{
public static void main(String arg[])
{
String s=”54″;
Double b=Double.parseDouble(s);
System.out.println(b);
}
}

3. charAt(int)

The charAt(int index) method returns the character at the specified index in a string .

class abc
{
public static void main(String arg[])
{
String s=”JAVA”;
char ch=s.charAt(0); //j index is 0
System.out.println(ch);
}
}

4. Float.toString(float)

The Float.toString(float) is used to convert float into string value.

class abc
{
public static void main(String arg[])
{
float s=34.3f;
String b=Float.toString(s);
System.out.println(b);
}
}

5. Integer.toString(int)

The Integer.toString(int) is used to convert integer type value into String type value .

class abc
{
public static void main(String arg[])
{
int s=76;
String b=Integer.toString(s);
System.out.println(b);
}
}

Amandeep kaur: Amandeep Kaur is passionate about programming and is working on Android development from her school days. She has worked on many open source projects and now part of Hoodu Digital solutions as a Android developer.