常用类
1. 枚举
枚举 enum修饰,用于规范某个数据的取值 枚举中默认都是全局静态常量的取值 直接写值,多个值使用逗号分割
package com.qfedu.test1;public enum Week {MON,TUE,WED,THUR,FRI,SAT,SUN}
package com.qfedu.test1;/*** 使用枚举作为switch条件打印一周的哪一天* @author WHD**/public class Test {public static void main(String[] args) {Week week = Week.MON;switch(week) {case MON:System.out.println("周一");break;case TUE:System.out.println("周二");break;case WED:System.out.println("周三");break;case THUR:System.out.println("周四");break;case FRI:System.out.println("周五");break;case SAT:System.out.println("周六");break;case SUN:System.out.println("周日");break;default:System.out.println("地球的一周只有七天");}printWeek(Week.SAT);}public static void printWeek(Week week) {switch(week) {case MON:System.out.println("周一");break;case TUE:System.out.println("周二");break;case WED:System.out.println("周三");break;case THUR:System.out.println("周四");break;case FRI:System.out.println("周五");break;case SAT:System.out.println("周六");break;case SUN:System.out.println("周日");break;default:System.out.println("地球的一周只有七天");}}}
2.包装类
2.1 构造方法
byte short int long float double boolean char Byte Short Integer Long Float Double Boolean Character 包装类构造方法 除了char包装类之外,其他的包装类都支持传入一个与之对应的基本数据类型构造当前实例 char包装类只支持传入char类型
package com.qfedu.test2;/*** byte short int long float double boolean char* Byte Short Integer Long Float Double Boolean Character* 包装类构造方法* 除了char包装类之外,其他的包装类都支持传入一个与之对应的基本数据类型构造当前实例* char包装类只支持传入char类型* @author WHD**/public class Test1 {public static void main(String[] args) {byte b = 12;Byte b1 = new Byte(b);Byte b2 = new Byte("120");System.out.println(b1);System.out.println(b2);Short s1 = new Short((short)555);Short s2 = new Short("2356");Integer i1 = new Integer(2356);Integer i2 = new Integer("5623");Long l1 = new Long(5623568989745121L);Long l2 = new Long("5689");Float f1 = new Float(2.2);Float f2 = new Float(2.2F);Float f3 = new Float("2.2");Double d1 = new Double(20.3);Double d2 = new Double("20.3");Boolean bl1 = new Boolean("fAlse");System.out.println(bl1);Boolean bl2 = new Boolean("TrUe");System.out.println(bl2);Boolean bl3 = new Boolean("abcdefg");System.out.println(bl3);Boolean bl4 = new Boolean(true);Character ch1 = new Character('a');System.out.println(ch1);// A 65 a 97Character ch2 = new Character((char)66);System.out.println(ch2);}}
2.2 包装类-基本数据类型互相转换方法
xxxxValue() 将包装类转换为基本数据类型
package com.qfedu.test3;/*** xxxxValue() 将包装类转换为基本数据类型** @author WHD**/public class Test1 {@SuppressWarnings("unused")public static void main(String[] args) {Byte b2 = new Byte("120");byte b3 = b2.byteValue();Short s1 = new Short((short)555);short s2 = s1.shortValue();Integer i1 = new Integer(2356);int i2 = i1.intValue(); // shift + alt + l 自动生成对应类型接收Long l1 = new Long(5623568989745121L);long longValue = l1.longValue();Float f1 = new Float(2.2);float floatValue = f1.floatValue();Double d1 = new Double(20.3);double doubleValue = d1.doubleValue();Boolean bl1 = new Boolean("fAlse");boolean booleanValue = bl1.booleanValue();Character ch1 = new Character('a');char charValue = ch1.charValue();}}
valueOf() 将基本数据类型转换为包装类 除了Character类以外 其他的包装类 还支持传入一个String对象 转换类型
package com.qfedu.test3;/*** valueOf() 将基本数据类型转换为包装类* 除了Character类以外 其他的包装类 还支持传入一个String对象 转换类型* @author WHD**/public class Test2 {public static void main(String[] args) {int a = 10;Integer i1 = Integer.valueOf(a);Integer i2 = Integer.valueOf("230");Byte b2 = Byte.valueOf((byte)12);Byte b3 = Byte.valueOf("62");Short s1 = Short.valueOf((short)231);Short s2 = Short.valueOf("5645");Long l1 = Long.valueOf(2356);Long l2 = Long.valueOf("8956");Float f1 = Float.valueOf(5623);Double d1 = Double.valueOf(230);Boolean bl1 = Boolean.valueOf(true);Character ch1 = Character.valueOf('a');}}
2.3 字符串与基本数据类型互相转换
toString方法 将基本数据类型转换为字符串
package com.qfedu.test4;/*** toString方法 将基本数据类型转换为字符串* @author WHD**/public class Test1 {@SuppressWarnings("unused")public static void main(String[] args) {byte b1 = 20;String byteStr1 = b1 + "";String byteStr = Byte.toString((byte)12);System.out.println(byteStr.length());String shortStr = Short.toString((short)220);System.out.println(shortStr.length());String intStr = Integer.toString(20);System.out.println(intStr.length());String longStr = Long.toString(2233);String floatStr = Float.toString(2.3F);String doubleStr = Double.toString(220);String booleanStr = Boolean.toString(false);String charStr = Character.toString('a');}}
parseXXX() 除了char包装类 其他包装类都提供对应的将字符串转换为基本数据类型的方法
package com.qfedu.test4;/*** parseXXX() 除了char包装类 其他包装类都提供对应的将字符串转换为基本数据类型的方法* @author WHD**/public class Test2 {public static void main(String[] args) {byte parseByte = Byte.parseByte("120");short parseShort = Short.parseShort("2233");int parseInt = Integer.parseInt("2356");long parseLong = Long.parseLong("5566");float parseFloat = Float.parseFloat("2255");double parseDouble = Double.parseDouble("74152");boolean parseBoolean = Boolean.parseBoolean("abcderAWEQD");}}
3. 拆箱和装箱
自动装箱和拆箱:JDK1.5以后允许包装类与基本数据类型混合运算 装箱:将基本数据类型转换为包装类 拆箱:将包装类转换为基本数据类型
package com.qfedu.test4;/*** 自动装箱和拆箱:JDK1.5以后允许包装类与基本数据类型混合运算* 装箱:将基本数据类型转换为包装类* 拆箱:将包装类转换为基本数据类型* @author WHD**/public class Test3 {public static void main(String[] args) {Byte b1 = new Byte((byte)20);Byte b2 = new Byte((byte)30);System.out.println(b1.toString());System.out.println(b2);System.out.println(b1 + b2);Integer i = 20; // 自动装箱int a = i; // 自动拆箱System.out.println(i + a);}}
4. Math类
Math类 数学类 提供有常用的数学计算方法 和 两个静态常量 E PI
package com.qfedu.test5;/*** Math类 数学类* 提供有常用的数学计算方法 和 两个静态常量 E PI* @author WHD**/public class Test1 {public static void main(String[] args) {System.out.println(Math.E);System.out.println(Math.PI);// 以下方法面试题System.out.println(Math.abs(-300));//绝对值System.out.println(Math.ceil(-3.3));//向上取整System.out.println(Math.floor(3.9));//向下取整System.out.println(Math.round(3.6));//四舍五入System.out.println(Math.round(35));System.out.println((int)(Math.random() * 13));//0-1之间的随机数}}
4. Random类
Random类 获取更多类型的随机数据
package com.qfedu.test5;import java.util.Random;/*** Random类 获取更多类型的随机数据* @author WHD**/public class Test3 {public static void main(String[] args) {Random ran1 = new Random();System.out.println(ran1.nextBoolean());System.out.println(ran1.nextDouble());System.out.println(ran1.nextFloat());System.out.println(ran1.nextInt());System.out.println(ran1.nextInt(100));System.out.println(ran1.nextLong());// 不同的Random对象使用相同的种子 生成的随机数是相同的Random ran2 = new Random(1);Random ran3 = new Random(111);System.out.println(ran2.nextInt());System.out.println(ran3.nextInt());}}
5. String类
字符串类提供的有很多操作字符串的方法
比较 长度 转换 大小写 忽略大小写比较 查找字符串出现的位置 第一次 和最后一次 去除空格 拼接字符串
package com.qfedu.test6;public class Test1 {public static void main(String[] args) {String str1 = "abcdefg";System.out.println(str1.length());String str2 = "abcdefg";System.out.println(str1.equals(str2));String str3 = "abcdEF";String str4 = "aBcDEF";System.out.println(str3.equals(str4));System.out.println(str3.equalsIgnoreCase(str4));String str5 = "abcdefg";System.out.println(str5.toUpperCase());System.out.println(str3.toLowerCase());String str6 = "abcdabcd";System.out.println(str6.indexOf("abc"));System.out.println(str6.indexOf("z"));System.out.println(str6.indexOf(98));String str7 = "中国";System.out.println(str7.indexOf(20013));System.out.println(str6.lastIndexOf("a"));System.out.println(str6.lastIndexOf(97));String str8 = "abcdefhijklmn";System.out.println(str8.substring(3));//只保留后边的System.out.println(str8.substring(3,6)); // 包前不包后String str9 = " dsajhdoqwererwq ds wqe qw ewqe wqe wqe wq ew ";System.out.println(str9.trim()); // 去除的首尾的空白String str10 = "a";String str11 = "b";System.out.println( str10 + str11);System.out.println(str10.concat(str11));}}
拆分字符串
package com.qfedu.test6;/*** 拆分歌词* @author WHD**/public class Test2 {public static void main(String[] args) {String song = "长亭外 古道边 芳草碧连天 晚风拂柳 笛声残 夕阳山外山";String [] strs = song.split(" ");for (int i = 0; i < strs.length; i++) {System.out.println(strs[i]);}}}
判断字符串是否以某一个字符串开头或者结尾
public static void main(String[] args) {String str1 = "abcdefg";System.out.println(str1.startsWith("f"));System.out.println(str1.endsWith("abcdefg"));}
