Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Comparison of C Sharp and Java
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
=== {{anchor|Nullable types}}Lifted (nullable) types === C# allows value/primitive/simple types to be "lifted" to allow the special {{mono|null}} value in addition to the type's native values. A type is lifted by adding a {{code|?}} suffix to the type name; this is equivalent to using the {{code|Nullable<T>}} [[Generic programming|generic]] type, where {{mono|T}} is the type to be lifted. Conversions are implicitly defined to convert between values of the base and the lifted type. The lifted type can be compared against {{mono|null}} or it can be tested for {{mono|HasValue}}. Also, lifted operators are implicitly and automatically defined based on their non-lifted base, where β with the exception of some Boolean operators β a null argument will propagate to the result. Java does not support type lifting as a concept, but all of the built-in primitive types have corresponding wrapper types, which do support the {{mono|null}} value by virtue of being reference types (classes). According to the Java spec, any attempt to dereference the {{mono|null}} reference must result in an exception being thrown at run-time, specifically a {{mono|NullPointerException}}. (It would not make sense to dereference it otherwise, because, by definition, it points to no object in memory.) This also applies when attempting to [[Auto-unboxing|unbox]] a variable of a wrapper type, which evaluates to {{mono|null}}: the program will throw an exception, because there is no object to be unboxed β and thus no boxed value to take part in the subsequent computation. The following example illustrates the different behavior. In C#, the lifted*operator propagates the {{mono|null}} value of the operand; in Java, unboxing the null reference throws an exception. Not all C# lifted operators have been defined to propagate {{mono|null}} unconditionally, if one of the operands is {{mono|null}}. Specifically, the [[Boolean operator (computer programming)|Boolean operator]]s have been lifted to support [[ternary logic]] thus keeping impedance with [[SQL]]. The Java Boolean operators do not support ternary logic, nor is it implemented in the base class library.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)