常用类

1. 枚举

枚举 enum修饰,用于规范某个数据的取值 枚举中默认都是全局静态常量的取值 直接写值,多个值使用逗号分割

  1. package com.qfedu.test1;
  2. public enum Week {
  3. MON,TUE,WED,THUR,FRI,SAT,SUN
  4. }
  1. package com.qfedu.test1;
  2. /**
  3. * 使用枚举作为switch条件打印一周的哪一天
  4. * @author WHD
  5. *
  6. */
  7. public class Test {
  8. public static void main(String[] args) {
  9. Week week = Week.MON;
  10. switch(week) {
  11. case MON:
  12. System.out.println("周一");
  13. break;
  14. case TUE:
  15. System.out.println("周二");
  16. break;
  17. case WED:
  18. System.out.println("周三");
  19. break;
  20. case THUR:
  21. System.out.println("周四");
  22. break;
  23. case FRI:
  24. System.out.println("周五");
  25. break;
  26. case SAT:
  27. System.out.println("周六");
  28. break;
  29. case SUN:
  30. System.out.println("周日");
  31. break;
  32. default:
  33. System.out.println("地球的一周只有七天");
  34. }
  35. printWeek(Week.SAT);
  36. }
  37. public static void printWeek(Week week) {
  38. switch(week) {
  39. case MON:
  40. System.out.println("周一");
  41. break;
  42. case TUE:
  43. System.out.println("周二");
  44. break;
  45. case WED:
  46. System.out.println("周三");
  47. break;
  48. case THUR:
  49. System.out.println("周四");
  50. break;
  51. case FRI:
  52. System.out.println("周五");
  53. break;
  54. case SAT:
  55. System.out.println("周六");
  56. break;
  57. case SUN:
  58. System.out.println("周日");
  59. break;
  60. default:
  61. System.out.println("地球的一周只有七天");
  62. }
  63. }
  64. }

2.包装类

2.1 构造方法

byte short int long float double boolean char Byte Short Integer Long Float Double Boolean Character 包装类构造方法 除了char包装类之外,其他的包装类都支持传入一个与之对应的基本数据类型构造当前实例 char包装类只支持传入char类型

  1. package com.qfedu.test2;
  2. /**
  3. * byte short int long float double boolean char
  4. * Byte Short Integer Long Float Double Boolean Character
  5. * 包装类构造方法
  6. * 除了char包装类之外,其他的包装类都支持传入一个与之对应的基本数据类型构造当前实例
  7. * char包装类只支持传入char类型
  8. * @author WHD
  9. *
  10. */
  11. public class Test1 {
  12. public static void main(String[] args) {
  13. byte b = 12;
  14. Byte b1 = new Byte(b);
  15. Byte b2 = new Byte("120");
  16. System.out.println(b1);
  17. System.out.println(b2);
  18. Short s1 = new Short((short)555);
  19. Short s2 = new Short("2356");
  20. Integer i1 = new Integer(2356);
  21. Integer i2 = new Integer("5623");
  22. Long l1 = new Long(5623568989745121L);
  23. Long l2 = new Long("5689");
  24. Float f1 = new Float(2.2);
  25. Float f2 = new Float(2.2F);
  26. Float f3 = new Float("2.2");
  27. Double d1 = new Double(20.3);
  28. Double d2 = new Double("20.3");
  29. Boolean bl1 = new Boolean("fAlse");
  30. System.out.println(bl1);
  31. Boolean bl2 = new Boolean("TrUe");
  32. System.out.println(bl2);
  33. Boolean bl3 = new Boolean("abcdefg");
  34. System.out.println(bl3);
  35. Boolean bl4 = new Boolean(true);
  36. Character ch1 = new Character('a');
  37. System.out.println(ch1);
  38. // A 65 a 97
  39. Character ch2 = new Character((char)66);
  40. System.out.println(ch2);
  41. }
  42. }

2.2 包装类-基本数据类型互相转换方法

xxxxValue() 将包装类转换为基本数据类型

  1. package com.qfedu.test3;
  2. /**
  3. * xxxxValue() 将包装类转换为基本数据类型
  4. *
  5. * @author WHD
  6. *
  7. */
  8. public class Test1 {
  9. @SuppressWarnings("unused")
  10. public static void main(String[] args) {
  11. Byte b2 = new Byte("120");
  12. byte b3 = b2.byteValue();
  13. Short s1 = new Short((short)555);
  14. short s2 = s1.shortValue();
  15. Integer i1 = new Integer(2356);
  16. int i2 = i1.intValue(); // shift + alt + l 自动生成对应类型接收
  17. Long l1 = new Long(5623568989745121L);
  18. long longValue = l1.longValue();
  19. Float f1 = new Float(2.2);
  20. float floatValue = f1.floatValue();
  21. Double d1 = new Double(20.3);
  22. double doubleValue = d1.doubleValue();
  23. Boolean bl1 = new Boolean("fAlse");
  24. boolean booleanValue = bl1.booleanValue();
  25. Character ch1 = new Character('a');
  26. char charValue = ch1.charValue();
  27. }
  28. }

valueOf() 将基本数据类型转换为包装类 除了Character类以外 其他的包装类 还支持传入一个String对象 转换类型

  1. package com.qfedu.test3;
  2. /**
  3. * valueOf() 将基本数据类型转换为包装类
  4. * 除了Character类以外 其他的包装类 还支持传入一个String对象 转换类型
  5. * @author WHD
  6. *
  7. */
  8. public class Test2 {
  9. public static void main(String[] args) {
  10. int a = 10;
  11. Integer i1 = Integer.valueOf(a);
  12. Integer i2 = Integer.valueOf("230");
  13. Byte b2 = Byte.valueOf((byte)12);
  14. Byte b3 = Byte.valueOf("62");
  15. Short s1 = Short.valueOf((short)231);
  16. Short s2 = Short.valueOf("5645");
  17. Long l1 = Long.valueOf(2356);
  18. Long l2 = Long.valueOf("8956");
  19. Float f1 = Float.valueOf(5623);
  20. Double d1 = Double.valueOf(230);
  21. Boolean bl1 = Boolean.valueOf(true);
  22. Character ch1 = Character.valueOf('a');
  23. }
  24. }

2.3 字符串与基本数据类型互相转换

toString方法 将基本数据类型转换为字符串

  1. package com.qfedu.test4;
  2. /**
  3. * toString方法 将基本数据类型转换为字符串
  4. * @author WHD
  5. *
  6. */
  7. public class Test1 {
  8. @SuppressWarnings("unused")
  9. public static void main(String[] args) {
  10. byte b1 = 20;
  11. String byteStr1 = b1 + "";
  12. String byteStr = Byte.toString((byte)12);
  13. System.out.println(byteStr.length());
  14. String shortStr = Short.toString((short)220);
  15. System.out.println(shortStr.length());
  16. String intStr = Integer.toString(20);
  17. System.out.println(intStr.length());
  18. String longStr = Long.toString(2233);
  19. String floatStr = Float.toString(2.3F);
  20. String doubleStr = Double.toString(220);
  21. String booleanStr = Boolean.toString(false);
  22. String charStr = Character.toString('a');
  23. }
  24. }

parseXXX() 除了char包装类 其他包装类都提供对应的将字符串转换为基本数据类型的方法

  1. package com.qfedu.test4;
  2. /**
  3. * parseXXX() 除了char包装类 其他包装类都提供对应的将字符串转换为基本数据类型的方法
  4. * @author WHD
  5. *
  6. */
  7. public class Test2 {
  8. public static void main(String[] args) {
  9. byte parseByte = Byte.parseByte("120");
  10. short parseShort = Short.parseShort("2233");
  11. int parseInt = Integer.parseInt("2356");
  12. long parseLong = Long.parseLong("5566");
  13. float parseFloat = Float.parseFloat("2255");
  14. double parseDouble = Double.parseDouble("74152");
  15. boolean parseBoolean = Boolean.parseBoolean("abcderAWEQD");
  16. }
  17. }

3. 拆箱和装箱

自动装箱和拆箱:JDK1.5以后允许包装类与基本数据类型混合运算 装箱:将基本数据类型转换为包装类 拆箱:将包装类转换为基本数据类型

  1. package com.qfedu.test4;
  2. /**
  3. * 自动装箱和拆箱:JDK1.5以后允许包装类与基本数据类型混合运算
  4. * 装箱:将基本数据类型转换为包装类
  5. * 拆箱:将包装类转换为基本数据类型
  6. * @author WHD
  7. *
  8. */
  9. public class Test3 {
  10. public static void main(String[] args) {
  11. Byte b1 = new Byte((byte)20);
  12. Byte b2 = new Byte((byte)30);
  13. System.out.println(b1.toString());
  14. System.out.println(b2);
  15. System.out.println(b1 + b2);
  16. Integer i = 20; // 自动装箱
  17. int a = i; // 自动拆箱
  18. System.out.println(i + a);
  19. }
  20. }

4. Math类

Math类 数学类 提供有常用的数学计算方法 和 两个静态常量 E PI

  1. package com.qfedu.test5;
  2. /**
  3. * Math类 数学类
  4. * 提供有常用的数学计算方法 和 两个静态常量 E PI
  5. * @author WHD
  6. *
  7. */
  8. public class Test1 {
  9. public static void main(String[] args) {
  10. System.out.println(Math.E);
  11. System.out.println(Math.PI);
  12. // 以下方法面试题
  13. System.out.println(Math.abs(-300));//绝对值
  14. System.out.println(Math.ceil(-3.3));//向上取整
  15. System.out.println(Math.floor(3.9));//向下取整
  16. System.out.println(Math.round(3.6));//四舍五入
  17. System.out.println(Math.round(35));
  18. System.out.println((int)(Math.random() * 13));//0-1之间的随机数
  19. }
  20. }

4. Random类

Random类 获取更多类型的随机数据

  1. package com.qfedu.test5;
  2. import java.util.Random;
  3. /**
  4. * Random类 获取更多类型的随机数据
  5. * @author WHD
  6. *
  7. */
  8. public class Test3 {
  9. public static void main(String[] args) {
  10. Random ran1 = new Random();
  11. System.out.println(ran1.nextBoolean());
  12. System.out.println(ran1.nextDouble());
  13. System.out.println(ran1.nextFloat());
  14. System.out.println(ran1.nextInt());
  15. System.out.println(ran1.nextInt(100));
  16. System.out.println(ran1.nextLong());
  17. // 不同的Random对象使用相同的种子 生成的随机数是相同的
  18. Random ran2 = new Random(1);
  19. Random ran3 = new Random(111);
  20. System.out.println(ran2.nextInt());
  21. System.out.println(ran3.nextInt());
  22. }
  23. }

5. String类

字符串类提供的有很多操作字符串的方法

比较 长度 转换 大小写 忽略大小写比较 查找字符串出现的位置 第一次 和最后一次 去除空格 拼接字符串

  1. package com.qfedu.test6;
  2. public class Test1 {
  3. public static void main(String[] args) {
  4. String str1 = "abcdefg";
  5. System.out.println(str1.length());
  6. String str2 = "abcdefg";
  7. System.out.println(str1.equals(str2));
  8. String str3 = "abcdEF";
  9. String str4 = "aBcDEF";
  10. System.out.println(str3.equals(str4));
  11. System.out.println(str3.equalsIgnoreCase(str4));
  12. String str5 = "abcdefg";
  13. System.out.println(str5.toUpperCase());
  14. System.out.println(str3.toLowerCase());
  15. String str6 = "abcdabcd";
  16. System.out.println(str6.indexOf("abc"));
  17. System.out.println(str6.indexOf("z"));
  18. System.out.println(str6.indexOf(98));
  19. String str7 = "中国";
  20. System.out.println(str7.indexOf(20013));
  21. System.out.println(str6.lastIndexOf("a"));
  22. System.out.println(str6.lastIndexOf(97));
  23. String str8 = "abcdefhijklmn";
  24. System.out.println(str8.substring(3));//只保留后边的
  25. System.out.println(str8.substring(3,6)); // 包前不包后
  26. String str9 = " dsajhdoqwererwq ds wqe qw ewqe wqe wqe wq ew ";
  27. System.out.println(str9.trim()); // 去除的首尾的空白
  28. String str10 = "a";
  29. String str11 = "b";
  30. System.out.println( str10 + str11);
  31. System.out.println(str10.concat(str11));
  32. }
  33. }

拆分字符串

  1. package com.qfedu.test6;
  2. /**
  3. * 拆分歌词
  4. * @author WHD
  5. *
  6. */
  7. public class Test2 {
  8. public static void main(String[] args) {
  9. String song = "长亭外 古道边 芳草碧连天 晚风拂柳 笛声残 夕阳山外山";
  10. String [] strs = song.split(" ");
  11. for (int i = 0; i < strs.length; i++) {
  12. System.out.println(strs[i]);
  13. }
  14. }
  15. }

判断字符串是否以某一个字符串开头或者结尾

  1. public static void main(String[] args) {
  2. String str1 = "abcdefg";
  3. System.out.println(str1.startsWith("f"));
  4. System.out.println(str1.endsWith("abcdefg"));
  5. }