Java百问

来源:互联网 发布:mac怎么查看群相册 编辑:程序博客网 时间:2024/05/16 15:42

http://www.programcreek.com/2013/04/java%E7%99%BE%E9%97%AE/


5.关于double的一个常见问题

6. String 不变性。

7  HashMap中 一定复写

public int hashCode()  //产生一个散列值 ,为了存取方便,相当于地址
public boolean equals(Object obj)


1. If two objects are equal, then they must have the same hash code.
2. If two objects have the same hashcode, they may or may not be equal.

import java.util.HashMap;public class Apple {    private String color;    public Apple(String color) {        this.color = color;    }    public int hashCode(){        System.out.println("判断hashcode");        return this.color.length();    }    public boolean equals(Object obj) {        System.out.println("判断equals");        if (!(obj instanceof Apple))            return false;        if (obj == this)            return true;        return this.color == ((Apple) obj).color;    }    public static void main(String[] args) {        Apple a1 = new Apple("green");        Apple a2 = new Apple("red");        Apple a3 = new Apple("green");        //hashMap stores apple type and its quantity        HashMap<Apple, Integer> m = new HashMap<Apple, Integer>();        m.put(a1, 10);//判断hashcode        m.put(a2, 20);//判断hashcode        m.put(a3, 30);//判断hashcode                        判断equals        System.out.println(m.get(new Apple("green")));        //判断hashcode        //判断equals        //30        System.out.println(m.size());        //2       }}

13 Overriding and overloading

在同一个类中,出现多个同名的方法的现象就是Overload

覆写掉继承来的东西是Overriding。 用于多态。
14 Fields不能被Overridden


19 私有构造函数:避免外部创建对象,用于单例。







39.Java是值传递还是址传递?为什么这样设计

值传递。object对象本身就是reference.



0 0
原创粉丝点击