Integer class contains some method to convert String into Integer:
Integer.parseInt(null);
Parameter: String Return: int, so I expected when parameter is null this throw NPE
Integer.valueOf(null);
Parameter: String Return: Integer,so I expected when parameter is null this should return null. But this precondition was wrong and I ask why there is no such method which can handle this situation ?
Java primitive types (such as int , double , or float ) cannot have null values, which you must consider in choosing your result expression and host expression types.
The null is actually a String, which i am passing in my service layer that accepts it as an Integer. So, whenever i try to cast a null String to Integer, it throws me an exception.
In Java, we can use Integer. valueOf() and Integer. parseInt() to convert a string to an integer.
An empty string is a string instance of zero length, whereas a null string has no value at all. An empty string is represented as "" . It is a character sequence of zero characters. A null string is represented by null .
null
is not a valid representation of integer number. Integer.parseInt()
requires that the string be parsed is a vaild representation of integer number.
Integer.parseInt()
public static int parseInt(String s, int radix)
440 throws NumberFormatException
441 {
442 if (s == null) {
443 throw new NumberFormatException("null");
444 }
Integer.valueOf(str)] // which inviokes Integer.parseInt(Str) to return an Integer instance.
public static Integer valueOf(String s) throws NumberFormatException
569 {
570 return new Integer(parseInt(s, 10));
571 }
The folks at Sun who implemented Integer (a long
time ago :) ) probably were not thinking of databases when they wrote that method. Except when dealing with database data, or rare cases where you are trying to explicitly represent "unknown" with null, null is usually a sign of something gone terribly wrong. In general, it's a good idea to raise an exception as soon as there is a problem. (a Fail fast design)
If you have ever spent time hunting down a segmentation fault in C that is due to a string value longer than the memory you supplied for it (which then overwrote some program code or other data) you will have a very good appreciation of how bad it is to not fail when something has gone wrong.
Since the time of Java 1.0, interaction with databases in java has become extremely common so you might be right to suggest that there should be a method to handle this. Integer is a final class so if you build your own Integer like class you will loose autoboxing, so this probably does require a change to the language by oracle.
Basically, what you observed is the way it is for now, and someone will have to pay you to code around it :)
I could be wrong, @hudi, but is this what you are looking for?
public Integer valueOf2( String inputString ) {
return (inputString == null) ? null : Integer.parseInt(inputString);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With