Java代码优化和安全


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);
}

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);
}

我想知道上面两个代码中哪一个更优化、更安全。此外,在传递第二个代码中的数组时,它是传递整个数组还是仅传递该数组的地址。就像intArray和iArray都指向同一个整数数组一样?

数组是通过引用传递的,因此这两个片段在效率方面是等效的,但如果没有将intArray用于其他目的:第二个版本将取消引用数组,并使其成为垃圾收集的候选。

也就是说,在第二种情况下,一旦someMethod执行返回,数组将成为要收集的候选者,而第一个版本将保持引用数组,直到程序结束,因为它是静态的。

从您的评论中,我了解到对于不同的文件,您将为每个文件调用className一次,对于每个文件,您会多次调用"someMethod"。然后我喜欢一个在某些方面类似于第一个的解决方案,但与第一个和第二个都不同。

该解决方案是为从中加载数据的每个文件提供一个Testing实例:

  1. 强制每个实例与一个具体文件相关联
  2. 使方法和属性不是静态的。这是为了让每个Testing元素从其文件中加载自己的数据
  3. 更改className,使其仅从文件中加载数据一次
  4. 成为Testing及其实例的正确用户。

    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);
        }
    }
    

使用示例:

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();

另一方面,如果你有一个由输入文件索引的整数数组的映射(比如缓存(,并且如果它们的字节在上面,你可以保留一个副本,那么你可以实现一个更好的解决方案。因此,有一个单独的实例od Testing,并保留File f作为"className"的输入参数。

最新更新