zoukankan      html  css  js  c++  java
  • java要注意的问题1

    一.优先返回空集合而非null

    如果程序要返回一个不包含任何值的集合,确保返回的是空集合而不是null。这能节省大量的”if else”检查。

    public class getLocationName {
        return (null==cityName ? "": cityName);
    }

    二.谨慎操作字符串

    如果两个字符串在for循环中使用+操作符进行拼接,那么每次循环都会产生一个新的字符串对象。这不仅浪费内存空间同时还会影响性能。类似的,如果初始化字符串对象,尽量不要使用构造方法,而应该直接初始化。比方说:

    //Slower Instantiation
    String bad = new String("Yet another string object");
    
    //Faster Instantiation
    String good = "Yet another string object"

    三.避免无用对象

    创建对象是Java中最昂贵的操作之一。因此最好在有需要的时候再进行对象的创建/初始化。如下:

    import java.util.ArrayList;
    import java.util.List;
    
    public class Employees {
    
        private List Employees;
    
        public List getEmployees() {
    
            //initialize only when required
            if(null == Employees) {
                Employees = new ArrayList();
            }
            return Employees;
        }
    }

    四.数组与ArrayList之争

    开发人员经常会发现很难在数组和ArrayList间做选择。它们二者互有优劣。如何选择应该视情况而定。

    import java.util.ArrayList;
    
    public class arrayVsArrayList {
    
        public static void main(String[] args) {
            int[] myArray = new int[6];
            myArray[7]= 10; // ArraysOutOfBoundException
    
            //Declaration of ArrayList. Add and Remove of elements is easy.
            ArrayList<Integer> myArrayList = new ArrayList<>();
            myArrayList.add(1);
            myArrayList.add(2);
            myArrayList.add(3);
            myArrayList.add(4);
            myArrayList.add(5);
            myArrayList.remove(0);
    
            for(int i = 0; i < myArrayList.size(); i++) {
            System.out.println("Element: " + myArrayList.get(i));
            }
    
            //Multi-dimensional Array 
            int[][][] multiArray = new int [3][3][3]; 
        }
    }
    • 数组是定长的,而ArrayList是变长的。由于数组长度是固定的,因此在声明数组时就已经分配好内存了。而数组的操作则会更快一些。另一方面,如果我们不知道数据的大小,那么过多的数据便会导致ArrayOutOfBoundException,而少了又会浪费存储空间。
    • ArrayList在增删元素方面要比数组简单。
    • 数组可以是多维的,但ArrayList只能是一维的。

    五.判断奇数

    看下这几行代码,看看它们是否能用来准确地判断一个数是奇数?  

    public boolean oddOrNot(int num) {
        return num % 2 == 1;
    }

    看似是对的,但是每执行四便会有一个错误的结果(用数据说话)。考虑到负奇数的情况,它除以2的结果就不会是1。因此,返回值是false,而这样是不对的。

    代码可以修改成这样:

    public boolean oddOrNot(int num) {
        return (num & 1) != 0;
    }

    这么写不光是负奇数的问题解决了,并且还是经过充分优化过的。因为算术运算和逻辑运行要比乘除运算更高效,计算的结果也会更快。

    六.单引号与双引号的区别

    public class Haha {
        public static void main(String args[]) {
        System.out.print("H" + "a");
        System.out.print('H' + 'a');
        }
    }

    看起来这段代码会返回”Haha”,但实际返回的是Ha169。原因就是用了双引号的时候,字符会被当作字符串处理,而如果是单引号的话,字符值会通过一个叫做基础类型拓宽的操作来转换成整型值。然后再将值相加得到169。

    七.一些防止内存泄露的小技巧

    内存泄露会导致软件的性能降级。由于Java是自动管理内存的,因此开发人员并没有太多办法介入。不过还是有一些方法能够用来防止内存泄露的。

    • 查询完数据后立即释放数据库连接
    • 尽可能使用finally块
    • 释放静态变量中的实例
    • 避免死锁

    死锁出现的原因有很多。避免死锁不是一句话就能解决的。通常来说,当某个同步对象在等待另一个同步对象所拥有的资源上的锁时,便会产生死锁。

    试着运行下下面的程序。它会告诉你什么是死锁。这个死锁是由于两个线程都在等待对方所拥有的资源,因此会产生死锁。它们会一直等待,没有谁会先放手。

    public class DeadlockDemo {
       public static Object addLock = new Object();
       public static Object subLock = new Object();
    
       public static void main(String args[]) {
    
          MyAdditionThread add = new MyAdditionThread();
          MySubtractionThread sub = new MySubtractionThread();
          add.start();
          sub.start();
       }
    private static class MyAdditionThread extends Thread {
          public void run() {
             synchronized (addLock) {
            int a = 10, b = 3;
            int c = a + b;
                System.out.println("Addition Thread: " + c);
                System.out.println("Holding First Lock...");
                try { Thread.sleep(10); }
                catch (InterruptedException e) {}
                System.out.println("Addition Thread: Waiting for AddLock...");
                synchronized (subLock) {
                   System.out.println("Threads: Holding Add and Sub Locks...");
                }
             }
          }
       }
       private static class MySubtractionThread extends Thread {
          public void run() {
             synchronized (subLock) {
            int a = 10, b = 3;
            int c = a - b;
                System.out.println("Subtraction Thread: " + c);
                System.out.println("Holding Second Lock...");
                try { Thread.sleep(10); }
                catch (InterruptedException e) {}
                System.out.println("Subtraction  Thread: Waiting for SubLock...");
                synchronized (addLock) {
                   System.out.println("Threads: Holding Add and Sub Locks...");
                }
             }
          }
       }
    }

    输出:

    Addition Thread: 13
    Subtraction Thread: 7
    Holding First Lock...
    Holding Second Lock...
    Addition Thread: Waiting for AddLock...
    Subtraction  Thread: Waiting for SubLock...

    但如果调用的顺序变一下的话,死锁的问题就解决了。

    public class DeadlockSolutionDemo {
       public static Object addLock = new Object();
       public static Object subLock = new Object();
    
       public static void main(String args[]) {
    
          MyAdditionThread add = new MyAdditionThread();
          MySubtractionThread sub = new MySubtractionThread();
          add.start();
          sub.start();
       }
    
    private static class MyAdditionThread extends Thread {
          public void run() {
             synchronized (addLock) {
            int a = 10, b = 3;
            int c = a + b;
                System.out.println("Addition Thread: " + c);
                System.out.println("Holding First Lock...");
                try { Thread.sleep(10); }
                catch (InterruptedException e) {}
                System.out.println("Addition Thread: Waiting for AddLock...");
                synchronized (subLock) {
                   System.out.println("Threads: Holding Add and Sub Locks...");
                }
             }
          }
       }
    
       private static class MySubtractionThread extends Thread {
          public void run() {
             synchronized (addLock) {
            int a = 10, b = 3;
            int c = a - b;
                System.out.println("Subtraction Thread: " + c);
                System.out.println("Holding Second Lock...");
                try { Thread.sleep(10); }
                catch (InterruptedException e) {}
                System.out.println("Subtraction  Thread: Waiting for SubLock...");
                synchronized (subLock) {
                   System.out.println("Threads: Holding Add and Sub Locks...");
                }
             }
          }
       }
    }

    输出:

    Addition Thread: 13
    Holding First Lock...
    Addition Thread: Waiting for AddLock...
    Threads: Holding Add and Sub Locks...
    Subtraction Thread: 7
    Holding Second Lock...
    Subtraction  Thread: Waiting for SubLock...
    Threads: Holding Add and Sub Locks...
  • 相关阅读:
    索引有什么用?
    数据类型的优化策略?
    MySQL的锁策略有什么?
    行锁
    Innodb_lock_waits
    Innodb_locks表
    软件安装笔记
    Spring Boot学习笔记
    AI学习总结
    笔试面试题总结
  • 原文地址:https://www.cnblogs.com/Eason-S/p/5417480.html
Copyright © 2011-2022 走看看