Hanno Fietz
Hanno Fietz

Reputation: 31360

How do I express NaN for integer types in Java?

I have a method returning long and there are cases where a valid result can not be computed. Double has the NaN constant, which is not expressable in long.

I can think of two solutions:

Is either preferrable / more "Javanic"? Are there other ways / Did I miss something obvious?

Upvotes: 23

Views: 40914

Answers (10)

Michael
Michael

Reputation: 1

Hi thanks for posting I had a similar issue. I read the Documentation on Integer.parseInt and found out that it throw a NumberFormatException if the value is not an integer. So I put my code in a try catch format and it works quite well!!

try {
                Integer gotoAddress = Integer.parseInt(gotoAddressTextField.getText());
                table.setRowSelectionInterval(gotoAddress, gotoAddress);
                table.scrollRectToVisible(table.getCellRect(gotoAddress, 1, true));


            } catch (NumberFormatException nfe) {
                JOptionPane.showMessageDialog(null, "You must enter a valid integer number!!");

            }

Upvotes: 0

pcjuzer
pcjuzer

Reputation: 2824

I wouldn't advice using a null for representing the NaN value. It can easily cause a NullPointerException later if you skip the null check. (And this kind of NullPointerException can be very hard to discover because of the implicit unboxing. You may not see directly what can be null.) Otherwise, operating with NaN values has sense: NaN+x=NaN, NaN*x=NaN.

Throwing an exception, using double (which supports NaN by the way) or using own class is better in my opinion.

Upvotes: 5

Pervaiz Ahmed
Pervaiz Ahmed

Reputation: 71

This is easy to show the NaN value, Just define a double type var and then assign value to it like

double identifier = java.lang.Double.NaN;

I had same situation so I used it and it worked. One must find a way around if a direct path is not defined..

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533472

I use MIN_VALUE to signify a NaN for long and int types. This requires additional checking and can be useful in some cases.

Upvotes: 3

Kevin Hopps
Kevin Hopps

Reputation: 727

As with most questions of this type, there is no right and wrong, and the answer is, "it depends."

It is important to consider why the valid result cannot be computed. If it is because of an invalid input, then you should throw an IllegalArgumentException.

If it is for some other reason that should not occur under normal circumstances, then some other exception is probably appropriate.

You could consider some long value that could not be a valid return value and use that to indicate an error.

Otherwise, Mr. Pollex's solution of using a Long and a null return is worth considering as well. The answer depends on what would be convenient for the majority of callers as well.

Upvotes: 2

mprabhat
mprabhat

Reputation: 20323

Any application returning NAN doesnt fit into the design perspecive, reason being NAN means something happened which is an exceptional condition, hence you should be throwing an Exception like IllegalStateException or EntityNotFoundException so that caller knows what happeded instead of NAN.

If incase your application accepts everything which is greater than 0 (zero) as valid value then you can return negative number instead of NAN which will indicate unusal path reached, if you wish not to throw an exception though I feel throwing an exception is best in such scenario,

Upvotes: 6

Chad Schultz
Chad Schultz

Reputation: 7860

My thought would be to throw an exception, because that indicates an error--the result could not be computed correctly. That's just a matter of opinion, however.

What you return really depends on your specific needs, and what values are valid. For example, if valid numbers are only 1 and higher, you could use 0 as your flag. Or you could use a negative number as your flag.

Or, as I type this, I see answers have been posted, suggesting returning Long and setting it null. Then that can be autoboxed. That is also a perfectly correct answer.

If you need to return more data from your method, you could even define your custom class that could contain the long and other properties containing data about the computation or result.

But again, my personal preference would be the exception.

Upvotes: 1

bpgergo
bpgergo

Reputation: 16037

I would cast my vote for throwing an exception

or as others suggested, return null value

Upvotes: 1

Michael Borgwardt
Michael Borgwardt

Reputation: 346260

Simply use a wrapper class (i.e. java.lang.Integer, java.lang.Long), which allows null values.

Upvotes: 7

Björn Pollex
Björn Pollex

Reputation: 76778

You can make the return-type Long (the boxed version of long) and return null.

Upvotes: 8

Related Questions