常用类

1.String类

charAt(int index) 返回指定位置的字符 toCharArray() 将字符串转换为char数组 replace(String oldStr,String newStr) 替换指定内容的字符串 replaceAll(String regx,String newStr) 支持正则表达式的字符串替换 isEmpty() 判断是否为null 长度为0 返回为true 否则false

  1. package com.qfedu.test1;
  2. /**
  3. * charAt(int index) 返回指定位置的字符
  4. * toCharArray() 将字符串转换为char数组
  5. * replace(String oldStr,String newStr) 替换指定内容的字符串
  6. * replaceAll(String regx,String newStr) 支持正则表达式的字符串替换
  7. * isEmpty() 判断是否为null 长度为0 返回为true 否则false
  8. * @author WHD
  9. *
  10. */
  11. public class Test1 {
  12. public static void main(String[] args) {
  13. String str1 = "abcdefg";
  14. System.out.println(str1.charAt(3));
  15. char[] charArray = str1.toCharArray();
  16. for (int i = 0; i < charArray.length; i++) {
  17. System.out.println(charArray[i]);
  18. }
  19. String str2 = "abcdefabcdef";
  20. System.out.println(str2.replace("a", "A"));
  21. String str3 = "1a1b1c1d1e1f";
  22. System.out.println(str3.replace("1", "hello"));
  23. String str4 = "1a2b3c4d5e6f";
  24. System.out.println(str4.replaceAll("\\d", "Hello"));
  25. String str5 = " sasa we rt v ty ytr vfd d ";
  26. System.out.println(str5.replace(" ", ""));
  27. String str6 = " ewewq ewqewq ewqewqewq vdssfdsgfsd cfdfsdw rerrew ";
  28. System.out.println(str6.replaceAll("\\s", ""));
  29. // 如果出现空指针异常
  30. // 表示肯定访问了一个指向null(空的)的引用 调属性或者方法
  31. String str7 = new String();
  32. System.out.println(str7.length());
  33. System.out.println(str7.isEmpty());
  34. }
  35. }

2.String类和包装类相关面试题

==和equals有什么区别? String 类直接使用=号赋值 将先从常量池中查找有没有同内容的变量 如果有 则直接使用已存在内容的地址 如果没有 将此内容存在常量池 完成赋值 这样做的好处是为了节省内存空间

  1. package com.qfedu.test2;
  2. /**
  3. * ==和equals有什么区别?
  4. * String 类直接使用=号赋值 将先从常量池中查找有没有同内容的变量
  5. * 如果有 则直接使用已存在内容的地址
  6. * 如果没有 将此内容存在常量池 完成赋值
  7. *
  8. * 这样做的好处是为了节省内存空间
  9. * @author WHD
  10. *
  11. */
  12. public class Test1 {
  13. public static void main(String[] args) {
  14. String str1 = "abc";
  15. String str2 = "abc";
  16. String str3 = "abc";
  17. String str4 = new String("abc");
  18. System.out.println(str1.equals(str2));
  19. System.out.println(str1 == str2);
  20. System.out.println(str1 == str3);
  21. System.out.println(str1 == str4);
  22. }
  23. }

==和equals有什么区别? 整型包装类和char包装类直接使用=号赋值 取值范围在byte以内 数值相同 ==比较为true 超过byte取值范围 ==比较为false 因为JDK的开发人员为了节省内存空间 将byte取值范围内的数值存放在一个缓存数组中 如果在byte取值 范围 那么将从数组中取出对应的值 如果不在byte取值范围 直接new一个新的对象

  1. package com.qfedu.test2;
  2. /**
  3. * ==和equals有什么区别?
  4. * 整型包装类和char包装类直接使用=号赋值 取值范围在byte以内
  5. * 数值相同 ==比较为true
  6. * 超过byte取值范围 ==比较为false
  7. *
  8. * 因为JDK的开发人员为了节省内存空间 将byte取值范围内的数值存放在一个缓存数组中
  9. * 如果在byte取值 范围 那么将从数组中取出对应的值
  10. * 如果不在byte取值范围 直接new一个新的对象
  11. * @author WHD
  12. *
  13. */
  14. public class Test2 {
  15. public static void main(String[] args) {
  16. System.out.println("int-----------------------------------");
  17. Integer i1 = 25;
  18. Integer i2 = 25;
  19. System.out.println(i1 == i2);
  20. Integer i3 = 200;
  21. Integer i4 = 200;
  22. System.out.println(i3 == i4);
  23. Integer i5 = 127;
  24. Integer i6 = 127;
  25. System.out.println(i5 == i6);
  26. Integer i7 = 128;
  27. Integer i8 = 128;
  28. System.out.println(i7 == i8);
  29. System.out.println("byte----------------------------------");
  30. Byte b1 = 20;
  31. Byte b2 = 20;
  32. System.out.println(b1 == b2);
  33. System.out.println("short---------------------------------");
  34. Short s1 = 120;
  35. Short s2 = 120;
  36. System.out.println(s1 == s2);
  37. Short s3 = 200;
  38. Short s4 = 200;
  39. System.out.println(s3 == s4);
  40. System.out.println("long------------------------------------");
  41. Long l1 = 20L;
  42. Long l2 = 20L;
  43. System.out.println(l1 == l2);
  44. Long l3 = 300L;
  45. Long l4 = 300L;
  46. System.out.println(l3 == l4);
  47. System.out.println("char-----------------------------------------");
  48. Character ch1 = 20;
  49. Character ch2 = 20;
  50. System.out.println(ch1 == ch2);
  51. Character ch3 = 200;
  52. Character ch4 = 200;
  53. System.out.println(ch3 == ch4);
  54. }
  55. }

3.StringBuffer&StringBuilder

String类是一个不可变对象 因为String类底层维护的是一个final修饰的char数组 任何对原字符串进行的增删改操作 都将产生一个新的字符串 所以 当我们需要频繁改变一个字符串的内容 推荐使用StringBuffer或者StringBuilder

面试题: StringBuffer StringBuilder 区别?

StringBuffer是线程安全的 JDK1.0 StringBuilder线程不安全 JDK1.5

  1. package com.qfedu.test3;
  2. /**
  3. * String类是一个不可变对象 因为String类底层维护的是一个final修饰的char数组
  4. * 任何对原字符串进行的增删改操作 都将产生一个新的字符串
  5. * 所以 当我们需要频繁改变一个字符串的内容 推荐使用StringBuffer或者StringBuilder
  6. * 面试题:
  7. * StringBuffer StringBuilder 区别?
  8. * StringBuffer是线程安全的 JDK1.0
  9. * StringBuilder线程不安全 JDK1.5
  10. * @author WHD
  11. *
  12. */
  13. public class Test1 {
  14. public static void main(String[] args) {
  15. String str1 = "a";
  16. str1 += "b";
  17. System.out.println(str1);
  18. String condition = "笔记本";
  19. condition += "戴尔";
  20. condition += "至强铂金";
  21. condition += "32GB";
  22. condition += "1TB";
  23. condition += "2颗CPU";
  24. condition += 2;
  25. // ……
  26. System.out.println(condition);
  27. // StringBuffer sb = new StringBuffer();
  28. StringBuilder sb = new StringBuilder();
  29. sb.append(false);
  30. System.out.println(sb.length());
  31. sb.append("hello world");
  32. System.out.println(sb.length());
  33. System.out.println(sb.charAt(3));
  34. // sb = sb.delete(0, 5);
  35. System.out.println(sb.delete(0, 5));
  36. sb.insert(3, "666");
  37. System.out.println(sb);
  38. sb.replace(0, 3, "abcd");
  39. System.out.println(sb);
  40. sb.reverse();
  41. System.out.println(sb);
  42. }
  43. }

4.System类

System类 提供的有获取当前系统信息的一系列方法

  1. package com.qfedu.test4;
  2. /**
  3. * System类 提供的有获取当前系统信息的一些列方法
  4. * @author WHD
  5. *
  6. */
  7. public class TestSystem {
  8. public static void main(String[] args) {
  9. System.out.println("普通hello world");
  10. System.err.println("特殊hello world");
  11. // 返回1970年元月1日0点0分0秒到目前的毫秒数
  12. // 1秒 等于 1000 毫秒 等于 10亿纳秒
  13. System.out.println(System.currentTimeMillis() / 1000 / 60 / 60 / 24 / 365);
  14. System.out.println(System.getProperty("user.name"));
  15. System.out.println(System.getProperty("java.version"));
  16. System.out.println(System.getProperty("os.version"));
  17. System.out.println(System.getProperty("os.name"));
  18. System.getProperties().list(System.out);
  19. String str = "abc";
  20. // gc garbage collection
  21. System.gc(); // 此时调用垃圾回收器 但不是真正立即回收垃圾
  22. System.out.println(str);
  23. }
  24. }

5.Runtime类

Runtime类提供了用于获取程序运行期间信息的方法

  1. package com.qfedu.test4;
  2. import java.io.IOException;
  3. /**
  4. * Runtime类提供了用于获取程序运行期间信息的方法
  5. * @author WHD
  6. *
  7. */
  8. public class TestRuntime {
  9. public static void main(String[] args) throws IOException {
  10. System.out.println(Runtime.getRuntime().maxMemory() / 1024 / 1024 );
  11. System.out.println(Runtime.getRuntime().freeMemory() / 1024 / 1024);
  12. System.out.println(Runtime.getRuntime().totalMemory() / 1024 / 1024);
  13. Runtime.getRuntime().gc();
  14. Runtime.getRuntime().exec("D:\\apps\\Git-2.21.0-64-bit.exe");
  15. }
  16. }

6.java.util.Date类

java.util.Date 日期类

  1. package com.qfedu.test5;
  2. import java.text.SimpleDateFormat;
  3. import java.util.Date;
  4. /**
  5. * java.util.Date
  6. * 日期类
  7. * @author WHD
  8. *
  9. */
  10. public class TestDate {
  11. public static void main(String[] args) {
  12. Date date = new Date();
  13. System.out.println(date.getYear() + 1900 + "年");
  14. System.out.println(date.getMonth() + 1 + "月");
  15. System.out.println(date.getDay() + "一周第几天");
  16. System.out.println(date.getDate() + "一个月中第几天");
  17. System.out.println(date.getHours() + "小时");
  18. System.out.println(date.getMinutes() + "分钟");
  19. System.out.println(date.getSeconds() + "秒钟");
  20. System.out.println(date.toString());
  21. // yyyy/MM/dd HH:mm:ss 年月日 时分秒
  22. SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
  23. System.out.println(sdf.format(date));
  24. // 传入毫秒数获取日期对象
  25. Date date1 = new Date(System.currentTimeMillis());
  26. // 根据日期对象获取毫秒数
  27. System.out.println(date1.getTime());
  28. }
  29. }

7.Calendar类

Calendar类 日历类 也提供了获取年月日时分秒方法 Calendar类不能new对象

  1. package com.qfedu.test5;
  2. import java.util.Calendar;
  3. /**
  4. * Calendar类 日历类 也提供了获取年月日时分秒方法
  5. * Calendar类不能new对象
  6. * @author WHD
  7. *
  8. */
  9. public class TestCalendar {
  10. public static void main(String[] args) {
  11. Calendar instance = Calendar.getInstance();
  12. System.out.println(instance.get(Calendar.YEAR) + "年");
  13. System.out.println(instance.get(Calendar.MONTH) + 1 + "月");
  14. System.out.println(instance.get(Calendar.DAY_OF_MONTH) + "日");
  15. System.out.println(instance.get(Calendar.HOUR) + "时");
  16. System.out.println(instance.get(Calendar.HOUR_OF_DAY) + "时");
  17. System.out.println(instance.get(Calendar.MINUTE) + "分");
  18. System.out.println(instance.get(Calendar.SECOND) + "秒");
  19. }
  20. }