44

In C# I can a variable to allow nulls with the question mark. I want to have a true/false/null result. I want to have it set to null by default. The boolean will be set to true/false by a test result, but sometimes the test is not run and a boolean is default to false in java, so 3rd option to test against would be nice.

c# example:

bool? bPassed = null;

Does java have anything similar to this?

cdonner
  • 37,019
  • 22
  • 105
  • 153
Green
  • 1,405
  • 4
  • 21
  • 27

8 Answers8

47

No.

Instead, you can use the boxed Boolean class (which is an ordinary class rather a primitive type), or a three-valued enum.

SLaks
  • 868,454
  • 176
  • 1,908
  • 1,964
  • 2
    In other words "Yes", since the corresponding class acts like the C# equivalent (except the C# version is a struct, and the Java one is a class). – ToolmakerSteve Sep 26 '15 at 15:55
  • 5
    @ToolmakerSteve: No; they aren't the same, in a number of ways. – SLaks Sep 27 '15 at 00:40
  • 1
    @SLaks "they aren't the same, in a number of ways" can you provide a few examples and/or a relevant link? Thanks. – Stéphane Gourichon Jun 14 '16 at 16:05
  • @StéphaneGourichon: https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html – SLaks Jun 14 '16 at 16:12
  • 1
    @SLaks thanks but that does not even mention C#. Given ToolmakerSteve's comment that you refuted I expected something along the lines of [Do autoboxing and unboxing behave differently in Java and C# - Stack Overflow](https://stackoverflow.com/questions/1597154/do-autoboxing-and-unboxing-behave-differently-in-java-and-c-sharp). – Stéphane Gourichon Jun 14 '16 at 16:44
  • @StéphaneGourichon: But the question is asking about nullable types, which have nothing to do with boxing. These are two completely different features that happen to have some similar effects. – SLaks Jun 14 '16 at 16:51
  • @ToolmakerSteve My understanding is that nullable types are a behavior wished by programmers, and boxing is an implementation providing it in practice. The difference between Java and C# is that Java makes difference between primitive types and equivalent classes (boxed types) salient so programmers can use boxed type when nullable behavior is needed, while C# (having chosen to handle primitive types and classes equally at source level) provides syntactic sugar to present explicit nullable types that looks more or less like regular types (e.g. transparent assignment). – Stéphane Gourichon Jun 14 '16 at 19:22
  • Java also wears a garbage collection overhead for boxed values since it creates an object instance on the heap per boxed value. C# will create a stack only structure (avoids heap) if the corresponding non-nullable type is a struct type which is the case for all primitive types (or struct types you declare e.g. Point(x,y)). For arrays of nullable values you get additional performance improvements as many array operations have corresponding CPU vector ops that don't transfer to arrays of heap references. – Tony O'Hagan Sep 20 '17 at 23:53
  • To clarify ... C# arrays heap allocated but if they contain nullable struct values then these values are not heap references. – Tony O'Hagan Sep 21 '17 at 00:12
21

you can use :

Boolean b = null;

that is, the java.lang.Boolean object in Java.

And then also set true or false by a simple assignment:

Boolean b = true; or Boolean b = false;

Saket
  • 45,521
  • 12
  • 59
  • 79
11

No, in java primitives cannot have null value, if you want this feature, you might want to use Boolean instead.

amit
  • 175,853
  • 27
  • 231
  • 333
8

Sure you can go with Boolean, but to make it more obvious that your type can have "value" or "no value", it's very easy to make a wrapper class that does more or less what ? types do in C#:

public class Nullable<T> {
    private T value;
    public Nullable() { value = null; }
    public Nullable(T init) { value = init; }
    public void set(T v) { value = v; }
    public boolean hasValue() { return value != null; }
    public T value() { return value; }
    public T valueOrDefault(T defaultValue) { return value == null ? defaultValue : value; }
}

Then you can use it like this:

private Nullable<Integer> myInt = new Nullable<>();
...
myInt.set(5);
...
if (myInt.hasValue()) 
   ....
int foo = myInt.valueOrDefault(10);

Note that something like this is standard since Java8: the Optional class. https://docs.oracle.com/javase/8/docs/api/java/util/Optional.html

geert3
  • 7,086
  • 1
  • 33
  • 49
7

Yes you can.

To do this sort of thing, java has a wrapper class for every primitive type. If you make your variable an instance of the wrapper class, it can be assigned null just like any normal variable.

Instead of:

boolean myval;

... you can use:

Boolean myval = null;

You can assign it like this:

myval = new Boolean(true);

... And get its primitive value out like this:

if (myval.booleanValue() == false) {
  // ...
}

Every primitive type (int, boolean, float, ...) has a corresponding wrapper type (Integer, Boolean, Float, ...).

Java's autoboxing feature allows the compiler to sometimes automatically coerce the wrapper type into its primitive value and vice versa. But, you can always do it manually if the compiler can't figure it out.

Joseph
  • 1,172
  • 1
  • 10
  • 12
5

In Java, primitive types can't be null. However, you could use Boolean and friends.

NPE
  • 486,780
  • 108
  • 951
  • 1,012
2

No but you may use Boolean class instead of primitive boolean type to put null

Yasin Okumuş
  • 2,299
  • 7
  • 31
  • 62
2

If you are using object, it allows null

If you are using Primitive Data Types, it does not allow null

That the reason Java has Wrapper Class

kazinix
  • 28,987
  • 33
  • 107
  • 157
BachT
  • 1,058
  • 9
  • 17