This question already has answers here:
Null check in Java 8 Elvis operator?
(2 answers)
Closed 5 years ago.
I have a situation where I need get the property of a Java object if the object exists or null.
Something like foo == null ? null : foo.bar
Is there an operator available in java to do the same?
No, there is no propagate null operator in Java, cf. C# for example, which does have one. (It was a proposal at some point in Java's evolution, but has not sadly yet been incorporated into the language: foo = foo?.bar would be an obvious notation.)
You need to write this out longhand, as you have done.
Related
This question already has answers here:
How to convert Optional<Object> to Optional<String> [closed]
(3 answers)
Closed 2 years ago.
I am trying to convert a set to string inside Optional.OfNullable method like:
test.setAbc(Optional.ofNullable(rule.getSampleSet().toString()).orElse(null));
but if sampleSet is null it will give me a NullPointerException.
can anyone tell me how to resolve this issue using .map method with Optional?
I know one traditional way of doing it by checking nullability beforehand:
if(rule.getSampeSet != null)
but I am much interested in knowing if we can do it in one line.
Instead of calling toString() inside the ofNullable, you could map the optional to it:
test.setAbc(Optional.ofNullable(rule.getSampleSet()).map(Object::toString).orElse(null));
This question already has answers here:
Interview : Java Equals
(7 answers)
Closed 6 years ago.
I have seen in many places that the constant value is used first and then the variable for example ("ram").equals(a) and in many places I have seen that the variable is used first and then the constant value with which they want to compare for example a.equals("ram").
what is the difference between a.equals("ram") and ("ram").equals(a) ?
which one is better to use and why ?
The first style is safer in situations when variable a is allowed to be null, because you can skip null checking.
When you write
if (a.equals("ram")) { ... }
you must ensure that a is not null to avoid a null pointer exception. This is not necessary for
if ("ram".equals(a)) { ... }
because equals() method is required to process null arguments without throwing an exception:
For any non-null reference value x, x.equals(null) should return false.
("ram").equals(a) is better. The code will never break even if a is null.
saves us from null pointer exception.
This question already has answers here:
No Exception while type casting with a null in java
(10 answers)
Closed 7 years ago.
When we are using System.out.println(null) then we get compilation error and to resolve this we typecast null like:
System.out.println((String)null)
and then it starts working fine.
My questions is how we are allowed to typecast null when we say that null is something which is not pointing to any location and thus has no value in itself.
Also if null is not pointing to any location then the object which is null will get Garbage collected then why we are using it at first place? I know it is litle bit out of the topic but it came to my mind.
I have seen few answers in below link but they mostly talk about println method functionality and implementation but I want to know about null only.
No Exception while type casting with a null in java
In Java, "null" is a special literal of the null type. It can be cast to any reference type, but not to any primitive type such as int or boolean. The null literal doesn't necessarily have value zero. And it is impossible to cast to the null type or declare a variable of this type.
That's why it does not throw error you can also do this null==null
This question already has answers here:
Optional vs. null. What is the purpose of Optional in Java 8? [duplicate]
(3 answers)
Closed 7 years ago.
I understand that Optionals ifPresent() call is meant to replace a null check. Pulling code examples from the Oracle documentation, it appears to be very useful in simple situations. For example:
Optional<Soundcard> soundcard = ...;
soundcard.ifPresent(System.out::println);
I just want to understand why this is considered better than a null check. Readability? Performance? It seems to me that this would cause a hit to project performance, as a new object must be introduced in order to hold the object we ultimately wish to obtain? In full, why is this soundcard.ifPresent()
considered better than this if(soundcard != null).
Optional is a way of replacing a nullable reference with a non-null value. An Optional may either contain a non-null reference (in which case we say the reference is "present"), or it may contain nothing (in which case we say the reference is "absent"). It is never said to "contain null."
And besides readability it forces you to think about the absent case if you want your program to compile at all, since you have to actively unwrap the Optional and address that case.
Source: Using and Avoiding null
And as referred before, have a look at this thoroughly answered post.
This question already has answers here:
Calling Java varargs method with single null argument?
(6 answers)
Closed 8 years ago.
I found myself checking for this and asking if it's necessary. I have code like this:
public Object myMethod(Object... many) {
if (many == null || many.length == 0)
return this;
for (Object one : many)
doSomethingWith(one);
return that;
}
But then I wondered... Am I being too cautious? Do I have to check if many == null? Is that ever possible in any current Java version? If so, how? If not, I'll probably keep checking, just for futureproofing in case Oracle decides it can be null one day.
I tried it with Java HotSpot(TM) 64-Bit Server VM, Java 1.8.0 on Linux 3.5.0-21-generic
Using myMethod(null) doesn't pass new Object[]{null}, it passes null. Arrays are objects in Java and therefore null is valid for the type Object[].
Your nullcheck is, therefore, not overly cautious.