java


Why immutable null is discouraged?


I'm currently using Immutable library for generating JSON object from my web application.
Looking at this chapter, the first line says:
The use of nullable attributes is discouraged.
So my question is:
1) why? what's wrong with a null object?
2) what if I'm using a wrapper of thirdy object and I don't know if item is null or not, so using the classing builder code will fail:
MyImmutableWrapperObject
.builder().
.mobile(input.getMobile()) // don't know if null or not
.build();
Is there any optimal solution?
EDIT:
#JsonProperty("mobile")
public abstract Optional<String> mobile();
...
// composing builder
if (input.getMobile() != null)
builder.mobile(input.getMobile());
The produced json is:
"mobile": {
"present": false
},
How can I completely remove the empty fields?
I read this, however it uses gson.toJson which return a String object which is not my desiderable way.
POSTEDIT:
I just found that Optional doesn't show real value even if present, but it just display the true/false value, so I don't need it.
That's exactly the point IMO, you don't know if an object is null or not; so when later using it you might obviously get a NullPointerException. By using a library that prohibits that, you will not care if something is null or not null (because it will not be null), thus less code and less if-statements that pollute the code with potential null checks.
Also having a null reference might mean different things in different contexts. For example I've seen code that needs three states for a Boolean : true, false and no yet set(as null). I find that wrong, but still occasionally see it.
That's the reason why Optional was introduced in Guava and now is in the JDK. It forces the user to actively do something with an Optional. For example:
Optional<Integer> i...
if(i.isPresent()).... // you are forced to do this check
Well obviously you could do:
i.get()
but that method is documented that it might break.
the same is simply not true for an reference:
Integer i = null;
if(i == null) // this is not an operation that is mandatory
The bet article I've seen/read to date on this subject is Guava explanation

Related Links

How to set spinner values in android those are coming from list?
Binary search now working for repetetive case of not a triangle
How to get or create new map entry
toUpperCase on Android is incorrect for two-argument and default Greek and Turkish Locales
Giving mulitple Jbuttons the same actionListener
Provide sample value for request parameter in Swagger
V/AsyncHttpRH: Progress 900 from 300 (300%) - can't do a HttpRequest
How to open URL on new window using java on production server [closed]
Gurantee switching to a specific window in Selenium
How to reset the index to 0 in an arraylist?
Depth configuration in fetch queries not using relationships directions in Neo4j OGM APIs
Jave : Date counter excluding Weekends and Business holidays [duplicate]
Calling pack() on JDialog doens't shrink it horizontally
How to create an Object using each in java from an array [duplicate]
Why CMS is stop the world for Initial mark but not for sweeping phase?
Fragment activity crash on item click

Categories

HOME
gitlab
layout
rdf
sql-server-2016
constraint-programming
amazon-cloudformation
wheelnav.js
modelica
export-to-csv
collectd
size
dtrace
seaborn
after-effects
transformation
google-static-maps
contextmenu
firefox-webextensions
nodatime
jtextfield
swingx
autocad-plugin
zapier
ehcache
sylius
mustache.php
sparse-matrix
chromebook
internet-explorer-9
replaceall
catch-all
restlet
document.write
cookiecutter-django
trim
hp-ux
spring-security-kerberos
btrace
wptoolkit
avconv
outlook-2013
azure-application-gateway
fancybox-2
carrot
spring-mongodb
account-kit
eventkit
migradoc
messenger
ableton-live
sts-springsourcetoolsuite
etsy
gridview-sorting
dataview
php-internals
ios4
system.management
drawbitmap
processmodel
sortedlist
fscommand
typekit
tablelayout
android-nested-fragment
relocation
markers
elliptic-curve
wp-query
dealloc
concurrent-collections
cloud-connect
java.nio.file
page-layout
coverflow
bulkloader
file-locking
cos
drools-planner
runas
viewswitcher
ticoredatasync
regsvr32
mercurial-server
doh
blackberry-playbook
django-tagging
table-footer
propagation
.nettiers
infrastructure
perfect-hash
webkit.net
javap

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App