常用类
Object类
超类、基类,所有累得直接或者间接弗雷,位于继承树的最顶层
任何类,如果没有书写extends继承某个类,都默认直接继承Object类,否则为间接继承
Object类中所定义的方法,是所有对象都具备的方法
Object类型可以存出任何对象:作为参数,可接受任何对象;作为返回值,可返回任何对象。
getClass()方法
返回引用中存储的实际对象类型
应用:常用于判断两个引用中实际存储对象类型是否一致
hashCode()方法
返回该对象的哈希值
哈希值是根据对象的地址或字符串或数字使用hash算法计算出来的int类型的数值
一般情况下,相同的对象返回相同的哈希值(判断两个对象是不是同一个)
toString()方法
返回该对象的字符串表示(表现形式)
可以根据程序的需求覆盖这个方法,如:展示对象的各个属性值
public String toString() {
return getClass().getName() + "@" + Integer.toHexString(hashCode());
}
equals()方法
比较两个对象地址是否相同
可以进行覆盖,比较两个对象的内容是否相同
重写的思想:
- 判断两个对象是否为同一引用,同一引用说明内容相同
- 判断和其对比的内容是不是空值,如果是空,则不存在相同比较
- 判断两者是否为相同类型(由于比较的对象为最底层的子类,所以用instanceof)
- 强制类型转换
finalize()方法
当对象被判定为垃圾对象的时候,由JVM自动改调用此方法,用以标记垃圾对象,进入回收队列
垃圾对象:没有有效引用指向此对象时,为垃圾对象
垃圾回收:由GC销毁垃圾对象,释放数据存储空间
自动回收机制:JVM的内存耗尽,一次性回收所有垃圾对象
手动回收机制:使用System.gc();通知JVM执行垃圾回收
代码实例
public class GraduateStudent {
private String name;
private int age;
public GraduateStudent(){}
public GraduateStudent(String name, int age) {
super();
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;
}
@Override
public String toString() {
return name+":"+age+" years old";
}
@Override
public boolean equals(Object obj) {
if (this == obj){
return true;
}
if (obj == null){
return false;
}
if(obj instanceof GraduateStudent){
GraduateStudent gs = (GraduateStudent)obj;
if (this.name.equals(gs.getName())&&this.age==gs.getAge()){
return true;
}else {
return false;
}
}
return false;
}
@Override
protected void finalize() throws Throwable {
System.out.println(this.name+"对象被回收了");
}
}
public class Application {
public static void main(String[] args) {
GraduateStudent gs1 = new GraduateStudent("wyb",23);
GraduateStudent gs2 = new GraduateStudent("wyf",24);
GraduateStudent gs3 = new GraduateStudent("wyb",23);
//判断gs1 gs2是不是一个类型
Class class1 = gs1.getClass();
Class class2 = gs2.getClass();
Class class3 = gs3.getClass();
if (class1 == class2){
System.out.println("The class of the gs1 is same to the gs2.");
}else{
System.out.println("The class of the gs1 is different from the gs2.");
}
if (class1 == class3){
System.out.println("The class of the gs1 is same to the gs3.");
}else{
System.out.println("The class of the gs1 is different from the gs3.");
}
System.out.println("==================hashCode方法=================");
//hashCode方法
System.out.println(gs1.hashCode());
System.out.println(gs2.hashCode());
System.out.println(gs3.hashCode());
GraduateStudent gs4 = gs1;
System.out.println(gs4.hashCode());
System.out.println("==================toString方法(练习重写)=================");
//toString方法
System.out.println(gs1.toString());
System.out.println(gs3.toString());
System.out.println("==================equals方法(练习重写)=================");
//equals方法
System.out.println(gs1.equals(gs3));
System.out.println("==================finalize方法(练习重写)=================");
//finalize方法
GraduateStudent gs5 = new GraduateStudent("aaa",30);
GraduateStudent gs6 = new GraduateStudent("bbb",30);
new GraduateStudent("ccc",30);
new GraduateStudent("ddd",30);
GraduateStudent gs9 = new GraduateStudent("eee",30);
//回收垃圾
System.gc();
System.out.println("回收垃圾");
}
}
包装类
基本数据类型所对应的引用数据类型(基本数据类型只能用基本运算符操作运算,为了扩展功能而出现了包装类)
把基本类型的数据放进堆里
Object可统一所有数据,包装类的默认值是null
| 基本数据类型 | 包装类型 |
|---|---|
| byte | Byte |
| short | Short |
| int | Integer |
| float | Float |
| boolean | Boolean |
| char | Character |
| double | Double |
| long | Long |
类型转换与装箱、拆箱
基本类型=====>引用类型(装箱)
基本类型<=====引用类型(拆箱)
8种包装类提供不同类型之间的转换方式
- Number父类中提供的六个共性方法
- parseXXX()静态方法
- valueOf()静态方法
注意:要保证类型兼容,否则会抛出NumberFormatException异常
代码实例
public class Demo01 {
public static void main(String[] args) {
//装箱操作 基本类型=====>引用类型
int num1 = 18;
Integer integer1 = new Integer(num1);
Integer integer2 = Integer.valueOf(num1);
//拆箱操作 基本类型<=====引用类型
Integer integer3 = new Integer(10);
int num2 = integer3.intValue();
System.out.println("装箱");
System.out.println(integer1);
System.out.println(integer2);
System.out.println("拆箱");
System.out.println(num2);
//JDK1.5之后提供了自动装箱和开箱
int age =30;
//自动装箱
System.out.println("自动装箱");
Integer integer4 = age;
System.out.println(integer4);
//自动拆箱
System.out.println("自动拆箱");
int age2 = integer4;
System.out.println(age2);
//基本类型和字符串之间的转换
System.out.println("===============基本类型和字符串之间的转换===============");
System.out.println("基本类型====>字符串");
int num3 = 11927050;
String s1 = num3+"";//使用+号
String s2 = Integer.toString(num3);//使用toString方法
String s3 = Integer.toString(num3,16);//使用toString方法,后面表示进制
System.out.println(s3);
System.out.println("基本类型<====字符串");
String s4 = "11927050";
int num4 = Integer.parseInt(s4);
System.out.println(num4);
//boolean字符串转换成基本类型,只有"true"会转换成true,其他的都会变成false
String s5 = "true";
String s6 = "amazing";
boolean b1 = Boolean.parseBoolean(s5);
boolean b2 = Boolean.parseBoolean(s6);
System.out.println(b1);
System.out.println(b2);
}
}
整数缓冲区
Java预创了256个常用的整数包装类型对象(-128~127)
在实际应用中,对已创建的对象进行复用(节省内存消耗)
代码实例
public class Demo02 {
public static void main(String[] args) {
//interview question
Integer integer1 = new Integer(100);
Integer integer2 = new Integer(100);
System.out.println(integer1==integer2);
Integer integer3 = Integer.valueOf(100);
Integer integer4 = Integer.valueOf(100);
System.out.println(integer3==integer4);//true
Integer integer5 = Integer.valueOf(128);
Integer integer6 = Integer.valueOf(128);
System.out.println(integer5==integer6);//false
}
}
String类
字符串是常量,创建之后不能改变。
字符串字面值存储在字符串池中,可以共享。

代码实例
public class Demo01 {
public static void main(String[] args) {
String name = "Faye";//"Faye"常量存储在字符串池(常量池)中
name = "Chile";//"Chile"赋值给name变量 没有修改数据,而是重新开辟了一个空间。(不可变性)
String name2 = "Chile";
//另一种创建字符串的方式
String name3 = new String("Nikita");//浪费空间
String name4 = new String("Nikita");
System.out.println(name4 == name3);//比地址
System.out.println(name3.equals(name4));//比内容
}
}
常用方法
public class Demo02 {
public static void main(String[] args) {
/*字符串方法的使用
1. length()返回字符串长度
2. charAt(int index)返回某个位置的字符
3. contains(String str)判断是否包含某个子字符串
4. toCharArray()将字符串转换为数组
5. indexof()返回子字符串首次出现的位置
6. lastindexof()返回字符串最后一次出现的位置
7. trim()去掉字符串前后的空格
8. toUpperCase()把小写变成大写
9. toLowerCase()把大写变成小写
10. endwith() startwieh() 是否以…结尾/开始
11. replace()用新的字符串来替换旧的字符串
12. split()对字符串进行拆分
13. equals compare 比较大小
*/
String sentence = "Faye is my favorite singer.Faye is my favorite singer.Faye is my favorite singer.";
System.out.println(sentence.length());
System.out.println(sentence.charAt(sentence.length()-1));
System.out.println(sentence.contains("Faye"));
System.out.println(Arrays.toString(sentence.toCharArray()));
System.out.println(sentence.indexOf("e"));
System.out.println(sentence.indexOf("e",4));//从哪个脚标开始寻找
System.out.println(sentence.lastIndexOf("e"));
System.out.println(sentence.replace("Faye","FaYe"));
String[] arr = sentence.split("[ .]");
System.out.println(arr.length);
for (String s : arr) {
System.out.println(s);
}
String s1 = "I LOVE YOU!";
String s2 = "I love you!";
System.out.println(s1.equals(s2));
System.out.println(s1.equalsIgnoreCase(s2));
String s3 = "abc";
String s4 = "def";
String s5 = "abcdefghijklmn";
System.out.println(s3.compareTo(s4));
System.out.println(s3.compareTo(s5));
}
}
练习题
public class Demo03 {
public static void main(String[] args) {
String str = "this is a text";
Arrays arrays1[];
str.replace("text","easy practice");
String [] arr = str.split(" ");
for (String s : arr) {
System.out.println(s);
}
for(int i = 0;i<arr.length;i++){
char first = arr[i].charAt(0);
char uf = Character.toUpperCase(first);
String news = uf + arr[i].substring(1);
System.out.println(news);
}
}
}
可变字符串
StringBuffer:可变长字符串,JDK1.0提供,运行效率慢,线程安全
StringBuilder:可变长字符串,JDK1.5提供,运行效率快,效率不安全
区别:1. 效率比String高 2. 节省内存
代码实例
public class Demo04 {
//StringBuffer StringBuilder
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
// append()追加内容
sb.append("Faye");
System.out.println(sb);
sb.append(" is the best singer.");
System.out.println(sb);
// insert()添加
sb.insert(0,"I think ");
System.out.println(sb);
// replace()替换
sb.replace(0,2,"Everyone ");
System.out.println(sb);
// delete()删除
sb.delete(0,15);
System.out.println(sb);
StringBuilder sb2 = new StringBuilder();
}
}
public class Demo05 {
//验证StringBuilder效率高于String
public static void main(String[] args) {
//设置开始时间
long start = System.currentTimeMillis();
StringBuffer sb = new StringBuffer();
//StringBuilder sb = new StringBuilder();
for (int i = 0;i<99999;i++){
sb.append(i);
}
System.out.println(sb);
long end = System.currentTimeMillis();
System.out.println("time is "+(end-start));
}
}
BigDecimal类
double和float的存储都是近似值
BigDecimal是精确存储,可以用于大的浮点数的精确计算
位于java.math包中
public class Demo06 {
public static void main(String[] args) {
double d1 = 0.9;
double d2 = 0.8;
System.out.println(d1-d2);
System.out.println((d1-d2)/0.1);
BigDecimal bd1 = new BigDecimal("1.0");
BigDecimal bd2 = new BigDecimal("0.9");
BigDecimal bd4 = new BigDecimal("0.05");
BigDecimal bd5 = new BigDecimal("0.05");
BigDecimal bd3 = bd1.subtract(bd2);
BigDecimal bd6 = bd4.add(bd5);
System.out.println(bd6);
System.out.println(bd3);
BigDecimal bd7 = new BigDecimal("1.4").subtract(new BigDecimal("0.5")).divide(new BigDecimal("0.9"));
BigDecimal bd8 = new BigDecimal("10").divide(new BigDecimal("3"),2,BigDecimal.ROUND_HALF_UP);
System.out.println(bd8);
}
}
Date类
表示特定的瞬间 精确到毫秒 Date类中的大部分方法都被Calendar类中的方法取代了。
public class Demo08 {
public static void main(String[] args) {
Calendar calendar = Calendar.getInstance();
System.out.println(calendar.getTime().toLocaleString());
System.out.println(calendar.getTimeInMillis());
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
int hour = calendar.get(Calendar.HOUR_OF_DAY);//HOUR_OF_DAY=====>24h HOUR===>12h
int minute = calendar.get(Calendar.MINUTE);
int second = calendar.get(Calendar.SECOND);
System.out.println(year+"年"+month+"月"+day+"日"+hour+":"+minute+":"+second);
//修改时间
Calendar calendar2 = Calendar.getInstance();
calendar2.set(Calendar.DAY_OF_MONTH,5);
System.out.println(calendar2.getTime().toLocaleString());
//add方法修改时间
calendar2.add(Calendar.HOUR,1);
System.out.println(calendar2.getTime().toLocaleString());
//补充方法
calendar2.add(Calendar.MONTH,1);
int max = calendar2.getActualMaximum(Calendar.DAY_OF_MONTH);
int min = calendar2.getActualMinimum(Calendar.DAY_OF_MONTH);
System.out.println(max);
System.out.println(min);
}
}
SimpleDateFormat类
public class Demo09 {
public static void main(String[] args) throws Exception{
SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
Date date = new Date();
String str = sdf.format(date);
System.out.println(str);
//把字符串转换成日期
Date date2 = sdf.parse("1997年01月13日23:59:59");
System.out.println(date2);
}
}
System类
主要用于获取系统的属性数据和其他操作,构造方法私有
| 方法名 | 说明 |
|---|---|
| arraycopy() | 复制数组 |
| long currentTimeMills() | 获取当前系统时间,返回毫秒值 |
| gc() | 建议JVM赶快启动垃圾回收期回收垃圾 |
| exit(int status) | 退出JVM,如果参数是0表示正常退出JVM,非0表示异常退出JVM |
public class Demo10 {
public static void main(String[] args) {
//arraycopy()数组复制
//src原数组 srcPos从哪个位置开始复制0 dest目标数组 destPos目标数组位置 length:复制的长度
int[] arr = {1,2,3,4,5,5,6,76,7,87,99,100};
int[] arrd = new int[8];
System.arraycopy(arr,0,arrd,0,arrd.length);
for (int i : arrd) {
System.out.println(i);
}
System.out.println(System.currentTimeMillis());
long start = System.currentTimeMillis();
for (int i = 0; i < 999999999; i++) {
for (int j = 0; j <999999999 ; j++) {
int result = i+j;
}
}
long end = System.currentTimeMillis();
System.out.println(end-start);
}
}
浙公网安备 33010602011771号