• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

When should ?int be used instead of int?

 
Ranch Hand
Posts: 1463
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In C# one can use ?int which is nullable int. When should it be used ? Earlier when this feature was not there how was the same thing achieved  ?

Thanks
 
Saloon Keeper
Posts: 12008
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You mean int?, not ?int.

It's really just syntactic sugar for Nullable<int>. Java has the same thing with OptionalInt, but didn't add special syntax to the language.

You use it when you want to pass or return an int optionally, or null.

Since its underlying type is Nullable<T>, you can use the null-propagating (?.) and the null-coalescing (??) operators on instances of int? and the default operator will return null when used in a context where int? is expected.
 
Monica Shiralkar
Ranch Hand
Posts: 1463
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:You mean int?, not ?int.
.



I am talking about the one as below :

 
Stephan van Hulst
Saloon Keeper
Posts: 12008
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, as you can see the question mark comes after the value type.

Has your question been answered sufficiently?
 
Monica Shiralkar
Ranch Hand
Posts: 1463
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. For instance, in case of Java optionalInt was introduced in java 1.8 to pass or return an int optionally or NULL.How was the same thing being done before that ?
 
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Thanks. For instance, in case of Java optionalInt was introduced in java 1.8 to pass or return an int optionally or NULL.

Only write NULL when writing C please. It's called null in Java®.

How was the same thing being done before that ?

There wasn't a “standard way” to do that before.
 
Monica Shiralkar
Ranch Hand
Posts: 1463
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok. What can be an example scenario when it should be used instead of int ?
 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Where what should be used instead of an int? Do you mean OptionalInt?
The originally intended use case for OptionalInt is with a 0‑length IntStream. If you try to work out the average of the values in a 0‑length IntStream, you can only come up with a nonsense value as a result, so you return an empty OptionalInt.

Well, actually, if you look at the IntStream#average() method, you find it calculates the average as a double, but it is the same concept.

[addition]Remember I am looking at Java® code, not C#.
 
Monica Shiralkar
Ranch Hand
Posts: 1463
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. Is returning an integer or a NAN be an example use case whether either a number is returns or null in case of NAN ?
 
Master Rancher
Posts: 3537
39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Thanks. For instance, in case of Java optionalInt was introduced in java 1.8 to pass or return an int optionally or NULL.How was the same thing being done before that ?


I would say that the most common way I've seen it done in Java is to simply replace a primitive with a wrapper... i.e. replace int with java.lang.Integer.  This allows all the same values as an int, plus null.  It's probably slower, in ways that very rarely make a real difference.  The most common use case that I've seen is for a POJO or DTO class which represents data from a database... if a column in the database is nullable (as most are by default), then you need a way to represent a null value.  In various specific contexts it may make sense to replace a null with a default like 0 or -1 - but as a general policy that's probably unwise.  So many ORMs will, by default, use a nullable wrapper class like Integer to represent a value that could be null.

Monica Shiralkar wrote:Thanks. Is returning an integer or a NAN be an example use case whether either a number is returns or null in case of NAN ?


Well, there is no integer NAN value, that's a floating point thing.  But yes, this is an example where you might want to use a null to represent a value that is somehow impossible to determine.  For example, using normal integer division, dividing by zero results in an ArithmeticException.  You may decide that for your application, you don't want that, but instead want to store the value as null.  In that case, you can do something like

Or with more modern code

This particular example it probably would have made more sense to express the result as a double, which also allows NaN and infinity, and then you would again have no real need for null... but in different applications, interacting with different systems, you may find that some system needs a null value to mean something in particular, so you create the code that is needed for that situation.
 
Monica Shiralkar
Ranch Hand
Posts: 1463
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.  In the above code ,what is the advantage of the second code over the first one ?
 
Stephan van Hulst
Saloon Keeper
Posts: 12008
257
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It makes it explicit that the function might not have an answer. It forces the client to check that a result is present, because the first code snippet will cause a NullPointerException if the client forgets to check the result.
 
Monica Shiralkar
Ranch Hand
Posts: 1463
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:It makes it explicit that the function might not have an answer. It forces the client to check that a result is present, because the first code snippet will cause a NullPointerException if the client forgets to check the result.



Thanks.Understood that now .
I wonder how was this being handled before Java 1.5 when the wrapper classes like Integer were introduced.
 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have already told you that.

Monica Shiralkar wrote:. . . before Java 1.5 when . . . .

The wrapper classes have been there from the very beginning.
 
Monica Shiralkar
Ranch Hand
Posts: 1463
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.Sorry ,I confused that with Autoboxing.

So does that mean that in case of objects of type OptionalInt ,we are not required to do a null check before using it which is often a practice with other objects  ?
 
Campbell Ritchie
Marshal
Posts: 69411
276
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You make sure never to allow a reference or returned value of type Optional<T> or OptionalSomethingElse be null. If you go through the different types of Optionals' documentation pages, you will find there are several methods for getting the value; you are probably better off not using get().
 
I found some pretty shells, some sea glass and this lovely tiny ad:
ScroogeXHTML 9.0 - RTF to HTML5 and XHTML converter
https://coderanch.com/t/731949/ScroogeXHTML-RTF-HTML-XHTML-converter
    Bookmark Topic Watch Topic
  • New Topic