Can a Java varargs be null? [duplicate] - java

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.

Related

Avoiding null check when accessing an object property in java [duplicate]

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.

local variable or repeated calls? [duplicate]

This question already has answers here:
Java optimization : local variable or function call
(5 answers)
Closed 6 years ago.
I have a very basic question. Which if the below 2 is better performance-wise:
if (getSomeValue() != null) {
processSomeValue(getSomeValue());
}
OR
String someValue = getSomeValue();
if (someValue != null) {
processSomeValue(someValue);
}
getSomeValue() is a normal getter which does not do anything else.
A best practice is to always use the 2nd way even you already know that the getSomeValue() is a simple getter. The key thing is that the call might be maintained in the future and changed by someone in the future. Any developer if change the inner code of getSomeValue() may not be aware of the invocation method that you are currently using.

how null is checked in java? [duplicate]

This question already has answers here:
Java Object Null Check for method
(8 answers)
Closed 7 years ago.
I was working on some project and got a condition when I have to check the object is null or not from a list and all variables of the object are null.
So can someone explain to me how an object is checked for null i.e. variable wise or some other way.
how an object is checked for null internally in java don't want the code. want the concept
Please in a little detail.
My Question: How Java internally checks if object contains a null value?
Apparently, you are actually asking how null checks are implemented under the hood.
The answer is implementation specific. It could be different for different JVMs and / or execution platforms. (If you want to research the specific implementation on a specific JVM, I suggest you checkout the JVM source code and/or get the JIT compiler to dump out the compiled native code for you to examine.)
Basically, there are two approaches:
An explicit x == null test will typically compile to an instruction sequence that compares the value of x against the value that represents a null. That is usually a 32-bit or 64-bit zero.
The implicit null check in x.toString() could be done the same way. Alternatively, it could be done by simply treating x as a machine address and attempting to fetch a value at that address. Assuming that the zero page has not been mapped, this will trigger a hardware "segmentation fault" exception. Java uses native code mechanisms to trap that exception, and turn it into a NullPointerException.
If you're looking at a single item:
if(object == null)
{
(...)
}
You mentioned a list. Let's pretend it's an ArrayList of Objects:
for(Object o : array_list)
{
if(o == null)
{
(...)
}
}
You'd also want to check to see if your list is null before you start looping through it.
Basically any can be easily checked for null value. Every internal details and implementations of null and comparison with object are totally managed by java so all we need is to have a compare of the object with null as :-
Object obj = null; // Object can be replaced with any class
if(obj == null){
// do your logics
}
As far as any List or Collection is considered, to see if object stored in it are null or not :-
List <String> list = new ArrayList<String>();
list.add("hi");
list.add(null);
for(String s : list){
if(s == null){
// do your logics here
}
}
Java does not check if an object is a "null".
You cannot have a null object as null does not extend the Object class.
What you can do in java is have a variable assigned to null, meaning it references "nothing". (In reality, it is referencing the bytes that null is defined as)
That is what the other answers are doing, they are checking if a reference variable is actually pointing to nothing (null). An object itself, however, is never null.

Passing params in java by reference [duplicate]

This question already has answers here:
Is Java "pass-by-reference" or "pass-by-value"?
(93 answers)
How to return multiple objects from a Java method?
(25 answers)
Closed 9 years ago.
I'm a new comer form C#, and I know clearly that "Java is always pass-by-value."
But pass-by-reference is useful when we want to get multiple outputs from one method.
How can we get multiple outputs from one method in java, as in C#.
I know one way to do this -- use a generic wrapper class, and get value from the field.
class Wrapper<T> {
public Wrapper(T value) {
Value = value;
}
public T Value;
}
Is there another way to realize this effect?
No, Java does not have out parameters. You can pass an object reference that the method is to modify to pretend that it has out parameters, but this isn't usually the best design and runs into other issues (multithreading and mutable state for one).
The best way to achieve a method that returns multiple values is to have the method return a type that contains multiple values.
Another way to simulate call by reference in Java is to pass a one-element array as a parameter.

When sent to a constructor in Java, what does "null" value do? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Which constructor is chosen when passing null?
I recently came across this curiosity while coding a few days back and can't seem to figure out why the following happens:
Given the class below
public class RandomObject{
public RandomObject(Object o){
System.out.println(1);
}
public RandomObject(String[] s){
System.out.println(2);
}
}
When the call new RandomObject(null); is made the output is always 2 regardless of the order in which the constructors were created. Why does null refer to the string array rather than the object?
The key here is that Object is the super type of String[]
Java uses the most specific available method to resolve such cases. Null can be passed to both methods without compilation errors so Java has to find the most specific method here. The version with String[] is more specific - therefore it will be chosen for execution.
Someone else has had this question earlier, check this post
If there are two cases to choose from, the compiler will first try to pick the more specific case. In this case, String will be picked over Object.
In the other question it was String str instead of String[] s
Thus, since String[] is a more specific datatype than its super type Object, it is picked.

Categories

Resources