Firstly, you never set an object to null. That concept has no meaning. You can assign a value of null to a variable, but you need to distinguish between the concepts of "variable" and "object" very carefully. Once you do, your question will sort of answer itself :)
Now in terms of "shallow copy" vs "deep copy" - it's probably worth avoiding the term "shallow copy" here, as usually a shallow copy involves creating a new object, but just copying the fields of an existing object directly. A deep copy would take a copy of the objects referred to by those fields as well (for reference type fields). A simple assignment like this:
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = list1;
... doesn't do either a shallow copy or a deep copy in that sense. It just copies the reference. After the code above, list1 and list2 are independent variables - they just happen to have the same values (references) at the moment. We could change the value of one of them, and it wouldn't affect the other:
list1 = null;
System.out.println(list2.size()); // Just prints 0
Now if instead of changing the variables, we make a change to the object that the variables' values refer to, that change will be visible via the other variable too:
list2.add("Foo");
System.out.println(list1.get(0)); // Prints Foo
So back to your original question - you never store actual objects in a map, list, array etc. You only ever store references. An object can only be garbage collected when there are no ways of "live" code reaching that object any more. So in this case:
List<String> list = new ArrayList<String>();
Map<String, List<String>> map = new HashMap<String, List<String>>();
map.put("Foo", list);
list = null;
... the ArrayList object still can't be garbage collected, because the Map has an entry which refers to it.
Firstly, you never set an object to null. That concept has no meaning. You can assign a value of null to a variable, but you need to distinguish between the concepts of "variable" and "object" very carefully. Once you do, your question will sort of answer itself :)
Now in terms of "shallow copy" vs "deep copy" - it's probably worth avoiding the term "shallow copy" here, as usually a shallow copy involves creating a new object, but just copying the fields of an existing object directly. A deep copy would take a copy of the objects referred to by those fields as well (for reference type fields). A simple assignment like this:
ArrayList<String> list1 = new ArrayList<String>();
ArrayList<String> list2 = list1;
... doesn't do either a shallow copy or a deep copy in that sense. It just copies the reference. After the code above, list1 and list2 are independent variables - they just happen to have the same values (references) at the moment. We could change the value of one of them, and it wouldn't affect the other:
list1 = null;
System.out.println(list2.size()); // Just prints 0
Now if instead of changing the variables, we make a change to the object that the variables' values refer to, that change will be visible via the other variable too:
list2.add("Foo");
System.out.println(list1.get(0)); // Prints Foo
So back to your original question - you never store actual objects in a map, list, array etc. You only ever store references. An object can only be garbage collected when there are no ways of "live" code reaching that object any more. So in this case:
List<String> list = new ArrayList<String>();
Map<String, List<String>> map = new HashMap<String, List<String>>();
map.put("Foo", list);
list = null;
... the ArrayList object still can't be garbage collected, because the Map has an entry which refers to it.
To clear the variable
According to my knowledge,
If you are going to reuse the variable, then use
Object.clear();
If you are not going to reuse, then define
Object=null;
Note: Compare to removeAll(), clear() is faster.
Please correct me, If I am wrong....
How to assign a "null" value to a single element in an int[] array in Java - Stack Overflow
Java, What is the difference between assigning null to object and just declaration - Stack Overflow
java - Assign an object to null - Stack Overflow
Why do you set your variables to null (JAVA)
Primitive Java integers cannot be null, but the Integer class, which wraps a primitive int can be null. Here is one option to consider:
Integer[] num = new Integer[3];
num[0] = 23;
num[1] = null;
num[2] = 12;
The boxing rules in Java make it fairly easy to use int and Integer interchangeably, most of the time.
In int array all the elements enitialize to 0 whether you have assigned a value or not. its because array is primitive type.
public class HelloWorld{
public static void main(String []args){
System.out.println("Hello World");
int [] a = new int[10];
for(int i=0; i<10; i++)
System.out.println(a[i]+"");
}
}
when you are initialize a Person object, Person is not a primitive type. so person can be null but age cannot be null since age is primitive type
It depends on the scope where you declare the variable. For instance, local variables don't have default values in which case you will have to assign null manually, where as in case of instance variables assigning null is redundant since instance variables get default values.
public class Test {
Object propertyObj1;
Object propertyObj2 = null; // assigning null is redundant here as instance vars get default values
public void method() {
Object localVariableObj1;
localVariableObj1.getClass(); // illegal, a compiler error comes up as local vars don't get default values
Object localVariableObj2 = null;
localVariableObj2.getClass(); // no compiler error as localVariableObj2 has been set to null
propertyObj1.getClass(); // no compiler error
propertyObj2.getClass(); // no compiler error
}
}
As mentioned, object reference as instance variable need not be assigned null as those take null as default value. But local variables must be initialized otherwise you will get compilation error saying The local variable s may not have been initialized.
For more details you can refer this link
You can never assign to an object. All you ever have are primitives and references. A reference is either null or a pointer to an object of suitable class.
Java arguments are passed by value. Your called method got a copy of a reference. It made that reference null. The calling method has its own reference which was unaffected by any assignments to the passed copy. That reference still points to the object.
Arguments to methods in Java are 'pass-by-value', which means you are passing a copy of the object reference into the method. Assigning this reference a value of null will change its value within the method call, but does nothing to the reference outside the method, since its a copy. Illustrated with code:
void doSomething(final String input) {
input = null;
System.out.println("Input is: " + input); // prints null
return;
}
final String name = "Bob";
doSomething(name);
System.out.println("Name is: " + name); // prints 'Bob'
Why do you set variables to null? instead just blank?
String x;
VS.
String x = null;
No, because a is a reference (not an object as in this question's title) and no method can modify the value of a reference except the method in which it is defined (I assume from the code context that a is a local variable).
Since Java doesn't have pass-by-reference, what you ask cannot be done: there's no way to collect addresses-of references in order to manage the addresses pointed to. You might use a wrapper object, but not sure what'd be the point.
As everyone else has said, this simply isn't possible. If it's cleaning up resources you're after, then you might consider using a pattern such as:
class A {
private boolean cleanedUp;
public void cleanUp() {
// clean up any resources
cleanedUp = true;
}
public boolean isCleanedUp() {
return cleanedUp;
}
}
And then using it like so:
A a = new A();
a.cleanUp();
if (a.isCleanedUp()) {
...
}
A better solution might be to implement the java.io.Closeable or java.lang.AutoCloseable interfaces depending on your circumstance:
class B implements AutoCloseable {
private boolean closed;
public boolean isClosed() {
return closed;
}
@Override public void close() throws Exception {
// clean up any resources
closed = true;
}
}
In which case you can use a try-with-resources statement:
try (B b = new B()) {
// do stuff
} catch (Exception ex) {
// oh crap...
}
Or you could even combine the two and do it that way, whichever you prefer.
Or lastly you could do it the way William Morrison explained (though I'd probably cheat and just use java.util.concurrent.atomic.AtomicReference instead of making my own class, and it comes with the added benefit of being a generified type), which, depending on your circumstance, may really be unnecessary. After all, you could always just do (even though it might seem a little odd):
A a = new A();
a.doStuffAndDisappear();
a = null;
if(a == null){
//...
}
Try to set JSONObject.NULL instead of null:
A sentinel value used to explicitly define a name with no value. Unlike null, names with this value:
- show up in the names() array
- show up in the keys() iterator
- return true for has(String)
- do not throw on get(String)
- are included in the encoded JSON string.
This value violates the general contract of equals(Object) by returning true when compared to null. Its toString() method returns "null".
For me with net.sf.json.JSONObject I need to create a null JSON by
new JSONObject(true)
This is how I get class into groovy:
groovy.grape.Grape.grab(group: "org.kohsuke.stapler", module: "json-lib", version: "2.4-jenkins-2")