Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Integer doesn't solve null String [closed]

Tags:

java

Integer class contains some method to convert String into Integer:

  1. Integer.parseInt(null); Parameter: String Return: int, so I expected when parameter is null this throw NPE

  2. 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 ?

like image 998
hudi Avatar asked Feb 27 '13 15:02

hudi


People also ask

Does Integer accept null?

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.

IS null a string or Integer?

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.

Can we convert empty string to int in Java?

In Java, we can use Integer. valueOf() and Integer. parseInt() to convert a string to an integer.

Is empty string considered null?

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 .


3 Answers

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     }
like image 187
PermGenError Avatar answered Oct 18 '22 23:10

PermGenError


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 :)

like image 40
Gus Avatar answered Oct 18 '22 23:10

Gus


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);
}
like image 31
Jess Avatar answered Oct 18 '22 22:10

Jess