Day04
常用类
Object类
toString()方法
返回该对象的字符串表示,可以根据程序的需求重写该方法,比如,通常展示对象各个属性值。
System.out.println(s1.toString());//object.Student@1b6d3586
System.out.println(s2.toString());//object.Student@4554617c
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
//重写之后的运行结果:
//Student{name='null', age=0}
//Student{name='null', age=0}
equals()方法
public boolean equals (Object obj){}
默认实现为(this == obj),比较两个对象地址是否相同。
返回该对象的字符串表示,可以根据程序的需求重写该方法,比如,通常比较对象属性值是否相同,相同的话则为同一对象。
//测试equals()方法
System.out.println(s1.equals(s2));//false
//重写equals()方法后
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || this.getClass() != o.getClass()) {
return false;
}else {
Student student = (Student) o;
return this.age == student.getAge() && this.name.equals(student.getName());
}
}
Student s4 = new Student("xiaohan", 22);
Student s5 = new Student("xiaohan", 22);
System.out.println(s4.equals(s5));//true
finalize()方法
当对象确定为是垃圾的时候,由JVM自动调用此方法进行标注为垃圾对象,进入回收队列。
垃圾对象:没有有效引用指向该对象时,成为垃圾对象。
垃圾回收:由GC销毁垃圾对象,释放数据存储空间。
自动回收机制:JVM的内存耗尽,一次性回收所有的垃圾对象
手动回收机制:使用System.gc();通知JVM执行垃圾回收。
包装类
基本数据类型所对应的引用数据类型
基本数据类型 | 包装类型 |
---|---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
//JDK1.5之后,提供自动装箱和拆箱
public class Int {
public static void main(String[] args) {
//JDK1.5之后,提供自动装箱和拆箱
int age = 22;
//自动装箱
Integer integer = age;
//自动拆箱
int age1 = integer;
}
}
//基本类型和字符串之间的转换
//1.基本类型转换字符串类型
int n = 22;
//1.使用 + 号
String s1 = n + "";
System.out.println(s1.getClass());//class java.lang.String
//2.使用Integer中的toString()方法
String s2 = Integer.toString(n);
System.out.println(s2.getClass());//class java.lang.String
//2.字符串类型转换成基本类型
//使用parseXXX()方法
String str = "22";
int anInt = Integer.parseInt(str);
System.out.println(anInt);
String 类
字符串是常量,创建之后不可改变。
字符串字面值存储在字符串池中,可以共享。
public class stringTest {
public static void main(String[] args) {
String str1 = "hello";//"hello"常量存储在字符串池中。
str1 = "hello world";//"hello world"赋值给str变量,给字符串赋值时,并没有修改数据,而是重新在字符串池中开辟了一个新空间。
String str2 = "hello world";//此时的str2也没有开辟新的空间进行存储,而是在字符串池中进行了匹配,实现了共享的功能。
}
}
String类方法的使用
-
length()方法返回字符串的长度
-
charAt(int index)方法,返回某个位置的字符
-
contains(String str);判断是否包含某个字符串
-
System.out.println("=======String类方法的使用======="); String s1 = "xiaohan会变得越来越优秀的!"; System.out.println(s1.length()); System.out.println(s1.charAt(0)); System.out.println(s1.contains("xiaohan"));
-
-
toCharArray();返回字符串对应的数组
-
indexOf();返回字符串首次出现的位置
-
lastIndexOf();返回字符串最后一次出现的位置
-
String s1 = "xiaohan会变得越来越优秀的,xiaohan会是最牛逼的,xiaohan加油!"; System.out.println(Arrays.toString(s1.toCharArray())); System.out.println(s1.indexOf("xiaohan")); System.out.println(s1.indexOf("xiaohan", 4)); System.out.println(s1.lastIndexOf("xiaohan"));
-
-
trim();去掉字符串前后的空格
-
toUpperCase();将小写转成大写;toLowerCase();将大写转成小写
-
endWith(str);判断是否以 str 结尾;startWith(str);判断是否以str开头
-
String s2 = " Hello World! "; System.out.println(s2.trim()); System.out.println(s2.toLowerCase()); System.out.println(s2.toUpperCase()); System.out.println(s2.startsWith(" ")); System.out.println(s2.endsWith(" "));
-
-
replace(char old,char new);用新的字符串或字符替换旧的字符串或字符
-
split();对字符串进行拆分
-
System.out.println(s1.replace("xiaohan", "小韩")); String[] split = s2.split("[ ]+"); for (String string:split ) { System.out.println(string); }
-
StringBuffer & StringBuilder
StringBuffer:可变长字符串,JDK1.0提供,运行速度较StringBuilder慢,线程安全。
StringBuilder:可变长字符串,JDK50提供,运行速度块,线程不安全。
StringBuffer 和 StringBuilder 都比 String 效率高,节省内存。
StringBuffer 和 StringBuilder 的方法使用:
append();追加
insert();添加
replace();替换
delete();删除、清空
public class StringBufferTest {
public static void main(String[] args) {
StringBuffer s1 = new StringBuffer();
s1.append("小韩真棒,");
System.out.println(s1.toString());
s1.insert(0,"小韩会越来越优秀的,");
System.out.println(s1.toString());
s1.replace(0,5,"小韩最厉害了");
System.out.println(s1.toString());
s1.delete(0,11);
System.out.println(s1.toString());
System.out.println(s1.delete(0, s1.length()).toString());
}
}
BigDecimalTest
public class BigDecimalTest {
public static void main(String[] args) {
BigDecimal bd1 = new BigDecimal("1.0");
BigDecimal bd2 = new BigDecimal("0.9");
//减法
BigDecimal bd3 = bd1.subtract(bd2);
System.out.println(bd3);
//加法
BigDecimal bd4 = bd1.add(bd2);
System.out.println(bd4);
//乘法
BigDecimal bd5 = bd1.multiply(bd2);
System.out.println(bd5);
//除法
BigDecimal bd6 = new BigDecimal("1.4").subtract(new BigDecimal("0.5")).divide(new BigDecimal("0.9"));
System.out.println(bd6);
//保留两位小数,四舍五入
BigDecimal bd7 = new BigDecimal("10").divide(new BigDecimal("3"), 2, BigDecimal.ROUND_HALF_UP);
System.out.println(bd7);
}
}
Date类
public class DateTest {
public static void main(String[] args) {
//1.创建Date对象
//今天
Date date1 = new Date();
System.out.println(date1.toString());
System.out.println(date1.toLocaleString());
//昨天
Date date2 = new Date(date1.getTime() - (24 * 60 * 60 * 1000));
System.out.println(date2);
//2. 方法 after before
System.out.println(date1.after(date2));
System.out.println(date2.before(date1));
//比较compareTo();
System.out.println(date1.compareTo(date2));
//比较是否相等equals()
System.out.println(date1.equals(date2));
}
}
Calendar 类
Calendar 提供了获取或设置各种日历字段的方法。
构造方法:protected Calendar () : 由于修饰符是protected,所以无法直接创建该对象。通过方法(getInstance()方法)来获得。
其他方法:
方法名 | 说明 |
---|---|
static Calendar getInstance() | 使用默认时区和区域获取日历 |
void set(int year,int month,int date,int hourofday,int minute,int second) | 设置日历的年、月、日、时、分、秒。 |
int get(int field) | 返回给定日历字段的值,字段比如年、月、日等 |
void setTime(Date date) | 用给定的Date设置此日历的时间 |
Date getTime() | 返回一个Date表示此日立的时间。 |
void add(int field,int amount) | 按照日历的规则,给指定字段添加或减少时间量 |
long getTimeInMillies() | 毫秒为单位返回该日历的时间值 |
SimpleDateFormat类
public class SimpleDateFormatTest {
public static void main(String[] args) throws ParseException {
//1.创建SimpleDateFormat对象, y年 M月
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:SS");
//2.创建Date
Date date = new Date();
//格式化date(把日期转成字符串)
String s = sdf.format(date);
System.out.println(s);//2020/09/20 19:11:871
//解析(把字符串转成日期)
Date date1 = sdf.parse("2020/9/20 20:30:12");
System.out.println(date1);//Sun Sep 20 20:30:00 CST 2020
}
}
System类
System系统类,主要用于获取系统的属性数据和其他操作,构造方法私有的。
方法名 | 说明 |
---|---|
static void arraycopy(…) | 复制数组 |
static long currentTimeMills(); | 获取当前系统时间,返回的是毫秒值 |
static void gc(); | 建议JVM赶快启动垃圾回收期回收垃圾 |
static void exit(int status); | 退出JVM,如果参数是0表示正常退出JVM,非0表示异常退出JVM |
public class SystemTest {
public static void main(String[] args) {
int[] arr = {1,2,3,4};
int[] dest = new int[arr.length];
System.arraycopy(arr,0,dest,0,arr.length);
for (int i = 0; i < dest.length; i++) {
System.out.println(dest[i]);
}
}
}