1

I am just checking regex for some expressions and in the method, I am reusing the variable again and again

Pattern pattern = Pattern.compile("([^test])");
        Matcher matcher = pattern.matcher("SS");
        if (matcher.find()) {
            return false;
        }
        //Making it 'null' before re-use
        pattern = null;
        matcher = null;
        pattern = Pattern.compile("([newtest]){4,}");
        matcher = pattern.matcher("test");
        if (matcher.find()) {
            return false;
        }
        pattern = null;
        matcher = null;
        pattern = Pattern.compile("[stack]{2,}");
        matcher = pattern.matcher("overflow");
        if (matcher.find()) {
            return false;
        }

Is it good to makethe variable to null before setting some other new value to that ?

Does it have a significance ?

Note : It may be sign of non-standard, using the same variable again, but i just want to know whether the nullifying the variable is good to proceed before re-assignment?

6 Answers 6

8

Is it good to makethe variable to null before setting some other new value to that ?

No - it's pointless, and makes the code harder to read.

It seems to me that you should work out some way of looping instead though anyway - currently you've got the same kind of code again and again, which is a bad idea. All that's changing is the data - so parameterize that. I assume in your real code you wouldn't be hard-coding both the pattern and the input data to start with, but you could easily have a list of patterns to test, for example.

I personally find that if I'm reusing a variable for a different purpose within a method (as opposed to, say, modifying it based on its current value), that suggests that I should refactor anyway.

Sign up to request clarification or add additional context in comments.

Comments

0

It will make no difference, because when you assign an object to a different reference, the old object will lose its reference and will be eligible for the garbage collection. Assigning to the null will also make the reference to lose the link to current object.

Comments

0

No, it doesn't have any significance.

When a new object is assigned to a non-null variable, existing object will lose its reference and it will be eligible for Garbage Collection. Same thing will happen when you set the variable as null.

Comments

0

It makes no difference.

If you assign null to a variable and assign a value to it right after, you are only adding one more line on your code. By the time you assign another value, instead of null, the old value become eligible for garbage collection.

Comments

0

Sometimes it prevents memory leakage well described in Effective Java item "Obsolete Reference" and make unused objects ready for garbage collection.

Comments

0

I performed some tests, and it seems that setting the variable to null does help the garbage collection process. Apparently, the JVM is more responsive to a variable being set to null than it is to the variable being assigned to a new object, leaving the previous object hanging. My code which demonstrated this can be found here:

http://www.evernote.com/shard/s4/sh/1a067433-722d-4d2c-9487-0ac661261c20/45f571f144a8af40d62d2e48ea1670e9

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.