Java Convert
Here we will learn in detail some java conversion e.g How to convert String to int, int to String, String to long, long to String, String to float, float to String, String to double, double to String. This conversion is also very important in java programming interviews.
So let's starts this beautiful java conversion concept.
( 1 ) Java String to int
We can easily convert String to int by using Integer.parseInt(String s) method.
Method Declaration
The parseInt is the static method of Integer class which is wrapper class in java. For example
public static int parseInt(String s)
Java String to int Example
This is a simple java convert String to int example.
public class StringToIntExample
{
public static void main(String args[])
{
String st = "50";
int i = Integer.parseInt(st);
System.out.println(i);
}
}
output : 50
( 2 ) Java int to String Example
We can easily convert int to String by using String.valueOf(int i) and Integer.toString(int i) methods.
Method Declaration
The valueOf is the static method of String class in java. For example
public static String valueOf(int i)
Java int to String Example
This is a simple java convert int to String example.
public class IntToStringExample
{
public static void main(String args[])
{
int i = 500;
String st = String.valueOf(i);
System.out.println(st);
}
}
output : 500
( 3 ) Java String to long
Method Declaration
The parseLong() is the static method of Long class in java. For example
public static long parseLong(String s)
Java String to long Example
This is a simple java convert String to long example.
public class StringToLongExample
{
public static void main(String args[])
{
String st = "132547698";
long l = Long.parseLong(st);
System.out.println(l);
}
}
output : 132547698
( 4 ) Java long to String
We can easily convert long to String by using String.valueOf(long l) and Long.toString() method.
Method Declaration
The valueOf() is an static method of String class and it is an overloaded method in java. For example
public static String valueOf(long l)
Java long to String Example
This is a simple java convert long to String example.
public class LongToStringExample
{
public static void main(String args[])
{
long l = 12121212121212L;//add L
String s = String.valueOf(l);
System.out.println(s);
}
}
output : 12121212121212
( 4. 1 ) Java long to String - Using toString()
We can easily convert long to String by using Long.toString() method.
Method Declaration
The toString() method is the static method of Long class in java. For example
public static String toString(long l)
Java long to String Example - Using toString() method
This is a simple java convert to long to String by using toString() method.
public class Simple
{
public static void main(String args[])
{
long l = 123456789L;
String s = Long.toString(l);
System.out.println(s);
}
}
output : 123456789
( 5 ) Java String to float
We can easily covert String to float by using Float.parseFloat(String s) method.
Method Declaration
The parseFloat() is the static method of Float class in java. For example
public static int parseFloat(String s)
Java String to float Example
This is a simple java convert String to float example.
public class StringToFloatExample
{
public static void main(String args[])
{
String st = "5.6";
float f = Float.parseFloat(st);
System.out.println(f);
}
}
output : 5.6
( 6 ) Java float to String
We can easily convert float to String by using String.valueOf() and Float.toString() method.
Method Declaration
The valueOf() is the static method of String class in java. For example
public static String valueOf(float f)
Java float to String Example
This is a simple java convert float to String example.
public class FloatToStringExample
{
public static void main(String args[])
{
float f = 11.5F;//add F
String st = String.valueOf(f);
System.out.println(st);
}
}
output : 11.5
( 7 ) Java String to double
We can easily convert String to double by using Double.parseDouble() method.
Method Declaration
The parseDouble() is the static method of Double class in java. For example
public static double parseDouble(String s)
Java String to double Example
This is a simple java convert String to double example.
public class StringToDoubleExample
{
public static void main(String args[])
{
String st = "44.7";
double d = Double.parseDouble(st);
System.out.println(d);
}
}
0 comments:
Post a Comment