i was trying to delete the minimum element in BST. there can be 2 cases:  Case I: right child not present In that case, set current node to null straight away.  node=delete(node);   Case II: right child present. In that case copy the contents of right child to current node and delete the right child.  if(node.right!=null)         {             node.data=node.right.data;             node.right=null;         }   My doubt is, why does the second case work and the first doesnt. I mean, if "node" is a reference variable and so nulling it doesnt make it null in actual tree, then WHY does node.right works just fine? My full code of the function:  public static int del_min(Node node) {     if(node.left==null)     {         minimum=node.data;         if(node.right!=null) //WORKING         {             node.data=node.right.data;             node.right=null;         }         else //NOT WORKING         {             node=null;         }     }     else     {         minimum=del_min(node.left);     }     return minimum; }   Please help me.
121
I always thought Java uses pass-by-reference. However, I've seen a couple of blog posts (for example, ) that claim that it isn't (the blog post says that Java uses pass-by-value). I don't think I understand the distinction they're making. What is the explanation?
40567
I'm working on a project where i find i'm checking for the following in many, many places:  if(item.Rate == 0 || item.Rate == null) { }   more as a curiousity than anything, what's the best way to check for both cases?  I've added a helper method which is:  public static bool nz(object obj) {     var parsedInt = 0;     var parsed = int.TryParse(obj.ToString(), out parsedInt);     return IsNull(obj) || (parsed &amp;&amp; parsedInt == 0); }   Is there a better way?
88330
I've seen some people using void operator in their code. I have also seen this in href attributes: javascript:void(0) which doesn't seem any better than javascript:;  So, what is the justification of using the void operator?
101830
There is a Java Void -- uppercase V-- . The only situation I have ever seen it used is to parameterize Callables    final Callable&lt;Void&gt; callable = new Callable&lt;Void&gt;() {             public Void call() {                 foobar();                 return null;             }         };   Are there any other uses for the Java Void reference type? Can it ever be assigned anything other than null? If yes, do you have examples?
21565
I have several objects set as children of a parent object. Whenever I remove the parent object (an empty in this case), the position of all of the children are skewed. I'm guessing that this is do to them being moved after the parent relationship was made? I'm looking for a way for all of the child objects to maintain their current positions after the parent has been removed. Is there a way to do this?
16225
Why does it (apparently) make a difference whether I pass null as an argument directly, or pass an Object that I assigned the value null?  Object testVal = null; test.foo(testVal);    // dispatched to foo(Object) // test.foo(null);    // compilation problem -&gt; "The method foo(String) is ambiguous"     public void foo(String arg) { // More-specific     System.out.println("foo(String)"); }  public void foo(Object arg) { // Generic     System.out.println("foo(Object)"); }   In other words, why is the (commented-out) second call to foo(...) not dispatched to foo(Object)?  Update: I use Java 1.6. I could compile Hemal's code without problems, but mine still doesn't compile. The only difference I see is that Hemal's methods are static while mine are not. But I really don't see why this should make a difference...?  Update 2: Solved. I had another method foo(Runnable) in my class, so the dispatcher couldn't unambiguously select the single most specific method. (See my comment in Hemal's second answer.) Thanks everyone for your help.
31631
I think we all understand the necessity of delete when reassigning a dynamically-allocated pointer in order to prevent memory leaks. However, I'm curious, to what extent does the C++ mandate the usage of delete? For example, take the following program  int main() {      int* arr = new int[5];      return 0; }   While for all intents and purposes no leak occurs here (since your program is ending and the OS will clean up all memory once it returns), but does the standard still require -- or recommend -- the usage of delete[] in this case? If not, would there be any other reason why you would delete[] here?
24404
Is there any way to determine (programatically, of course) if a given pointer is "valid"? Checking for NULL is easy, but what about things like 0x00001234? When trying to dereference this kind of pointer an exception/crash occurs.  A cross-platform method is preferred, but platform-specific (for Windows and Linux) is also ok.  Update for clarification: The problem is not with stale/freed/uninitialized pointers; instead, I'm implementing an API that takes pointers from the caller (like a pointer to a string, a file handle, etc.). The caller can send (in purpose or by mistake) an invalid value as the pointer. How do I prevent a crash?
203
I came across this code:  void f(const std::string &amp;s);   And then a call:  f( *((std::string*)NULL) );   And I was wondering what others think of this construction, it is used to signal that function f() should use some default value (which it computes) instead of some user provided value.  I am not sure what to think of it, it looks weird but what do you think of this construction?
55175
I'm implementing compareTo() method for a simple class such as this (to be able to use Collections.sort() and other goodies offered by the Java platform):  public class Metadata implements Comparable&lt;Metadata&gt; {     private String name;     private String value;  // Imagine basic constructor and accessors here // Irrelevant parts omitted }   I want the natural ordering for these objects to be: 1) sorted by name and 2) sorted by value if name is the same; both comparisons should be case-insensitive. For both fields null values are perfectly acceptable, so compareTo must not break in these cases.   The solution that springs to mind is along the lines of the following (I'm using "guard clauses" here while others might prefer a single return point, but that's beside the point):  // primarily by name, secondarily by value; null-safe; case-insensitive public int compareTo(Metadata other) {     if (this.name == null &amp;&amp; other.name != null){         return -1;     }     else if (this.name != null &amp;&amp; other.name == null){         return 1;     }     else if (this.name != null &amp;&amp; other.name != null) {         int result = this.name.compareToIgnoreCase(other.name);         if (result != 0){             return result;         }     }      if (this.value == null) {         return other.value == null ? 0 : -1;     }     if (other.value == null){         return 1;     }      return this.value.compareToIgnoreCase(other.value); }   This does the job, but I'm not perfectly happy with this code. Admittedly it isn't very complex, but is quite verbose and tedious.  The question is, how would you make this less verbose (while retaining the functionality)? Feel free to refer to Java standard libraries or Apache Commons if they help. Would the only option to make this (a little) simpler be to implement my own "NullSafeStringComparator", and apply it for comparing both fields?  Edits 1-3: Eddie's right; fixed the "both names are null" case above  About the accepted answer  I asked this question back in 2009, on Java 1.6 of course, and at the time  was my preferred accepted answer. I never got round to changing that until now (2017).  There are also —a 2009 Apache Commons Collections one and a 2013 Guava one, both posted by me—that I did prefer at some point in time.  I now made the clean  the accepted answer. That should definitely be preferred if on Java 8, and these days Java 8 should be available to nearly all projects.
77872
Say I have the following C++:  char *p = new char[cb]; SOME_STRUCT *pSS = (SOME_STRUCT *) p; delete pSS;   Is this safe according to the C++ standard? Do I need to cast back to a char* and then use delete[]? I know it'll work in most C++ compilers, because it's plain-ordinary-data, with no destructors. Is it guaranteed to be safe?
70260
I have a bunch of objects in a flat structure. These objects have an ID and a ParentID property so they can be arranged in trees. They are in no particular order. Each ParentID property does not necessarily matches with an ID in the structure. Therefore their could be several trees emerging from these objects.  How would you process these objects to create the resulting trees ?   I'm not so far from a solution but I'm sure it is far from optimal...  I need to create these trees to then insert Data into a database, in proper order.  There are no circular references. A Node is a RootNode when ParentID == null or when ParentID can't be found in the other objects
41167
I am told that good developers can spot/utilize the difference between Null and False and 0 and all the other good "nothing" entities. What is the difference, specifically in PHP?  Does it have something to do with ===?
12375
I have a simple setter method for a property and null is not appropriate for this particular property. I have always been torn in this situation: should I throw an , or a ? From the javadocs, both seem appropriate. Is there some kind of an understood standard?  Or is this just one of those things that you should do whatever you prefer and both are really correct?
38112
Here's the scenario...    if (entry.Properties["something"].Value != null)   attribs.something = entry.Properties["something"].Value.ToString();   While effective and working correctly, this looks ugly to me.  If I don't check for a null before performing the ToString() then it throws an exception if the property was null.  Is there a better way to handle this scenario?  Much appreciated!
62552
From a given node tree, what is the most reliable way to get the end node being used for output, if any?  My current approach is to check for the correct output type for the tree (OUTPUT_MATERIAL, OUTPUT_WORLD...) and for is_active_output:  nodes = node_tree.nodes  for n in nodes:     if n.type == 'OUTPUT_WORLD' and n.is_active_output:         return n  return None   However, I see no guarantee that there is only one node with is_active_output (I can set this to True in multiple nodes).  The ShaderNodeTree has an active_input field, but the value is -1.
40162
Please demonstrate how the ternary operator works with a regular if/else block. Example:  Boolean isValueBig = value &gt; 100 ? true : false;     Exact Duplicate: