java中的常用类
Object类
- java.lang包是应用程序的缺省导入包
- java.lang.Object是所有类的根类(直接父类或者间接父类)
- 所有类的对象都可以声明成Object引用
- Object的子类继承了Object的方法
- getClass()方法
- equals()方法
- toString()
getClass()方法
返回一个对象的实际类型
- 比较2个对象的内容是否相等
“==”比较的地址,equals默认实现也是比较地址,如果相对内容进行比较,我们需要重写equals方法,String类以帮我们重写了该方法
String s1=new String("suns"); String s2=new String("suns"); System.out.println(s1==s2); System.out.println(s1.equals(s2));
- 如果要是对象类型,必须要重写equals方法
equals()方法重写规则
public boolean equals(Object o){ if(o==null) return false;//传入对象是否为空 if(this==o) return true;//是否为同一对象 if(this.getClass()!=o.getClass())return false;//是否同一类型 Student s=(Student)o; if(this.name.equals(s.name)&&this.age==s.age){ return true; }else { return false; } }
toString()方法
返回一个对象的字符串表示
class Student{ String name; String no; public Student(String name,String no){ this.name = name; this.no = no; } public String toString(){ return “姓名: ”+this.name+“ 学号: "+this.no; } }
String类
- String类位于java.lang包中,提供了一系列用于字符串操作的方法
- 有两种创建String对象的方法:
- String str = “abc”;
- String str = new String(“abc”);
- 每个String对象(如”abc”)都是一个不可改变的Unicode字符序列
- 任何修改字符串对象的操作(例如用”+”串接字符串)都将产生一个新的字符串对象
String类的常用方法
- 修改字符串的方法:
- concat: 串接字符串;
- subString: 截取字符串的字串;
- toLowerCase和toUpperCase: 转换为小写/大写;
- trim: 删除首尾空格或制表符;
- 搜索方法:
- startsWith和endsWith: 判断是否以指定字符串开始/结尾;
- idnexOf和lastIndexOf: 返回子串的位置索引;
- 比较方法:
- equals和equalslgnoreCase: 判断是否相等;
- 其他方法:
- length: 返回字符串长度;
案例 TestString类
public class TestString {
public static void main(String[] args) {
String str = "abcd1234";
System.out.println("substring \t" + str.substring(4));
System.out.println("toUpperCase:\t" + str.toUpperCase());
System.out.println("str:\t" + str);
System.out.println("startsWith: \t" + str.startsWith("abcd"));
System.out.println("indexOf: \t" + str.indexOf("1234"));
System.out.println("equals: \t" + str.equalsIgnoreCase("ABCD1234"));
System.out.println("length: \t" + str.length());
}
}
输出结果为:
substring 1234
toUpperCase : ABCD1234
str abcd1234
startsWith: true
indexOf: 4
equals: true
length: 8
StringBuffer和StringBuilder类
- 两个类均位于java.lang包中,提供了一系列用于字符串操作的方法
- 每个StringBuffer和StringBuilder对象都是一个可以改变的Unicode字符序列,修改字符串对象的操作不会产生新字符串
- StringBuffer和StringBuilder功能完全相同,区别在于前者是”线程安全“的,在非多线程的环境下,应优先使用后者
- 两个类均提供了相同的方法集
- append、insert和replace: 添加(在尾部)、插入和替换字符串
- indexOf和lastIndexOf: 返回子串的位置索引
- subString: 取子字符串序列
- toString: 转换为String对象
StringBuilder类实例
public class TestStringBuilder {
public static void main(String[] args) {
StringBuilder str = new StringBuilder("1234");
str.append("xyz");
System.out.println("append \t" + str.toString());
str.insert(0, "abcd");
System.out.println("insert \t" + str.toString());
System.out.println("substring \t" + str.substring(4));
System.out.println("indexOf: \t" + str.indexOf("1234"));
System.out.println("length: \t" + str.length());
}
}
输出结果为:
append 1234xyz
insert abcd1234xyz
substring 1234xyz
indexOf: 4
length: 11
Math类
- 位于java.lang包中,是一个包含了常用数学函数(均为静态方法)和常量的final类
- Math类中常用的数学函数及常量包括:
- 截断函数: ceil(取高值)、floor(取低值)、round(四舍五入)
- 变量函数: max(最大值)、min(最小值)
- 随机函数: random(0.0≤n<1.0之间)
- 常量: PI
Math类实例
import static java.lang.Math.*;
public class TestMath {
public static void main(String[] args) {
float f1 = 10.23f, f2 = 20.78f;
System.out.println(" \t" + f1 + " \t" + f2);
System.out.println("ceil:\t" + ceil(f1) + "\t" + ceil(f2));
System.out.println("floor:\t" + floor(f1) + "\t" + floor(f2));
System.out.println("round:\t" + Math.round(f1) + "\t" + round(f2));
System.out.println("max:\t" + max(f1, f2));
System.out.println("min:\t" + min(f1, f2));
System.out.println("产生10-20之间的随机整数:"
+ (int) (random() * 10 + 10));
}
}
日期类
- 常用日期类
- java.util.Date
- java.test.SimpleDateFormat