So I use C# and I often see many devs use null.
What and which kind of situation do you use this variable?
I am reading c# guide on programming book and I am on Clearing memory now and I haven't encountered null yet. Should I be worried?
I’m asking because my friend, who’s seen Murder Drones several times, apparently though Null was just some randomly generated word, it isn’t!
Does anybody in this sub actually know what Null means?? I’d assume so, but… y’know…
Hello all again,
I have another stupid question here lol, so I'm trying to wrap my head around NULL. Im currently under the impression that NULL is a built in constant that has a value of zero, but what does that actually mean? When would it be appropriate to use null? If someone could explain it in layman's terms that would be super helpful!
I encourage the team to consider rethinking the slogan 'null is nothing, nuls is everything.' The very first words of the business slogan shouldn't be a negative idea unrelated to the product. OK this is boring, but 'custom blockchain for business solutions' at least has a positive message.
There's this post that has many comments with null as their username . . .
Also, it says it was posted 46 years ago . . .
How and why is this?
Edit: for some strange reason, none of the comments say null anymore, coincidentally, but they did, and now I feel stupid . . .
It appears the concept of nulls came from Tony Hoare back in 1965 when he was working on Algol W. He called it his "billion dollar mistake". I was wondering if James Gosling has ever expressed any thoughts about wether or not adding nulls to Java was a good or bad thing?
Personally, coming to Java from Scala and Haskell, nulls seem like a very bad idea, to me.
I am considering making an argument to my company's engineering team to switch from using nulls to using `Optional` instead. I am already quite aware of the type system, code quality, and coding speed arguments. But I am very open to hearing any arguments for or against.
String str = null;
means a String reference, named str, not pointing to anything
String str = "";
means a String reference, named str, pointing to an actual String instance. And for that String instance, it is a zero-length String, but it is still an actual object.
Just a little update with some diagram which hopefully can help you visualize that:
assume I have
String nullStr = null;
String emptyStr = "";
String myStr = "ab";
What it conceptually is something look like:
// String nullStr = null;
nullStr ----------> X pointing to nothing
// String emptyStr = "";
+------------------+
emptyStr ---------> | String |
+------------------+
| length = 0 |
| content = [] |
+------------------+
// String myStr = "ab";
+------------------+
myStr ------------> | String |
+------------------+
| length = 2 |
| content = [ab] |
+------------------+
(of course the internal structure of the String object is not the real thing in Java, it is just for giving you an idea)
More edit for the rationale behind NULL:
In fact in some language they do not provide concept of NULL. Anyway, in Java (or similar language), Null means semantically different from "empty" object. Use String as an example, I may have a People class with a String preferedTitle attribute. A Null preferedTitle means there is NO preferred title for that people (so that we need to derive and show the title for it, maybe), while a preferedTitle being an empty string means there IS a preferred title, and that's showing nothing.
Btw, although a bit off topic: concept of Null is seen as problematic for some people (because all those extra handling it need etc). Hence some languages (e.g. Haskell) are using some other ways to handle the situation where we used to use Null.
String str is a reference to an object. That is, it's not an actual object, but a variable which can contain the address of an object. When you assign a value to str you are changing the address stored within and changing which object it addresses.
null is reference value which points to no object. It's about as close to nothing as you can get. If you assign null to a String reference (String str = null;), you cannot then invoke any method of String using that reference -- all attempts will result in NullPointerException.
"" is a character String which contains no characters -- zero length. It is still an object, though, and if you assign its address to your String reference variable (String str = "";) you can then take its length, compare it to another String, extract its hashCode, etc.
What do null in sql store
i was watching a music video and then my mouse slipped on the options menu it should say options but it says null its creeping me out can someone tell me why? (i use brave browser)
I'm relatively new in programming and these two data types are somehow interchangeable at least in my current understanding. Whenever I try to access a certain variable (that I haven't explicitly set a value), it returns either null or undefined and I still haven't made an intuition of their clear difference.
Edit: programming language is JavaScript
Look, I get it: Null pointer exceptions are annoying but so is having to constantly check every object . against null. Is this the new religion after OOP and FP are being phased out? Null just means that the object is not initialized. It's useful to indicate such a state. If you just force yourself to initialize every object to some empty value then you're going to end up passing around empty strings - but wait, an empty string might intentionally be empty or it might indicate "please fill me".
Because I can already tell you what massive insights will come out soon: That null is actually not a mistake at all, it's useful. And the most ironic part about all this? These same people who hate on null are loving nullable types. Litearlly just types that *can be null but you just have to check every time before you use them*. So there is literally no difference except it forces you on a compiler level to check for null every time you use it. When I realized this I thought I was in dreamland, how can these people worship this meaningless little feature so much, crazy. It's literally the same as having null in the language except with a tiny tiny "advantage" that the compiler forces you to check. Which 99.9% of the time, you dont want to be forced because maybe you know implicitly and you dont care [right now].
I am a learning java, btw why is used the return null;?
http://paste.ofcode.org/RGYLSzmFLzHVPjthJ4Ndr9
And when should I be using 'null' In java. I can't find a lot of information on it that isn't just dissuading me to use it.
Here it is just so that the variable "out" is set to something instead of being "uninitialized". Usually used in the finally block then you want to close it. and you cant access variables from inside the try block.
The variable is simply initialized to null. If it wasn't included it would still be initialized to null so this isn't needed. Turns out this isn't true. Thanks u/zifyoip.
When learning I was told to initialize everything because some languages don't have consistent initializations so this is mostly how I write code.
My understanding is that NULL represents true absence of value or a total unknown value. This is not the same as empty which is a known value, or a string of zero length. I've worked with banking data and often see lots of NULL values in various fields but if NULL represents UNKNOWN does that mean something simply went wrong/error in the system or is it a legitimate value? Because otherwise I'd think putting empty there makes more sense.
Not really sure how to treat NULL values in these datasets, should I simply ignore them? What if I'm trying to transform the data (or preform joins) on these rows wouldn't NULL values throw all the calculations off?
How should I think about and handle NULL values as they come into my codebase?
Thanks
See it under commercial callback dates etc. Tried looking it up but still confused
I am a software engineer but my boss isn't. I understand the difference between a null value and, say, an empty string but myself and the other programmers are unable to explain that in a way someone with business experience can understand. Please help me explain like I'm five what Null is and how it differs from the empty string.
EDIT: I love the analogy. This is a good explanation. Answer
We all know about Tony Hoare and his admitted "Billion Dollar Mistake":
Tony Hoare introduced Null references in ALGOL W back in 1965 "simply because it was so easy to implement", says Mr. Hoare. He talks about that decision considering it "my billion-dollar mistake".
But i'm not here looking at it not just null pointer exceptions,
but how they really can infect a language,
and make the right thing almost impossible to do things correctly the first time.
Leading to more lost time, and money: contributing to the ongoing Billion Dollar Mistake.
It Started With a Warning
I've been handed some 18 year old Java code. And after not having had used Java in 19 years myself, and bringing it into a modern IDE, i ask the IDE for as many:
-
hints
-
warnings
-
linter checks
as i can find. And i found a simple one:
Comparing Strings using == or !=
Checks for usages of == or != operator for comparing Strings. String comparisons should generally be done using the equals() method.
Where the code was basically:
firstName == ""
and the hint (and auto-fix magic) was suggesting it be:
firstName.equals("")or alternatively, to avoid accidental assignment):
"".equals(firstName)
In C# that would be a strange request
Now, coming from C# (and other languages) that know how to check string content for equality:
-
when you use the equality operator (
==) -
the compiler will translate that to
Object.Equals
And it all works like you, a human, would expect:
string firstName = getFirstName();
-
firstName == "": False -
"" == firstName: False -
"".Equals(firstName): False
And a lot of people in C#, and Java, will insist that you must never use:
firstName == ""
and always convert it to:
firstName.Equals("")or possibly:
firstName.Length == 0
Tony Hoare has entered the chat
Except the problem with blindly converting:
firstName == ""
into
firstName.Equals("")is that you've just introduced a NullPointerException.
If firstName happens to be null:
-
firstName == "": False -
"" == firstName: False -
"".Equals(firstName): False -
firstName.Length == 0: Object reference not set to an instance of an object. -
firstName.Equals(""): Object reference not set to an instance of an object.
So, in C# at least, you are better off using the equality operator (==) for comparing Strings:
-
it does what you want
-
it doesn't suffer from possible NullPointerExceptions
And trying to 2nd guess the language just causes grief.
But the null really is a time-bomb in everyone's code. And you can approach it with the best intentions, but still get caught up in these subtleties.
Back in Java
So when i saw a hint in the IDE saying:
-
convert
firstName == "" -
to
firstName.equals("")
i was kinda concerned, "What happens if firstName is null? Does the compiler insert special detection of that case?"
No, no it doesn't.
In fact Java it doesn't insert special null-handling code (unlike C#) in the case of:
firstName == ""
This means that in Java its just hard to write safe code that does:
firstName == ""
But because of the null landmine, it's very hard to compare two strings successfully.
(Not even including the fact that Java's equality operator always checks for reference equality - not actual string equality.)
I'm sure Java has a helper function somewhere:
StringHelper.equals(firstName, "")
But this isn't about that.
This isn't C# vs Java
It just really hit me today how hard it is to write correct code when null is allowed to exist in the language. You'll find 5 different variations of string comparison on Stackoverflow. And unless you happen to pick the right one it's going to crash on you.
Leading to more lost time, and money: contributing to the ongoing Billion Dollar Mistake.
Just wanted to say that out loud to someone - my wire really doesn't care :)
Addendum
It's interesting to me that (almost) nobody has caught that all the methods i posted above to compare strings are wrong. I intentionally left out the 1 correct way, to help prove a point.
Spelunking through this old code, i can see the evolution of learning all the gotchas.
-
Some of them are (in hindsight) poor decisions on the language designers. But i'm going to give them a pass, it was the early to mid 1990s. We learned a lot in the subsequent 5 years
-
and some of them are gotchas because
nullis allowed to exist
Real Example Code 1
if (request.getAttribute("billionDollarMistake") == "") { ... }It's a gotcha because it's checking reference equality verses two strings being the same. Language design helping to cause bugs.
Real Example Code 2
The developer learned that the equality operator (==) checks for reference equality rather than equality. In the Java language you're supposed to call .equals if you want to check if two things are equal. No problem:
if (request.getAttribute("billionDollarMistake").equals("") { ... }Except its a gotcha because the value billionDollarMistake might not be in the request. We're expecting it to be there, and barreling ahead with a NullPointerException.
Real Example Code 3
So we do the C-style, hack-our-way-around-poor-language-design, and adopt a code convention that prevents a NPE when comparing to the empty string
if ("".equals(request.getAttribute("billionDollarMistake")) { ... }Real Example Code 4
But that wasn't the only way i saw it fixed:
if ((request.getAttribute("billionDollarMistake") == null) || (request.getAttribute("billionDollarMistake").equals("")) { ... }Now we're quite clear about how we expect the world to work:
"" is considered empty null is considered empty therefore null == ""
It's what we expect, because we don't care about null. We don't want null.
Like in Python, passing a special "nothing" value (i.e. "None") to a compare operation returns what you expect:
a
nulltakes on it's "default value" when it's asked to be compared
In other words:
-
Boolean:
None == falsetrue -
Number:
None == 0true -
String:
None == ""true
Your values can be null, but they're still not-null - in the sense that you can get still a value out of them.