Difference between null and empty (“”) Java String
You may also understand the difference between null and an empty string this way: Original image by R. Sato (@raysato)
You may also understand the difference between null and an empty string this way: Original image by R. Sato (@raysato)
Setting a pointer to 0 (which is “null” in standard C++, the NULL define from C is somewhat different) avoids crashes on double deletes. Consider the following: Foo* foo = 0; // Sets the pointer to 0 (C++ NULL) delete foo; // Won’t do anything Whereas: Foo* foo = new Foo(); delete foo; // Deletes … Read more
An “undefined variable” is different from the value undefined. An undefined variable: var a; alert(b); // ReferenceError: b is not defined A variable with the value undefined: var a; alert(a); // Alerts “undefined” When a function takes an argument, that argument is always declared even if its value is undefined, and so there won’t be … Read more
In a generic way, you may use an expression tree and check with an extension method: if (!person.IsNull(p => p.contact.address.city)) { //Nothing is null } Full code: public class IsNullVisitor : ExpressionVisitor { public bool IsNull { get; private set; } public object CurrentObject { get; set; } protected override Expression VisitMember(MemberExpression node) { base.VisitMember(node); … Read more
if (myString != null && !myString.isEmpty()) { // doSomething } As further comment, you should be aware of this term in the equals contract: From Object.equals(Object): For any non-null reference value x, x.equals(null) should return false. The way to compare with null is to use x == null and x != null. Moreover, x.field and … Read more
Select * From Table Where (col is null or col=””) Or Select * From Table Where IsNull(col, ”) = ”
You can simply use Apache Commons Lang: result = ObjectUtils.compare(firstComparable, secondComparable)
nil should only be used in place of an id, what we Java and C++ programmers would think of as a pointer to an object. Use NULL for non-object pointers. Look at the declaration of that method: – (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context Context is a void * (ie a C-style pointer), so … Read more
In my experience, tests of the form if (ptr) or if (!ptr) are preferred. They do not depend on the definition of the symbol NULL. They do not expose the opportunity for the accidental assignment. And they are clear and succinct. Edit: As SoapBox points out in a comment, they are compatible with C++ classes … Read more
Karl is absolutely correct, there is no need to set objects to null after use. If an object implements IDisposable, just make sure you call IDisposable.Dispose() when you’re done with that object (wrapped in a try..finally, or, a using() block). But even if you don’t remember to call Dispose(), the finaliser method on the object … Read more