Shree
Shree

Reputation: 9

Memory allocation to string variable

Please confirm which of the two codes are efficient a or b . Where string is defines inside loop or outside loop. Kindly sugggest.

a)

for(int i=0;i<configIndexNumber.size();i++){
                     ConfigurationDetailsObject oldIndex =(ConfigurationDetailsObject)configIndexNumber.get(i);
                     String Key = oldIndex.getVehConfigNo()+oldIndex.getBaseEngineKey()+oldIndex.getEngineCode()+oldIndex.getTestGroupId()+oldIndex.getTransConfig()+oldIndex.getInertiaWeightClassNo()+oldIndex.getAxleRatioValue();
                        if(!configIdxNnb.containsKey(Key))
                        {
                                newConfigIndexList.add(oldIndex);
                                configIdxNnb.put(Key,oldIndex);
                        }

                 }

b)

String Key=null;

 for(int i=0;i<configIndexNumber.size();i++){
                     ConfigurationDetailsObject oldIndex =(ConfigurationDetailsObject)configIndexNumber.get(i);
                     Key = oldIndex.getVehConfigNo()+oldIndex.getBaseEngineKey()+oldIndex.getEngineCode()+oldIndex.getTestGroupId()+oldIndex.getTransConfig()+oldIndex.getInertiaWeightClassNo()+oldIndex.getAxleRatioValue();
                        if(!configIdxNnb.containsKey(Key))
                        {
                                newConfigIndexList.add(oldIndex);
                                configIdxNnb.put(Key,oldIndex);
                        }

                 }

Upvotes: 1

Views: 53

Answers (5)

TheLostMind
TheLostMind

Reputation: 36304

There is very little/ negligible difference b/w the execution times of the 2... The compiler optimizes your code...

public class Test
{
    public static void main(String[] args) {

        long l = System.nanoTime();
        func1("hi");
        System.out.println(System.nanoTime()-l);
        long l1 = System.nanoTime();
        func2("hi");
        System.out.println(System.nanoTime()-l1);
    }

    public static void func1(String str)
    {
        for(int i=0;i<10000;i++)
        {
            String s= str ;
        //  System.out.println(s);
        }
    }

    public static void func2(String str)
    {String s= null;
        for(int i=0;i<10000;i++)
        {
             s= str ;
        //  System.out.println(s);
        }
    }
}

O/p : 
1. 277530
   265557

2. 267694
  243747 

Upvotes: 0

Suresh Atta
Suresh Atta

Reputation: 121998

There will be a minor difference in memory aspect. JVM is enough intelligent to manage.

But you should declare a variable in the smallest scope required . i.e the first case is looking good to me.

I did'nt remember the exact quote from efffective java, It goes something like

The Garbage collector do the best when we use smallest possible scope.

But if you want to access that variable outside the loop the second is the option.

Upvotes: 0

Aarish Ramesh
Aarish Ramesh

Reputation: 7023

Unlike C++ ,Local variables need not be initialised separately in java prior to the scope in which it is being used. So there is no need for initialising

String Key = null 

prior to its usage. It is a better programming practice to initialise a local variable only when it is used . It is also efficient in a certain sense that you allocate memory for the variable in the heap only when it is being put to use inside the for loop.

Refer the oracle docs for greater understanding on variable initialisations

http://docs.oracle.com/javase/tutorial/java/javaOO/initial.html

Upvotes: 0

Irit Katriel
Irit Katriel

Reputation: 3564

It doesn't matter in terms of efficiency, but (a) is the better of the two - it is better for code readability to define variables in the smallest possible scope.

Upvotes: 0

Jakub Kotowski
Jakub Kotowski

Reputation: 7571

There's no difference between the two cases. In both cases you are assigning a new value to the String variable.

I know what optimization you have in mind. That kind of optimization is routinely done by compilers these days. But again, it doesn't apply in your case anyway.

Upvotes: 1

Related Questions