java常用类(部分)
Object类
-
getClass()方法:返回一个类。
例:
//创建一个Student类 public class Student{ } //再创建一个测试类 ppublic class StudentTest{ Student st1 = new Student(); Student st2 = new Student(); //判断st1和st2是不是通一个类型,用getClass()方法 Class cla1 = st1.getClass(); Class cla2 = st2.getClass(); if(cla1 == cla2){ System.out.print("st1和st2是同一个属性"); }else System.out.print("st1和st2不是同一个属性"); } //结果输出:st1和st2是同一个属性
-
hashCode()方法
- public int hashCode(){}
- 返回该对象的哈希码值;
- 哈希值根据对象的地址或字符串或数字使用hash算法计算出来的int类型的数值;
- 一般情况下相同对象返回相同哈希码;
//输出上面代码的hash码 System.out.print(st1.hashCode()); System.out.print(st1.hashCode()); //两个输出的hash码不一样,因为指向了不同的内存地址 //再来个st3 = st1,st3的hash码和st1的就一样了
-
toString()方法
- public String toString(){}
- 返回该对象的字符串表示;
- 可以根据程序需求覆盖该方法;
-
finalize()方法
- 当对象被判定为垃圾对象时,由JVM自动调用此方法,用以标记垃圾对象,进入回收队列;
- 垃圾对象:没有有效引用指向此对象时,为垃圾对象;
- 垃圾回收:由GC销毁垃圾对象,释放数据存储空间;
- 自动回收机制:使用System.gc();通知JVM执行垃圾回收;
包装类
-
基本类型数据存储在栈中,引用类型数据存储在堆中。
-
基本数据类型 包装类 byte Byte boolean Boolean short Short int Integer long Long float Float double Double char Character
类型转换与装箱、拆箱
-
将栈中的数据转到堆中为装箱(基本类型-->引用类型);
-
将堆中的对象转到栈中为拆箱 (引用类型-->基本类型);
-
装箱,基本类型转为引用类型
//基本类型 int num = 10; //使用Integer类创建对象 Integer integer = new Integer(num);
-
拆箱,引用类型转为基本类型
//引用类型 Integer integer = new Integer(100); //基本类型接收引用类型值 int num = integer.intValue();
-
JDK1.5后自动装拆箱
int age = 10; //自动装箱 Integer integer = age; //自动拆箱 int age2 = integer;
-
基本类型转为字符串(toString()方法)
int i = 10; //1.使用“+” String s1 = i + ""; //2.使用Integer中的toString()方法 String s2 = Integer.toString(i);
-
字符串转为基本类型
String str = "150"; //使用Integr.parseXXX(); int n = Integer.parseInt(str); //注:字符串中不能出现除数字字符之外的字符
-
整数缓冲区(面试题)
Integer integer1 = new Integer(100);
Integer integer2 = new Integer(100);
System.out.print(integer1 == integer2); //false
//因为在堆中创建两个不同对象,存放在不同地址
//--------------------------------------------
Integer integer3 = 100; //自动装箱 实际上为:Integer integer3 = Integer.valueOf(100);
Integer integer4 = 100;
System.out.print(integer3 == integer4); //true
//--------------------------------------------
Integer integer5 = 200;
Integer integer6 = 200;
System.out.print(integer5 == integer6); //false
/*
在Integer类中定义了范围为-128~127之间,在范围内的数值在堆中创建一个地址,创建的对象都指向这个地址。
不在范围中的数值执行new Integer();
*/
String类
-
字符串是常量,创建之后不可改变;
-
字符串字面值存储在字符串池中,可以共享;(字符串池在方法区中)
-
String name = "Hello";产生一个对象,在字符串池中存储;
-
String name = new String("Hello");产生两个对象,一个在堆中,一个在字符串池中;
String name = "Hello"; //"Hello"存储在字符串池中 name = "zhangsan"; //"zhangsan"赋值给name /* 并不是修改创建好的"Hello",而是重新开辟一块空间存放"zhangsan",然后name指向"zhangsan" */ String name2 = "zhangsan"; //字符串的另一种定义方式 String str = new String("java"); String str2 = new String("java");
- String常用方法
- public int length();返回字符串的长度;
- public char charAt(int index); 根据下标获取字符;
- public boolean contains(String str); 判断当前字符串是否包含str;
- public char[] toCharArray(); 将字符串转换为数组;
- public int indexOf(String str); 查找str首次出现的下标,存在则返回下标,不存在则返回-1;
- public int lastIndexOf(String str); 查找字符串在当前字符串中最后一次出现的下标索引;
- public String trim(); 去掉字符串前后空格;
- public StringtoUpperCase(); 将小写转为大写;
- public boolean endWith(String str);判断字符串是否以str结尾;
- public String replace(char oldChar , char newChar); 将旧字符串替换成新字符串;
- public String[] split(String str); 根据str做拆分;
可变字符串
-
StringBuffer:可变长字符串,JDK1.0提供,相比StringBuilder运行效率慢,相比String 快、线程安全;
提前开辟一个缓冲区,在缓冲区中操作。
StringBuffer stb = new StringBuffer(); //1.append(); 追加 stb.append("java"); //2.insert(); 添加,可指定位置;以下表示第0位添加 stb.insert(0,"前面"); //3.replace(); 替换,替换指定范围字符串;一下将0-3替换为"hello" stb.replace(0,3,"hello"); //4.delete();删除,删除指定范围字符串 stb.delete(0,5); //
-
StringBuilder:可变长字符串,JDK5.0提供,运行效率快、线程不安全;
StringBuilder用法同StringBuffer
BigDecimal
double d1 = 1.0;
double d2 = 0.9;
System.out.print(d1 - d2);
//输出结果为:0.0999999999998
//面试题
double result = (1.4 - 0.5)/0.9;
System.out.print(result);
//输出结果:0.99999999999999
//double存的是近似值
-
很多实际应用中需要精确运算,而double是近似值存储,不再符合要求,需要借助BigDecimal;
-
BigDecimal:
-
位置:java.math包中;
-
作用:精确计算浮点数;
-
创建方式:BigDecimal bd = new BigDecimal("1.0");
//BigDecimal,大的浮点数精确计算 BigDecimal bd1 = new BigDecimal("1.0"); BigDecimal bd2 = new BigDecimal("0.9"); BigDecimal r1 = bd1.subtract(bd2); //加法为:add(); 乘法为:mutiply(); 除法为:divide() System.out.print(r1); //输出结果0.1
-
SimpleDateFormat
//创建SimpleDateFormat对象表示y年M月d日
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
//创建Date
Date date = new Date();
//格式化date
String str = sdf.format(date);
System.out.print(str);
//-----------------------------------
//解析(把字符串转成日期)
Date date2 = sdf.parse("1900/10/10");