Convert String to long in Java
ποΈ Java Fun: Converting Strings to Longs π’β
Ever found yourself in a Java jungle trying to convert a String into a Long? Fear not, brave coder! Let's explore three ways to achieve this transformation while having a bit of fun. π
β‘ Quick Referenceβ
String number = "2018"; // A String, not just a number!
long value1 = Long.parseLong(number, 10); // The classic way!
long value2 = Long.valueOf(number); // Another solid choice!
long value3 = new Long(number); // Deprecated, but hey, itβs an option!
π οΈ 1. Using Long.valueOf(String)
β
Think of Long.valueOf()
as a magic decoder ring for numbers in String disguise. It takes a String, checks if it's made of decimal digits, and poof!βout comes a long. The first character can be a +
or -
, but no funny business, or it throws a tantrum (NumberFormatException). π€―
π Exampleβ
String positiveNumber = "+12001";
long value1 = Long.valueOf(positiveNumber); // 12001L
String negativeNumber = "-22002";
long value2 = Long.valueOf(negativeNumber); // -22002L
π¨ Warning! Try sneaking in a non-number character, and Java will catch you red-handed:
Assertions.assertThrows(NumberFormatException.class, () -> {
Long.valueOf("alexa");
});
π― 2. Using Long.parseLong(String)
β
This method is valueOf()
's twin, just without the extra wrapping. Same rules applyβonly decimal digits and an optional +
or -
sign are allowed. It does the job, and it does it well! π
ποΈββοΈ Exampleβ
String positiveNumber = "+12001";
long value1 = Long.parseLong(positiveNumber); // 12001L
String negativeNumber = "-22002";
long value2 = Long.parseLong(negativeNumber); // -22002L
π΅οΈββοΈ Bonus Trick: Different Number Basesβ
Want to parse a hexadecimal (base 16) number? No problem!
String numberInHex = "-FF";
long value = Long.parseLong(numberInHex, 16); // -255L
π 3. Using new Long(String)
Constructor (Old School)β
Back in the day (before Java 9), you could create a Long object using the new Long(String)
constructor. But like disco pants, this approach is deprecated. πΊ
long value = new Long("100"); // 100L
Use parseLong()
insteadβitβs faster, cooler, and Java wonβt yell at you. π
β οΈ 4. Beware the NumberFormatException
Monster! πβ
If your input string contains anything other than decimal digits (and an optional sign at the start), Java will throw a NumberFormatException
at you faster than you can say "Oops!" π±
String number = "12001xyz";
long value = Long.parseLong(number);
π₯ Boom! Runtime error incoming:
Exception in thread "main" java.lang.NumberFormatException: For input string: "12001xyz"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
at java.lang.Long.parseLong(Long.java:589)
at java.lang.Long.<init>(Long.java:965)
at com.howtodoinjava.StringExample.main(StringExample.java:9)
So, keep those Strings clean, stick to digits, and happy coding! π