JavaNo Comments

default thumbnail

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);
}
}

Be the first to post a comment.

Add a comment