Java 杂谈程序员

Java基础(二) | 零碎知识小贴士

2017-06-20  本文已影响0人  采风JS

特意将零碎的知识点都整理出来,方便快速查阅。

一、重载与重写

二、类中成员的初始化顺序

三、序列化与反序列化

//Student类实现Serializable接口,transient关键字修饰属性不能序列化
  public static void main(String[] args) {
        Student s = new Student("lvziwei",22,"Xidian");
        File file = new File("D://s.txt");
        try {
            file.createNewFile();
        } catch (Exception e) {
            e.printStackTrace();
        }
        try{
            //序列化对象,输入输出流的判别标准为从内存中读为输出流,向内存中写为输入流
            FileOutputStream fos = new FileOutputStream(file);
            ObjectOutputStream oos = new ObjectOutputStream(fos);
            oos.writeObject(s);
            oos.flush();
            oos.close();
            fos.close();
            
            //反序列化对象
            FileInputStream fis = new FileInputStream(file);
            ObjectInputStream ois = new ObjectInputStream(fis);
            Student ss = (Student) ois.readObject();
            fis.close();
            ois.close();
            System.out.println(ss.getSchool() + "->" + ss.getAge() + "->" + ss.getName());
        }catch(Exception e){
            e.printStackTrace();
        }   
    }

四、自动装箱与拆箱

// 下面以int为例解释
Integer i = 10; //自动装箱 Integer i = Integer.valueOf(10);
int i2 = i; //自动拆箱 int i2 = Integer.intValue(i);

Integer a = 10;
Integer b = 10;
System.out.println("i == b : " + (a == b)); //true 
Integer a1 = 200;
Integer b1 = 200;
System.out.println("i1 == b1 : " + (a1 == b1)); //false

//原因解释 IntegerCache.low = -128 IntegerCache.high = 127
public static Integer valueOf(int i) {
        if (i >= IntegerCache.low && i <= IntegerCache.high)
            return IntegerCache.cache[i + (-IntegerCache.low)];
        return new Integer(i);
    }
//自动装箱时,如果整数值在-128~127之间,则自动返回缓存值;
//不在此范围内,则自动新建对象;
// 下面以String为例解释
 String str1 ="abc";
 String str2 ="abc";
 System.out.println(str2==str1); //输出为 true
 System.out.println(str2.equals(str1)); //输出为 true
 
 String str3 =new String("abc");
 String str4 =new String("abc");
 System.out.println(str3==str4); //输出为 false
 System.out.println(str3.equals(str4)); //输出为 true 

// 原因解释 "==" : 引用是否相同 "equals()" : 比较对象内容是否相同
// 字符串对象为不可改变对象,返回自身引用

五、Collection与Collections的区别

六、序列号serialVersionUID

七、Iterable和Iterator的区别

八、Map.Entry

九、Collection中的toArray()方法

    public static void main(String[] args) {
        List<Integer> list = new ArrayList<Integer>();
        list.add(1);
        list.add(2);
        list.add(3);
        Object[] i = list.toArray();
        Integer[] i = list.toArray();// 错误,只能转换为Object类型
        Integer[] ii = list.toArray(new Integer[0]);// 新建的数组长度随意,仅提供数组类型即可
    }

十、Java的跨平台性

十一、switch支持的数据类型

public class TypeTest {
    public static void main(String[] args) {
        switch (new Integer(1)) {
        case 0:
            System.out.println("The num is : 0");
            break;
        case 1:
            System.out.println("The num is : 1");
            break;
        default:
            System.out.println("The num is null");
            break;
        }
    }
}
The num is : 1
// 基于Jclasslib查看字节码文件
 0 new #16 <java/lang/Integer>
 3 dup
 4 iconst_1
 5 invokespecial #18 <java/lang/Integer.<init>>
 8 invokevirtual #21 <java/lang/Integer.intValue> //此处实现自动拆箱,调用intValue方法
11 tableswitch 0 to 1   0:  32 (+21)
    1:  43 (+32)
    default:  54 (+43)
32 getstatic #25 <java/lang/System.out>
35 ldc #31 <The num is : 0>
37 invokevirtual #33 <java/io/PrintStream.println>
40 goto 62 (+22)
43 getstatic #25 <java/lang/System.out>
46 ldc #39 <The num is : 1>
48 invokevirtual #33 <java/io/PrintStream.println>
51 goto 62 (+11)
54 getstatic #25 <java/lang/System.out>
57 ldc #41 <The num is null>
59 invokevirtual #33 <java/io/PrintStream.println>
62 return

十二、JUC原子类

十三、小数计算精度

上一篇下一篇

猜你喜欢

热点阅读