一、toString
打印语句调用时,默认调用toString方法
来自object中的toSring方法
把实体列以字符串的类型输出
String s = String.valueOf(x);
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();//重写
}
二、hashcode
返回对象的哈希码值-----------用于判断数据是否重复
1.整数对应的hashcode就是它本身
2.单个字符的hashcode对应ASCII码值
3.字符串要通过String的hashcode来进行运算
4.自定义实体类可以自定义hashcode方法,通过类的属性来对其运算
可以把hashcode理解为方法区中常量所对应的地址
三、权限访问修饰符
public 公共的 作用域:在整个工程下任意地方都可以访问
default 默认的 作用域:同包下可以访问
protected 受保护的 作用域:同包和子类下
private 私有的 作用域:只能本类中访问
类 class 前缀只能使用 public default static final abstract
变量 field----public default static final protected private
方法 method ---- public default static final protected private
constructor 构造器
五、final
1.当final修饰类的时候 该类无法被继承。 例如String Integer
2.当final修饰方法的时候 该方法不能被重写
3.当final修饰变量的时候 该变量为常量 且常量不能被修改。
六、封装类
所对应的八大基本数据类型都有自己的所对应的封装类
Byte ------byte
Short ------short
Integer ------int
Long ------long
Float ------float
Double ------double
Boolean -----boolean
Character ----char
自动装箱和自动拆箱
Int ----Integer -----自动装箱 装箱 integer.ValueOf(int i);
Integer ----int ----自动拆箱 拆箱 intValue() 来自Number
装箱代码:
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
拆箱代码:
public int intValue() {
return value;
}
integer
父类是Number
Number的父类是Object
Number是所有数值类型封装类的父类
Integer被final所修饰,所以不能被继承
Number 是个抽象类 intvalue longvalue shortvalue 这些方法的主要目的是用来类型转换的
//byteValue() 把数字转换成byte类型 方法的实现
public byte byteValue() {
return (byte)value;
}
//Compare
public static int compare(int x, int y) {
return (x < y) ? -1 : ((x == y) ? 0 : 1);
}
Integer.parseInt(“123”) 字符串转成Integer类型
如果在参数中传递字母 会发生 java.lang.NumberFormatException 数字格式异常
底层是通过判断字符串的字符是否在‘0-9’ascii码之间 如果是则为数字,返回,如果不是则抛NumberFormatException。
Cache –缓存