In this core java tutorial we will learn how to convert String to long primitive type in java and convert String to long object with program and examples in java. We will also understand When to use convert String to long primitive type and when when to convert String to long object in java.
Also read : convert Array to Set and Set to array in Java
Programs to convert String to long in java -
- Program/Example 1 to How to convert String to long primitive type in java.
- Program/Example 2 to How to convert String to long object in java.
Program/Example 1 to How to convert String to long primitive type in java
package convertStringToLongPkg;
public class ConvertStringToLongExample1 {
public static void main(String[] args) {
String string = "12";
try {
long l = Long.parseLong(string);
System.out.println("long number = " + l);
} catch (NumberFormatException e) {
e.printStackTrace();
}
}
}
/*OUTPUT
long number = 12
*/
|
If in case invalid string is passed which can't get converted to long will throw NumberFormatException in java.
Program/Example 2 to How to convert String to long object in java
Long.valueOf(string) method returns long object.
package convertStringToLongPkg;
public class ConvertStringToLongExample2 {
public static void main(String[] args) {
String string = "12";
Long longObject = Long.valueOf(string);
System.out.println("long longObject = " + longObject);
}
}
/*OUTPUT
long longObject = 12
*/
|
Programs to convert long to String in java -
- Program/Example 3 to How to convert long primitive type to String in java.
- Program/Example 4 to How to convert long object to String in java
Now, in this core java tutorial we will learn how to convert long primitive type to String in java and convert long object to String with program and examples in java. We will also understand When to use convert String to long primitive type and when when to convert String to long object in java.
Program/Example 3 to How to convert long primitive type to String in java.
String.valueOf(long) method converts long primitive type to String in java.
package convertLongToStringPkg;
public class ConvertLongToStringExample1 {
public static void main(String[] args) {
long l = 12L;
String string = String.valueOf(l);
System.out.println("string = "+string);
}
}
/*OUTPUT
string = 12
*/
|
Program/Example 4 to How to convert long object to String in java
toString() method of java.lang.Long class convert long object to String in java.
package convertLongToStringPkg;
public class ConvertLongObjectToStringExample2 {
public static void main(String[] args) {
Long longObject = 12L;
String string = longObject.toString();
System.out.println("string = "+string);
}
}
/*OUTPUT
string = 12
*/
|
Summary -
- In this core java tutorial we learned conversion of String to long primitive type in java and convert String to long object with program and examples in java.
- we also learned conversion of long primitive type to String in java and convert long object to String with program and examples in java.
Having any doubt? or you you liked the tutorial! Please comment in below section.
Please express your love by liking JavaMadeSoEasy(JMSE) on facebook, following on google+ or Twitter.
RELATED LINKS>