String to Long Example in Java

public class StringToLong {
	public static void main(String[] args) {

		String stringValue="120";
		
		//Constructs a newly allocated Long object
		//NumberFormatException will be thrown, if the string does not contain a parsable number
		Long longValue1 = new Long(stringValue);
		System.out.println(longValue1);
		
		//Returns a Long object holding the long value represented by the string. 
		//NumberFormatException will be thrown, if the string does not contain a parsable long.
		Long longValue2 = Long.valueOf(stringValue);
		System.out.println(longValue2);
		
		//Returns a new long primitive of Stringvalue
		//If stringValue is null, then a NullPointerException 
		//NumberFormatException will be thrown, if the string does not contain a parsable long.
		long longValue3= Long.parseLong(stringValue);
		System.out.println(longValue3);
		
	}
}
Output
120 120 120










Related Examples


Write a Comparable to compare students based upon their age?
Simple Array Sorting Program
Write a program to convert from decimal to binary?
Write a program to get list of all the files from a given directory?
Producer Consumer Example in Thread
Write a program to reverse a String?
Write a Comparator to compare students based upon their name and age?








Receive Latest Java Examples in Your Email: