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 can I integrate Woocommerce into an Android Application?
Java Futures: Could someone explain me the program flow
Intellij duplicate class found in the file
Jdk update 121 URLClassloader change
Connection to db isn't closed after query ends
How can I display multiple JPanel's from different classes onto my JFrame?
Memory consumption from drawString method out of control. Memory leak? (Graphics)
get Data from DB using Spring MVC to show in dropdown list like Country, state, city
Best way to verify text in a table is between 2 specified items - Selenium - Java
Is it standard practice create an instance of a class within that class?
zk framework: message 'unknown message code:<code>' instead of standart labels
RESTClient cannot connect to Oauth 2.0 server
Spontaneous up spring boot context in test
Not able to access Second Database using Spring batch
Java program that parses words from a LaTeX file
Saving a dataframe as text file without using databricks API

Categories

HOME
maven
listview
debugging
iterator
q
routes
filtering
tizen-web-app
leon
packages
podio
synchronization
jplayer
pc
angular-ui
sms-gateway
errorlevel
facebook-instant-articles
core-text
mmap
firebase-crash-reporting
smb
one-to-many
apache-commons-io
windows-error-reporting
vision
google-cloud-nl
gsmcomm
r-raster
lightswitch-2013
react-chartjs
javascriptcore
ecto
phonegap
http-status-code-503
karaf
column-family
avcapturesession
geopositioning
http-referer
git-merge
adobe-premiere
retina-display
theano.scan
jna
leading-zero
squib
galen
no-www
sencha-touch-2.3
convertapi
pango
theming
pebble-watch
git-diff
composite-key
qtwebview
memory-alignment
gnome-shell-extensions
specrun
angular-cache
file-writing
autorest
hill-climbing
whois
independentsoft
ford-fulkerson
libressl
angular-leaflet-directive
android-listview
ionic
appfabric-cache
angular-local-storage
npapi
sorl-thumbnail
cdt
mdt
code-access-security
xojo
project-planning
comexception
prettify
terminal-services
quantlib-swig
jquery-mobile-dialog
cassini-dev
angularjs-controller
manchester-syntax
getmessage
wse3.0
transactionscope
floating
digest-authentication
ajax-forms

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