【Java学习】Java常用类总结
- 一、Object类详解
- 1)getClass()
- 2)hashCode()
- 3)toString()
- 4)equals()
- 二、Math类详解
- 1)三角函数方法
- 2)指数函数方法
- 3)取整函数方法
- 4)取最大值、最小值、绝对值函数方法
- 三、Random类
- 1)Math.random()方法
- 2)Random类
- 四、包装类
- 五、String类
- String类常用方法
- 六、StringBuffer、StringBuilder类
一、Object类详解
- Object类存储在java.lang包中。
- Object是比较特殊的类,是
所有java类的父类
。 - 用户创建一个类时,除非已经指定要从其他类继承,否则他就是从java.lang.Object类继承而来的。
- 所以,在定义时可省略extends Object关键字。
1)getClass()
- 返回当前对象的
类对象型
- 应用:用于判断两个引用中实际存储对象类型是否一致
class anything{
public void test(){}
}
public class MyApplication1{
public static void main(String args[]){
anything obj = new anything();
Class class1 = obj.getClass();
System.out.println(class1);
}
}
/*
*运行结果:
*class demo.anything
*/
2)hashCode()
- 返回该对象的
哈希码值
- 哈希值根据
对象的地址
或字符串
或数字
使用hash算法计算出来的int类型的数值。 - 相同的对象返回相同的哈希值
//在上面代码的基础之上可以直接调用 hashCode()
System.out.println(obj.hashCode());
/*
*366712642
*/
3)toString()
- 返回该对象的
字符串
表示 - 应用:可以根据程序需求
覆盖
该方法
如:展示各个对象的属性值
直接调用:
package demo;
class student{
String name;
int age;
student(String name,int age){
this.name = name;
this.age = age;
}
String getName(){
return name;
}
int getAge(){
return age;
}
void setter(String name,int age){
this.name = name;
this.age = age;
}
}
public class MyApplication1{
public static void main(String args[]){
student s1 = new student("wang",20);
System.out.print(s1.toString());
}
}
/*
*demo.student@15db9742
*/
对不满足的结果可以进行重写:
package demo;
class student{
String name;
int age;
student(String name,int age){
this.name = name;
this.age = age;
}
String getName(){
return name;
}
int getAge(){
return age;
}
void setter(String name,int age){
this.name = name;
this.age = age;
}
public String toString(){
return name+":"+age;
}
}
public class MyApplication1{
public static void main(String args[]){
student s1 = new student("wang",20);
System.out.print(s1.toString());
}
}
/*
*wang:20
*/
4)equals()
- 默认实现为(this == obj),比较两个
对象地址
是否相同。 - 可进行覆盖,比较两个
对象内容
是否相同
源码实现:
public boolean equals(Object obj){
return (this==obj);
}
Q:equals 和 == 的区别 ???
A:
equals比较的是两个对象值是否相等,如果没有被重写,比较的是对象的引用地址是否相同。而==用于比较基本数据类型的值是否相等,或比较两个对象的引用地址是否相等;
二、Math类详解
- 调用形式:
Math.数学方法
常量:Math.PI 或Math.E等等 - 在Math类中包含的三角函数方法如下:
1)三角函数方法
public static double sin(double a)//正弦
public static double cos(double a)//余弦
public static double tan(double a)//正切
public static double asin(double a)//反正弦
public static double acos(double a)//反余弦
public static double atan(double a)//反正切
public static double toRadians(double angdeg)//将角度转换为弧度
public static double toDegrees(double angrad)//将弧度转换为角度
2)指数函数方法
public static double exp(double a)//e的a次方
public static double log(double a)//lna
public static double log10(double a)//底数为10的对数
public static double sqrt(double a)//a的平方根
public static double cbrt(double a)//a的立方根
public static double pow(double a,double b)//a的b次方
3)取整函数方法
public static double ceil(double a)//大于等于参数的最小整数
public static double floor(double a)//小于等于参数的最大整数
public static double rint(double a)//与参数最接近的整数(若相等,取偶数)
public static int round(float a)//加上0.5后与参数最接近的整数
4)取最大值、最小值、绝对值函数方法
public static double max(double a,double b)//a、b的最大值
public static double min(double a,double b)//a、b的最小值
public static double abs(double a)//绝对值
三、Random类
1)Math.random()方法
Math.random()//产生[0,1)的double数
(int)(Math.random()*100)//产生[0,100)的int数
50+(int)(Math.random()*100)//产生[50,150)的int数
(char)('a'+Math.random()*('z'-'a'+1))//产生a~z之间的字符
(char)(cha1+Math.random()*(cha2-cha1+1))//产生cha1~cha2之间的字符
2)Random类
除了Math.random()方法,Java还提供了一种可以获取随机数的方式,那就是java.util.Random类。
public int nextInt()
public int nextInt(int n)
public double nextDouble()
......
四、包装类
基本数据类型(8种
)与包装类的对应关系如下:
- byte → Byte
- short → Short
- int → Integer
- long → Long
- float → Float
- double → Double
- char → Character
- boolean→ Boolean
包装类的继承关系:
五、String类
- 字符串是
常量
,创建后不可改变 (重要!!!)
- 字符串字面值存储在
字符串池
中,可以共享。 - Sting s = “Hello”; 产生一个对象,字符串池中存储。
- String s = new String(“Hello”); 产生两个对象,堆、池各存储一个。
String类常用方法
length()//返回字符串长度
charAt(int index)//返回某个位置的字符
contains(String str)//判断是否包含某个子字符串
indexOf()//返回子字符串首次出现的位置
trim()//去掉字符串前后空格
toUpperCase()//转大写
toLowerCase()//转小写
statsWith(str)//判断是否str开头
endsWith(str)//判断是否str结尾
注意区分:
- String s = “a”+1+2
结果:a12 - String s = ‘a’+1+2
结果:100 - String s = 1+2+“a”
结果:3a
六、StringBuffer、StringBuilder类
- 是
可变长字符串
- 和 String 类不同的是,StringBuffer 和 StringBuilder 类的对象能够被多次的修改,并且不产生新的未使用对象。
- StringBuffer运行
效率慢、线程安全
- StringBuilder运行
效率快、线程不安全
- 可通过append(str)方法
追加
str字符串