1
class Testing{
    private static int counter;
    private static int[] intArray;
    public static ReturnClassName className(File f){
        ReturnClassName returnCN= new ReturnClassName();
        byte[] b;
        try{
            DataInputStream dataIStream= new DataInputStream(new FileInputStream(f));
            intArray= new int[dataIStream.available()];
            b= new byte[dataIStream.available()];
            dataIStream.read(b);
            intArray= b;
            // setting methods for ReturnClassName 
            // counter increment
            returnCN.setNumber(someMethod(5));
            }//catch() block

    return returnCN;
}
private static int[] someMethod(int l){
    return Arrays.copyOfRange(intArray, counter, counter + l);
}

Or

class Testing{
    private static int counter;
    public static ReturnClassName className(File f){
        ReturnClassName returnCN= new ReturnClassName();
        byte[] b;
        try{
            DataInputStream dataIStream= new DataInputStream(new FileInputStream(f));
            intArray= new int[dataIStream.available()];
            b= new byte[dataIStream.available()];
            dataIStream.read(b);
            intArray= b;
            // setting methods for ReturnClassName 
            // counter increment
            returnCN.setNumber(someMethod(intArray,5));

    }//catch() block

    return returnCN;
}

private static int[] someMethod(int[] iArray, int l){

    return Arrays.copyOfRange(iArray, counter, counter + l);
}

I want to know which one is more optimized and safe of the above two codes. Also while passing the array in the 2nd code, is it passing the whole array or just the address of that array. Like both intArray and iArray are pointing to the same integer array?

5
  • codereview.stackexchange.com Commented Apr 16, 2014 at 8:24
  • stackoverflow.com/questions/12757841/… Commented Apr 16, 2014 at 8:27
  • 2
    both are exceptionally safe against runtime errors, in that neither will ever run (they won't compile). Commented Apr 16, 2014 at 8:47
  • 1
    @jwenting I agree but, on the other hand, I think kaze just wants us to consider the differences concerning local vs attribute and parameters passing. Commented Apr 16, 2014 at 8:56
  • @PabloFranciscoPérezHidalgo yes, i just want to know the difference.. Commented Apr 16, 2014 at 9:24

1 Answer 1

1

Arrays are passed by reference so both snippets are equivalent concerning efficiency except for the fact that if you are not using intArray for some other purpose: The second version will unreference the array and make it a candidate for garbage collection.

This is, in the second case, the array will be a candidate to be collected as soon as someMethod execution returns whereas the first version will keep the array referenced until the program ends since it is static.

From your comments I understand that you will call className once per file for different files and for each file you will call 'someMethod' many times. Then I like a solution similar to the firstone at some points but different to both the first and the second one.

That solution is to have a instance of Testing for each file you load data from:

  1. Force each instance to be associated with a concrete file.
  2. Make methods and attributes not static. This is for each Testing element to have its own data loaded from its file.
  3. Change className so it will load data from its file only once.
  4. Make a right user of Testing and its instances.

    class Testing{
    
        public Testing(File f)
        {
            this.f = f;
        }
    
        private File f;
        private int[] intArray;
        public static ReturnClassName className(){
            ReturnClassName returnCN= new ReturnClassName();
            byte[] b;
            if(intArray == null || intArray.length > 0) return //If it was called before, then we don't load the file again.
            {
                try{
                    DataInputStream dataIStream= new DataInputStream(new FileInputStream(f));
                    intArray= new int[dataIStream.available()];
                    b = new byte[dataIStream.available()];
                    dataIStream.read(b);
                    intArray= b;
                    // setting methods for ReturnClassName 
                    // counter increment
                } catch(Exception e) { 
                ...
                ...
                }
            }
            returnCN.setNumber(someMethod(5));
            return returnCN;
        }
    
        private int[] someMethod(int l){
            return Arrays.copyOfRange(intArray, counter, counter + l);
        }
    }
    

Example of use:

Testing forFile1 = new Testing(fileObj01);
ReturnClassName x = ReforFile1.className();
ReturnClassName y = ReforFile1.className();

Testing forFile2 = new Testing(fileObj02);
ReturnClassName z = ReforFile2.className();
ReturnClassName w = ReforFile2.className();

You could, on the other hand, implement a better solution were you have a map of integer arrays indexed by the input file (like a cache) and you keep a copy if their bytes on it. Having thus a single instance od Testing and keep File f as input parameter for 'className'.

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

13 Comments

if I am going to use the someMethod frequently, which code is more preparable.. I mean which one will execute faster?
@kaze If you are not regenerating intArray before each someMethod call then the first one but removing possible intArray repeated constructions. If you have to generate new values for intArray each time then both codes are equivalent except for the details remarked by "mok" (answer above) and the fact that the values generates for the last call won't be collected (check my answer). To sum up, I would only use a modified version of the first case (generating intArray just once) if the values of intArray are always the same.
I am not going to reinitialized 'intArray' before calling 'someMethod' again, as you can see in the code, I am passing the 'intArray' to 'someMethod' only to get a subpart of the 'intArray'. What do you mean by "generating 'intArray' just once"?
@kaze Think just a second about the second solution: Are you going to call someMethod from outside className? If that is the case, wich parameter would you pass to it?
the above two methods are the only methods in my class Testing. what i thought was if I am using the second code, the execution time might be longer than the first one (because I am passing the array and also I am going to run it in a low processor, CPU). Is my assumption correct?
|

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.