Java常用类
Object类
- Object是所有类的父类 所有类都默认继承了Object类
| 属性 | 说明 |
|---|---|
| hashCode | 返回该对象的哈希码值 |
| equals | 比较字符串是否相等 |
| toString | 返回对象的字符串表示形式 |
| getclass | 获取对象运行时类的class对象 |
hashcode
public class Demo {
public static void main(String[] args) {
String a = "张三";
String b = "李四";
System.out.println(a.hashCode()); //774889
System.out.println(b.hashCode()); //842061
System.out.println(a.hashCode() == b.hashCode()); //false
}
}equals
public class Demo1 {
public static void main(String[] args) {
String a = "likedx.com";
String a1 = new String("likedx.com");
System.out.println(a.equals(a1)); //true
String b = "likedx";
//这里用了字符串的拼接
b += ".com";
System.out.println(b.equals(a)); //true
}
}equals和==有什么区别
- equals用来比较字符串是否相同
- ==用来比较字符串的内存地址
toString
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
//这里可以用快捷键生成 alt+ins
public String toString() {
return "name = " + name + "\t" + "age = " + age;
}
}public class Demo2 {
public static void main(String[] args) {
Student s = new Student("张三",15);
//这里调用的就是toString方法
System.out.println(s); //name = 张三 age = 15
}
}getClass
public class Demo3 {
public static void main(String[] args) {
Demo3 d = new Demo3();
System.out.println(d.getClass()); //class Demo3.Demo3
}
}Math类
| 属性 | 说明 |
|---|---|
| Math.abs(a) | 取a的绝对值 |
| Math.sqrt(a) | 取a的平方根 |
| Math.cbrt(a) | 取a的立方根 |
| Math.max(a,b) | 取a、b之间的最大值 |
| Math.min(a,b) | 取a、b之间的最小值 |
| Math.pow(a,b) | 取a的b平方 |
public class Demo3 {
public static void main(String[] args) {
//abs 绝对值
System.out.println("【绝对值】"+Math.abs(-10.3));//结果: 10.3
System.out.println("【最大值】"+Math.max(10,30));//结果: 30
System.out.println("【最小值】"+Math.min(10,30));//结果: 10
System.out.println("【正弦值】"+Math.sin(3.56));//结果: -0.4063057021444168
System.out.println("【对数值】"+Math.log(20));//结果: 2.995732273553991
}
}Random类
| 属性 | 说明 |
|---|---|
| Random | 生成一个对象 |
| nextInt | 生成一个随机数,如果有参数则生成一个在0~参数之间的随机数 |
| nextBoolean | 生成一个随机的布尔值(true或false) |
| nextDouble | 生成一个在0~1之间的小数 |
| nextFloat | 生成一个在0~1之间的小数 |
| nextLong | 生成一个随机的long型数 |
public class Demo3 {
public static void main(String[] args) {
Random random = new Random();
//生成0-9之间的整数
int num = random.nextInt(10);
System.out.println(num);
int math = (int) (Math.random() * 10);
System.out.println(math);
System.out.println("==========");
//生成0-9之间的整数 用循环打印出来
for (int i = 0; i < 10; i++) {
int n = random.nextInt(10);
System.out.println(n);
}
}
}包装类
| 基本数据类型 | 引用包装类型 |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| long | Long |
| float | Float |
| double | Double |
| boolean | Boolean |
| char | Character |
public class User {
private int age;
private Integer pic;
public static void main(String[] args) {
User u = new User();
System.out.println(u.age); //0
System.out.println(u.pic); //null
}
}public class Demo4 {
public static void main(String[] args) {
int a = 10;
// 自动装箱 int --- Integer
Integer a1 = a;
//自动拆箱 Integer --- int
int i = a1.intValue();
System.out.println(a); //10
System.out.println(a1); //10
System.out.println(i); //10
}
}类型互转
public class Demo5 {
public static void main(String[] args) {
//String 转换为int
String s = "100";
int i = Integer.parseInt(s);
System.out.println("String转换int:" + "\t" + (i+1)); //String转换int: 101
//int转换为String
int i1 = 100;
String ss = String.valueOf(i1);
System.out.println("int转换为String" + "\t" + ss+1); //int转换为String 1001
//int转换为Integer
Integer integer = 100;
System.out.println("int转换为Integer" + "\t" + integer); //int转换为Integer 100
//Integer转换为int
Integer integer1 = 100;
int i2 = integer1.intValue();
System.out.println("Integer转换为int" + "\t" + i2); //Integer转换为int 100
//String转换为Integer
String s1 = "100";
Integer integer2 = Integer.valueOf(s1);
System.out.println("String转换为Integer" + "\t" + integer2); //String转换为Integer 100
//Inreger转换为String
Integer integer3 = 100;
String s2 = String.valueOf(integer3);
System.out.println("Inreger转换为String" + "\t" + s2+1); //Inreger转换为String 1001
}
}