9

I have an interesting question. I understand the following C# code enables users to input null values in parameters:

public DateTime? date (DateTime dt) {}

What is the equivalent when coding in Java?

AWb
  • 161
  • 3
  • 10
  • This method returns a nullable DateTime, but its argument isn't nullable. – Matías Fidemraizer Jan 20 '12 at 10:09
  • 1
    The `c#` example allows a nullable DateTime to be returned. The DateTime being passed as the parameter cannot be null. – Ash Burlaczenko Jan 20 '12 at 10:10
  • possible duplicate of [Creating Nullables types in java](http://stackoverflow.com/questions/1123544/creating-nullables-types-in-java) – Ash Burlaczenko Jan 20 '12 at 10:12
  • So please elaborate why we would ever use the '?' in the code? I know that without it, an error will appear if I were to set dt = null. – AWb Jan 20 '12 at 10:13
  • possible duplicate of [How to present the nullable primitive type int in Java?](http://stackoverflow.com/questions/985151/how-to-present-the-nullable-primitive-type-int-in-java) – Oded Jan 20 '12 at 10:15

4 Answers4

6

If I recall correctly each object in java can be nullable. Primitive types (int, double, float, char etc...) cannot be null. For using null with them you have to use their Object counterpart (Integer, Double, Float...)

Regarding dates, java.util.Date is an Object, so it can be null. Same goes for Calendar & GregorianCalendar.

equivalent code will be something like:

public Date date(Date dt) throws NullPointerException { 
  if (dt == null) throw new NullPointerException();
  ...
}

In C# you can use ? to allow null val in primitive types (e.g. to enforce object null reference errors). I don't understand why this thing bothers you. If you need for example a nullable integer parameter in java, you simply have to use java.lang.Integer and not primitive int type.

BigMike
  • 6,683
  • 1
  • 23
  • 24
  • This question is really about updating my knowledge with C# and Java syntax and to simply understand best practice for setting nullable parameters. Many thanks for your answer. – AWb Jan 20 '12 at 10:24
  • C# and java are somewhat similar but still they are two different languages. There are some things you'll find in Java with no counterpart in C# and viceversa. My suggestion is not to compare syntax specific issues, but comparing them on functionality. Have a nice day. – BigMike Jan 20 '12 at 10:27
  • Actually throwing NullPointerException is a runtime error while the C# syntax causes a compilation error if you try to pass null. Rather different things. – Joachim Isaksson Jan 20 '12 at 10:49
  • Sure, and honestly I prefer the Compile time check of C# against the runtime check of Java. However using the throw clause will make the compiler issue an error if you "forget" the try/catch (better than nothing IMO). On this side I prefer the java way to enforce catch block somewhere in your code. In C# you're not forced to catch (or am I recalling wrong?) – BigMike Jan 20 '12 at 11:23
1

Since a Date in Java is a class, a reference to it can already be null. In other words;

public Date date(Date dt) { }

...is Java's version of the same.

Note that the parameter also can be null in Java, which it can't in the C# version.

Joachim Isaksson
  • 176,943
  • 25
  • 281
  • 294
  • nope, in order to be equal you have to check the dt parameter and throw a NullPointerException if the given value is null (of course I'm joking :P) – BigMike Jan 20 '12 at 10:15
0

How to present the nullable primitive type int in Java? says "Java does not have a nullable feature as C# has." Is that an answer?

Community
  • 1
  • 1
Mr Lister
  • 45,515
  • 15
  • 108
  • 150
0

Java doesn't support user-defined value types (e.g. types that can not be null). In this regard, a Java DateTime object already supports nullable by virtue of allowing null to be assigned.

For other types, such as int and double, you can achieve the same effect by using the boxed versions Integer and Double which are allowed to be assigned null.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103