Object类:
所有类的父类,常用方法9个
-
Object clone() 创建与该对象的类相同的新对象
①类要实现 Cloneable接口, 不然会抛出CloneNotSupportedException的异常
②clone实现的是浅拷贝,例如:Student类的属性类型是引用类型的话,经过clone得到的对象的属性和原对象的属性指向同一个地址. -
boolean equals(Object) 比较两对象是否相等
①== 比较的是地址 equal比较的是属性的值
②所有的引用类型,都应使用equal进行比较
Integer i = 12380;Integer j= new Integer(12380);
System.out.println(i == j); //false
③String类的equals是重写过的 -
void finalize() 当垃圾回收器确定不存在对该对象的更多引用时,对象垃圾回收器调用该方法
-
Class getClass() 返回一个对象运行时的实例类
其效果等同于 类名称.class -
int hashCode() 返回该对象的散列码值
①相同的两个对象hashCode是一样的,hashcode一样,但两个对象不一定相等(例如:“Aa”,“BB”)
②实例对象属性改变会导致实例对象的hashCode值改变,但不会导致类对象的hashCode值改变
③String类的hashCode是重写过的 -
String toString() 返回该对象的字符串表示
将对象转为字符串, 通常实例对象要重写 -
void notify() 激活等待在该对象的监视器上的一个线程
-
void notifyAll() 激活等待在该对象的监视器上的全部线程
-
void wait() 在其他线程调用此对象的 notify() 方法或 notifyAll() 方法前,导致当前线程等待
wait()、notify()和notifyAll() 线程协作相关
https://www.cnblogs.com/dolphin0520/p/3920385.html
String类:
参考: https://www.runoob.com/w3cnote/java-different-of-string-stringbuffer-stringbuilder.html
- 字符串常量,字符串不可变,底层由char数组实现 private final char value[];
字符串不可变 为何可反复赋值: https://www.cnblogs.com/leskang/p/6110631.html - String equals方法是Object中方法equals重写之后的
常量池位置: https://blog.youkuaiyun.com/wanderlustLee/article/details/80762851
Java6和6之前,常量池是存放在方法区(永久代)中的。
Java7,将常量池是存放到了堆中。
Java8之后,字符串常量池依然存放在堆中。,取而代之的是元空间。运行时常量池和静态常量池存放在元空间中
① 比较地址 ② instanceof 判断是否是String类型 ③ 循环比较
public boolean equals(Object anObject) {
if (this == anObject) {
return true;
}
if (anObject instanceof String) {
String anotherString = (String)anObject;
int n = value.length;
if (n == anotherString.value.length) {
char v1[] = value;
char v2[] = anotherString.value;
int i = 0;
while (n-- != 0) {
if (v1[i] != v2[i])
return false;
i++;
}
return true;
}
}
return false;
}
- String, StringBuffer,StringBuilder的区别?
它们底层都是char数组
StringBuffer是线程安全的,因为加了synchronized
StringBuilder则不是线程安全的
以下内容参考: https://blog.youkuaiyun.com/u011541946/article/details/79865160
- String st1 = new String(“abc”)创建了几个对象?
两个,一个在堆内存中,一个在常量池中 - String str1 = null;String str2 = “222222”;
String str = str1 + str2;
System.out.println(str); //null222222
{
String st1 = "a" + "b" + "c"; //直接在常量池中创建新的
String st2 = "abc";
System.out.println(st1 == st2); //true
System.out.println(st1.equals(st2));
}
{
String st1 = "ab";
String st2 = "abc";
String st3 = st1 + "c"; //走StringBuilder或者StringBuffer进行append的
System.out.println(st2 == st3); //false
System.out.println(st2.equals(st3));
}
{
String str1 = null;
String str2 = "222222";
String str = str1 + str2; // 走StringBuilder或者StringBuffer null-->"null"
System.out.println(str); //null222222
}
Math类
Math.random() 取一个0.0到1.0之间的随机数
rint 四舍五入 返回double值
Math.rint(10.1) // 10.0
Math.rint(10.7) // 11.0
Math.rint(-10.6) // -11.0
Math.rint(-10.2) // -10.0
然而 .5时奇数进一 偶数舍去
Math.rint(10.5) //10
Math.rint(9.5) //10
Math.rint(-10.5) //-10
Math.rint(-11.5) //-11
round 四舍五入 float时返回int值,double时返回long值
long round = Math.round(10.1D);
int round = Math.round(10.1F);
Math.round(-10.51) //-11
Math.round(10.7) //11
然而 正数.5加1 负数.5舍去
Math.round(10.5) //11
Math.round(11.5) //12
Math.round(-10.5) //-10
Math.round(-11.5) //-11
Random类
构造方法:
Random() //以当前时间相关的数字作为种子数
Random(long seed) //指定种子数
种子数只是随机算法的起源数字,和生成的随机数字的区间无关。
种子数相同 相同次数生成的随机数字是完全相同的
常用方法:
double nextDouble() 生成一个随机的double值,数值介于[0,1.0)之间
int nextInt() 随机的int值 -231到231-1之间
int nextInt(int n) 随机的int值介于[0,n)的区间
Math类中的random方法:
直接调用 直接调用Random类中的nextDouble方法实现的。