java常用类

铁佛  金牌会员 | 2023-5-18 21:41:20 | 显示全部楼层 | 阅读模式
打印 上一主题 下一主题

主题 915|帖子 915|积分 2745

java常用类

Object类

基类,超类,所有类的直接或间接父类
object类定义的方法是所有对象都具有的方法
object类型可以存储任何对象

  • 作为参数,可以接受任何对象
  • 作为返回值,可以返回任何对象
getClass()

返回引用中存储的实际对象类型
  1. public class  Student  {
  2. public static void main(String[] args) {
  3.         Student student = new Student();
  4.         System.out.println(student.getClass());
  5.         System.out.println(student.getClass().getName());
  6.         System.out.println(student.getClass().getSimpleName());
  7.     }
  8. }
  9. /* out:
  10. class com.xxx.Student
  11. com.xxx.Student
  12. Student
复制代码
hashCode()

返回该对象的哈希码值(int)
哈希值:根据对象的地址或字符串或数字使用hash算法计算出来的int类型的数值。
一般情况下相同对象返回相同哈希码
  1. Student student = new Student();
  2. System.out.println(student.hashCode());
  3. Student student1 = new Student();
  4. System.out.println(student.hashCode()==student1.hashCode());
  5. /*
  6. 1435804085
  7. false
复制代码
toString()

返回对象的字符串表现形式
可以进行重写,自己来觉得展示的字符串形式
  1. Student student = new Student();
  2. System.out.println(student.toString());
  3. //com.xxx.Student@5594a1b5
复制代码
equals()

比较两个对象地址是否相同
也可以重写,自己觉得比较方式
  1. Student student = new Student();
  2. Student student1 = new Student();
  3. System.out.println(student.equals(student1));
  4. //false
复制代码
finalize()

当对象被判定为垃圾对象时,由JVM自动调用此方法,用来标记,进入回收队列
垃圾对象:没有有效引用指向此对象,
垃圾回收:释放数据存储空间
自动回收机制:JVM内存耗尽,一次性回收所有垃圾对象
手动回收机制:使用System.gc();通知JVM回收
垃圾回收时调用finalize(),本身为空需要重写
  1. public static void main(String[] args) {
  2.     Student student = new Student();
  3.     //后两者为垃圾
  4.     new Student();
  5.     new Student();
  6.     System.gc();
  7. }
  8. @Override
  9. protected void finalize() throws Throwable {
  10.     System.out.println("一个垃圾被回收了");
  11. }
  12. /*一个垃圾被回收了
  13. 一个垃圾被回收了
复制代码
包装类

为解决基本数据类型没有相应方法,设计了基本数据类型对应的引用数据类型,这就是包装类
包装类默认值:null
基本数据类型包装数据类型byteByteshortShortintIntegerlongLongfloatFloatdoubleDoublebooleanBooleancharCharacter包装类的方法可以自行查阅java的官方文档
类型转换和装箱、拆箱

装箱:基本类型转换成引用类型
拆箱:引用类型转换为基本类型
Number类

Number类是六个包装类以及一些其他类的子类

基本类型转引用类型

  1. int a = 2;
  2. Integer integer = new Integer(a);
  3. Integer integer1 =  Integer.valueOf(2);
复制代码
自动装箱(jdk1.5之后)
  1. int a = 2;
  2. Integer integer = a;
复制代码
引用类型转基本类型
  1. int b = integer.intValue();
复制代码
自动拆箱(jdk1.5之后)
  1. int a = 2;
  2. Integer integer = a;Integer integer1 = integer;
复制代码
parseXXX()静态方法

基本类型转字符串
  1. int n1=100;
  2. String s1 = n1+"";
  3. //或者
  4. String s2 = Integer.toString(n1);
  5. //十六进制
  6. String s2 = Integer.toString(n1,16);
复制代码
字符串转基本类型

字符串转int:Integer.parseInt(String)
字符串转其他:包装类.praseXXX(String)
boolean字符串转基本类型,除了"true"是true全是false
Integer缓冲区
  1. //因为左边创建变量在栈,栈存放堆里地址,两者地址不同不相等
  2. Integer integer1 = new Integer(100);
  3. Integer integer2 = new Integer(100);
  4. System.out.println(integer1==integer2);
  5. //自动装箱,自动装箱本质是调用Integer.ValueOf方法,可以直接看一下Integer.ValueOf方法,因为该方法中有判断条件为:如果值大于等于-128,小于等于127直接返回缓冲区(堆中)对象值,否则new一个新的Integer返回。综上所以后两者结果不同
  6. integer1 =100;
  7. integer2 =100;
  8. System.out.println(integer1==integer2);
  9. integer1 =200;
  10. integer2 =200;
  11. System.out.println(integer1==integer2);
  12. //地址相等了
  13. Integer integer3 = new Integer(100);
  14. Integer integer4 = integer3;
  15. System.out.println(integer3==integer4);
  16. /*
  17. false
  18. true
  19. false
  20. true
复制代码
String类

String的一些特性

字符串是常量,创建之后不可改变:
什么意思呢?当我们修改字符串时并不是把旧的字符串的值改成了新的值,而是在字符串池开辟新空间存储新增,并且把栈中的声明地址指向新的值。旧的值成了“垃圾”
与此同时,当我们再次什么一个新的字符串变量,并且把他赋值为和刚才已有变量相同的值,那么直接指向已有变量值的字符串池空间,不再开辟新的空间存值
使用new创建字符串时又有所不同,他是在堆中创建了的空间存储对象然后堆中空间又指向(可能说指向不对但可以这样理解)字符串池(相同值依旧指向同一个字符串池空间),总的来说创建了两个对象分别在方法区和堆中
  1. String s = "name";
  2. String s1 = "name";
  3. System.out.println(s==s1);
  4. //true
  5. String s = "name";
  6. String s1 = new String("name");
  7. System.out.println(s==s1);
  8. //equals比较的是数据
  9. System.out.println(s.equals(s1));
  10. //false
  11. //true
复制代码
字符串字面值(右边)存储在字符串池(在方法区中(方法区逻辑上独立,物理上属于堆))中,可以共享
java常用方法


这些方法应该不用我一行行的演示吧
可变字符串

由于上面描述的String的不可变性质,我们需要通过其他的类来解决这个特性带来的一些问题(效率低等)
StringBuffer类:可变长字符串在String的基础上加上了缓冲区,运行效率慢、线程安全
StringBuilder:可变长字符串,运行效率快、线程不安全
两者相对于String,效率更高,更加节省内存
  1. StringBuffer sb = new StringBuffer();
  2. //追加
  3. sb.append("hello");
  4. System.out.println(sb.toString());
  5. sb.append("world");
  6. System.out.println(sb.toString());
  7.    //添加
  8.    sb.insert(0,"最前面");
  9.    System.out.println(sb.toString());
  10.    //replace();左闭右开
  11.    sb.replace(0,3,"hello");
  12.    System.out.println(sb.toString());
  13.    //delete()
  14.    sb.delete(0,5);
  15.    System.out.println(sb.toString());
  16.    //其他方法如翻转清空自己搜索
  17. /*
  18. hello
  19. helloworld
  20. 最前面helloworld
  21. hellohelloworld
  22. helloworld
  23. //StringBuider和StringBuffer相同,单线程用前者,否则后者
复制代码
BigDecimal

关于double存在精度丢失(存储的实际上是近似值)
  1. double d1 = 1.0;
  2. double d2 = 0.9;
  3. double d3 = d1-d2;
  4. System.out.println(d3);
  5. //0.09999999999999998
复制代码
需要精确计算或是小数比较时用BigDecimal类,可以精确计算浮点数
  1.         BigDecimal bigDecimal = new BigDecimal("1.0");
  2.         BigDecimal bigDecimal1 = new BigDecimal("0.9");
  3.         //减法
  4.         System.out.println(bigDecimal.subtract(bigDecimal1));
  5.         //加法
  6.         bigDecimal.add(bigDecimal1);
  7. //        乘法
  8.         bigDecimal.multiply(bigDecimal1);
  9. //        除法
  10.         bigDecimal.divide(bigDecimal1);
  11.         
  12.         bigDecimal.subtract(bigDecimal1).divide(bigDecimal1);
  13. //+保留几位,四舍五入
  14. bigDecimal.subtract(bigDecimal1).divide(bigDecimal1,1,BigDecimal.ROUND_HALF_EVEN);
  15. //0.1
复制代码
Data

很多方法已经过时或者被Calendar类替代
不想多说自己看文档
  1. Date date = new Date();
  2. System.out.println(date.toString());
  3. //Thu May 18 19:41:35 CST 2023
复制代码
Calendar

  1. Calendar calendar = Calendar.getInstance();
  2. System.out.println(calendar.getTime().toLocaleString());
  3. //2023年5月18日 下午7:46:33
  4. //年
  5.         int year = Calendar.get(Calendar.YEAR);
  6.         //月,比实际小1
  7.         calendar.get(Calendar.MONTH);
  8. //        日
  9.         calendar.get(Calendar.DAY_OF_MONTH);
  10. //        小时
  11.         calendar.get(Calendar.HOUR_OF_DAY);//HOUR12小时,HOUR_OF_DAY24小时
  12. //        分钟
  13.         calendar.get(Calendar.MINUTE);
  14. //        秒
  15.         calendar.get(Calendar.SECOND);
复制代码
其他自己查文档
SimpleDataFormat

与语言环境有关的方式来格式化和解析日期的具体类
实现日期-->文本或者文本-->日期
  1. SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH:mm:ss");
  2.         Date date = new Date();
  3.         //格式化
  4.         String string = sdf.format(date);
  5.         System.out.println(string);
  6.         //解析
  7.         SimpleDateFormat sdf1 = new SimpleDateFormat("yyyy-MM-dd");
  8.         Date date2 = sdf1.parse("1990-05-01");
  9.         System.out.println(date2);
  10. /*2023年05月18日20:09:03
  11. Tue May 01 00:00:00 CDT 1990
复制代码
System类

构造方法私有,方法是静态的

[code]int[] arr={20,18,15,8,45,90,1,65};int[] dest=new int[8];//源数组-开始复制位置下标-目标数组-目标数组的位置-复制长度System.arraycopy(arr,0,dest,0,arr.length);for(int i=0;i

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

铁佛

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表