day 13
一、枚举
理解:
实现:1. 不需要提高Set方法,因为枚举对象通常为只读
2.对枚举对象、属性使用final + static 共同修饰,实现底层优化。
3.枚举对象名通常使用全部大写,常量的命名规范。
4.枚举根据对象需要,也可以有多个属性。
class Seanson{private String name;//季节名称private String description;//季节描述public String getName(){return name;}public String gerDescription(){return description;}private Season(String name,String description){this.name = name;this.description = description;}public static final Season SPRING = new Season("春天","温度");public static final Season SUMMER= new Season("夏天","温度");public static final Season AUTUMN= new Season("秋天","温度");public static final Season WINITER = new Season("冬天","温度"); }
枚举实现:
1.构造器私有化
在类中创建一组对象[四个春夏秋冬]
对外露对象(通过为对象添加public final static 修饰符)
提共get 方法但不提供set 方法(外部只能读)
使用enum 来实现枚举类:
使用关键字 enum 替代class
如:public static final Season SPRING = new Season("春天","温暖")直接使用 SPRING ("春天","温暖")
如果有多个常量对象,使用逗号隔开最后一个用分号。
如果使用enum 来实现枚举类,要求将将定义常量对象写在最前面
enum Season2{SPRING ("春天","温暖"),SUMER("夏天","炎热")....; }
enum关键字实现枚举注意事项:
当使用enum 关键字开发枚举类时,默认会继承Enume类
如果使用无参构造器创建枚举对象,则实参列表和小括号可以省略
enum Gender{BOY,GIRL; }
二、包装类(重点):
知识点:包装类的使用 含义:8种基本数据类型对应的类 出现原因: Java为纯面向对象语言(万物皆对象),但是8种基本数据类型不能new对象, 就破坏了Java为纯面向对象语言的特征,所以Java又为8种基本数据类型匹配了对应的类,这种类叫做包装类/封装类
基本数据类型 | 包装类 | 继承类 |
---|---|---|
byte | Byte | Object.Number.Byte |
short | Short | Object.Number.Short |
int | Integer | Object.Number.Integer |
long | Long | Object.Number.Long |
folat | Float | Object.Number.Float |
double | Double | Object.Number.Double |
char | Character | Object.Character |
boolean | boolean | Object.Boolean |
注意:int类型的包装类为Integer,char类型的包装类为Character,其余的包装类都是基本数据类型的首字母大写
1.integer 底层实现:
//该类MyInteger类缓存类private static class MyIntegerCache{public static final int low = -128;public static final int high = 127;public static final MyInteger[] cache;//存储MyInterger对象的缓存类数据static{cache = new MyInteger[high -low +1];int j = low;for(int i = 0;i<cache.length;i++){cache[i] = new MyInteger(j++);}}}
package com.day13.test; /*** * */ public class Test01 {public static void main(String[] args){//手动装箱 基本数据类型转换成对象int i = 100;Integer integer= Integer.valueOf(i);System.out.println(integer);//手动拆箱Integer integer1 = new Integer(100);int i1 = integer1.intValue();System.out.println(i1);//自动装箱int i = 100;Integer integer = i;System.out.println(integer);//自动拆箱Integer integer = new Integer(100);int i = integer;System.out.println(i);//拓展:将string 转换成int String str = "123";int num = Integer.parseInt(str);System.out.println(num);} }
//重点:package com.day13.test; public class MyInteger {private final int value;public MyInteger(int value){this.value = value;}public static MyInteger valueOf(int i ){if(i >= MyIntegerCache.low && i <= MyIntegerCache.high){return MyIntegerCache.cache[i -MyIntegerCache.low];}return new MyInteger(i);}public String toString(){return String.valueOf(value);}public int intValue(){return value;}//该类MyInteger类缓存类private static class MyIntegerCache{public static final int low = -128;public static final int high = 127;public static final MyInteger[] cache;//存储MyInterger对象的缓存类数据static{cache = new MyInteger[high -low +1];int j = low;for(int i = 0;i<cache.length;i++){cache[i] = new MyInteger(j++);}}} }
测试类:
package com.day13.test; /*** 包装类深入* * * */ public class Test02 {public static void main(String[] args) {//封箱Integer integer1 = Integer.valueOf(100);Integer integer2 = Integer.valueOf(100);System.out.println(integer1 == integer2);//true Integer integer3= Integer.valueOf(200);Integer integer4 = Integer.valueOf(300);System.out.println(integer3 == integer4);//falseSystem.out.println(integer1);System.out.println(integer2);System.out.println(integer3);System.out.println(integer4);System.out.println("===============================integer底层如下");MyInteger myInteger1 = MyInteger.valueOf(100);MyInteger myInteger2 = MyInteger.valueOf(100);System.out.println(myInteger1 == myInteger2); MyInteger myInteger3 = MyInteger.valueOf(200);MyInteger myInteger4 = MyInteger.valueOf(300);System.out.println(myInteger3 == myInteger4); //打印对象System.out.println(myInteger1);System.out.println(myInteger2);System.out.println(myInteger3);System.out.println(myInteger4);//打印基本数据类型integerSystem.out.println(myInteger1.intValue());System.out.println(myInteger2.intValue());System.out.println(myInteger3.intValue());System.out.println(myInteger4.intValue()); } }
面试题
MyInteger myInteger1 = MyInteger.valueOf(100);MyInteger myInteger2 = MyInteger.valueOf(100);System.out.println(myInteger1 == myInteger2); //ture MyInteger myInteger3 = MyInteger.valueOf(200);MyInteger myInteger4 = MyInteger.valueOf(300);System.out.println(myInteger3 == myInteger4); //fase
ture因为 myInteger1 == myInteger2 是相同对象指针指向的是同一个地址
false因为integer 的取值范围在-128~127之间,200和300超出了integer 取值范围,myInteger3 == myInteger4,属于两个不同的对象指向不同的地址所有两个地址不同值也不同。
三、常用类:
字符串类:
String
StringBuffer 安全的,因为它会对线程上锁,确保了信息私密性。
StringBuilder 线程不安全 。
底层:
String -> char[]
面试题:
StringBuilder:线程不安全,在单线程的情况下使用 StringBuffer:因为方法上加锁(synchronized),线程安全,在多线程的情况下使用。
注意:
StringBuffer和StringBuilder使用是一样的
1. String类使用:
知识点:字符串类 分类: String StringBuffer StringBuilder
底层:
public class String{//常量类型的字符数组final char[] value; //不可扩容}//StringBuilder和StringBuffer都是可变的字符串public class AbstractStringBuilder{char[] value; //可扩容}public class StringBuffer extends AbstractStringBuilder{}public class StringBuilder extends AbstractStringBuilder{}
str.concat("DEF123");//在末尾追加字符串,并返回新的 str.substring(2);//从指定下标处截取到字符串末尾,并返回新的字符串 str.substring(1, 7);//从开始下标处(包含)截取到结束下标处(排他),并返回新的字符串 str.toLowerCase();//转小写,并返回新的字符串 str.toUpperCase();//转大写,并返回新的字符串 str.trim();//去除首尾空格,并返回新的字符串 str.replace('c', 'C');//替换字符,并返回新的字符串 str.replaceFirst("12", "xx");//替换第一次出现的字符串,并返回新的字符串str.replaceAll("3", "888");//替换字符串,并返回新的字符串str.replaceAll(" ", "");//去除空格str.indexOf("888") //获取字符串第一次出现的下标str.lastIndexOf("888")//获取字符串最后一次出现的下标str.charAt(7)// 获取指定下标上的字符
package com.ady.string_class; import java.util.Arrays; public class Test01 { public static void main(String[] args) {String str = "123abc";str = str.concat("DEF123");//在末尾追加字符串,并返回新的字符串str = str.substring(2);//从指定下标处截取到字符串末尾,并返回新的字符串str = str.substring(1, 7);//从开始下标处(包含)截取到结束下标处(排他),并返回新的字符串str = str.toLowerCase();//转小写,并返回新的字符串str = str.toUpperCase();//转大写,并返回新的字符串str = " 123a bcD EF12 3 ";str = str.trim();//去除首尾空格,并返回新的字符串str = str.replace('c', 'C');//替换字符,并返回新的字符串str = str.replaceFirst("12", "xx");//替换第一次出现的字符串,并返回新的字符串str = str.replaceAll("3", "888");//替换字符串,并返回新的字符串str = str.replaceAll(" ", "");//去除空格System.out.println("获取字符串第一次出现的下标:" + str.indexOf("888"));//2System.out.println("获取字符串最后一次出现的下标:" + str.lastIndexOf("888"));//13System.out.println("获取指定下标上的字符:" + str.charAt(7));//CSystem.out.println("判断两个字符串是否相同(区分大小写):" + str.equals("xx888abCDEF12888"));//trueSystem.out.println("判断两个字符串是否相同(不区分大小写):" + str.equalsIgnoreCase("xx888ABCdef12888"));//trueSystem.out.println("判断字符串是否以某个字符串开头:" + str.startsWith("xx8"));//trueSystem.out.println("判断字符串是否以某个字符串结尾:" + str.endsWith("12888"));//true//将字符串转换为字符数组char[] charArray = str.toCharArray();System.out.println(Arrays.toString(charArray));//[x, x, 8, 8, 8, a, b, C, D, E, F, 1, 2, 8, 8, 8]//将其他类型转换为字符串System.out.println(String.valueOf(123));//int -> StringSystem.out.println(String.valueOf(123.123));//double -> StringSystem.out.println(String.valueOf('A'));//char -> StringSystem.out.println(String.valueOf(true));//boolean -> String//将其他类型转换为字符串 -- 简单版本System.out.println(123 + "");System.out.println(123.123 + "");System.out.println('A' + "");System.out.println(true + "");} }
2. StringBuffer的使用
package com.ady.string_class; public class Test02 {/*** 知识点:StringBuffer的使用*/public static void main(String[] args) {StringBuffer sb = new StringBuffer();//将字符串追加到末尾sb.append("123abc");sb.append("DEF123");//将字符串插入到指定下标的位置sb.insert(6, "XXX");//从开始下标处(包含)替换到结束下标处(排他)的字符串sb.replace(6, 9, "XYl");//删除指定下标处的字符sb.deleteCharAt(5);//从开始下标处(包含)删除到结束下标处(排他)的字符串sb.delete(3, 11);//反转字符串sb.reverse();System.out.println(sb);} }
3.StringBuilder的使用
package com.qf.string_class; public class Test03 {/*** 知识点:StringBuilder的使用*/public static void main(String[] args) {StringBuilder sb = new StringBuilder();//将字符串追加到末尾sb.append("123abc");sb.append("DEF123");//将字符串插入到指定下标的位置sb.insert(6, "XXX");//从开始下标处(包含)替换到结束下标处(排他)的字符串sb.replace(6, 9, "XYl");//删除指定下标处的字符sb.deleteCharAt(5);//从开始下标处(包含)删除到结束下标处(排他)的字符串sb.delete(3, 11);//反转字符串sb.reverse();System.out.println(sb); }
四、String 类深入
1.知识点:深入String -- 创建对象
package com.ady.string_class; public class Test06 {/*** 知识点:深入String -- 字符串频繁拼接问题* * 注意:字符串频繁拼接使用StringBuilder 或 StringBuffer*/public static void main(String[] args) {//获取1970.1.1 0:0:0到现在的毫秒数(1000毫秒=1秒)//long currentTimeMillis = System.currentTimeMillis();// long startTime = System.currentTimeMillis(); // String str = "琉璃"; // for (int i = 0; i < 100000; i++) { // str += "皇冠给你带";//底层实现:// str = str + "小可爱,皇冠给你带"; // str = new StringBuilder(String.valueOf(str)).append("小可爱,皇冠给你带").toString(); // } // long endTime = System.currentTimeMillis(); // System.out.println("消耗时长:" + (endTime-startTime));long startTime = System.currentTimeMillis();StringBuilder sb = new StringBuilder(900000);//减少底层容器的伸缩性(减少底层容器的扩容次数)sb.append("蒋羽");for (int i = 0; i < 100000; i++) {sb.append("小可爱,皇冠给你带");}long endTime = System.currentTimeMillis();System.out.println("消耗时长:" + (endTime-startTime));//16} }
2. 深入StringBuilder 和 StringBuffer
知识点:深入StringBuilder 和 StringBuffer的底层原理
知识点:深入StringBuilder 和 StringBuffer的底层原理
StringBuilder:线程不安全,在单线程的情况下使用
StringBuffer:因为方法上加锁(synchronized),线程安全,在多线程的情况下使用
package com.ady.string_class; public class Test07 {public static void main(String[] args) {//底层默认创建16个长度字符数组//StringBuilder sb = new StringBuilder();//底层创建10000个长度字符数组//StringBuilder sb = new StringBuilder(10000);//底层创建str.length() + 16 个长度字符数组//StringBuilder sb = new StringBuilder("琉璃");//sb.append("aaa");//sb.append("bbb");//底层默认创建16个长度字符数组//StringBuffer sb = new StringBuffer();//底层创建10000个长度字符数组//StringBuffer sb = new StringBuffer(10000);//底层创建str.length() + 16 个长度字符数组StringBuffer sb = new StringBuffer("琉璃");sb.append("aaa");sb.append("bbb");} }
3.String vs StringBuffer vs StringBuilder
知识点:String vs StringBuffer vs StringBuilder
package com.ady.string_class; public class Test08 {/*** 知识点:String vs StringBuffer vs StringBuilder* * String是不可变的字符串* public class String{* final char[] value;//不可扩容* }* * StringBuffer和StringBuilder是可变的字符串* * public class AbstractStringBuilder{* char[] value;//可扩容* }* * public class StringBuffer extends AbstractStringBuilder{}* * public class StringBuilder extends AbstractStringBuilder{}* */ }
面试题:
package com.ady.string_class; public class Test04 {/*** 知识点:深入String -- 创建对象*/public static void main(String[] args) {//面试题1:描述下列代码创建多个少String对象?//答案:1个,“abc”作为字面值常量,是放在常量池中,常量池中的数据不允许重复 // String str1 = "abc";//0x11 // String str2 = "abc";//0x11 // System.out.println(str1 == str2);//true //面试题2:描述下列代码创建多个少String对象?//答案:3个,"abc"+new出来的两个String对象String str1 = new String("abc");String str2 = new String("abc");System.out.println(str1 == str2);//false} }
package com.day13.Test01; public class Test02 {public static void main(String[] args) {String str1 = "abc";String str2 = "abc";System.out.println(str1 == str2);//tureString str5 = "abc";String str6 = "abc";System.out.println(str5 == str6);//tureString str3 = "ab"+"b";String str4 = "abc";System.out.println(str3 == str4); //falseString str7 = "ab"+"c"; //在编译的时候就已经拼接完了System.out.println(str7 == str1);//turefinal String s1 = "ab";final String s2 = "c"; //在常量池的时候就已经拼接了System.out.println(s1 == str4);String s3 = "ab";String s4 = "c";String str12 = s3 +s4; //底层:new StringBuidler(String.valueOf().appdend(s4).toString();)System.out.println(str12 == str5);//false} }
package com.day13.Test01; /**字符串拼接问题 */ public class Test01 {public static void main(String[] args) {String str1 = "abc";String str2 = "abc";System.out.println(str1 == str2); //ture原因是str1和str2 都是同一个数组空间,指向的是同一地址String str3 = new String("abc");String str4 = new String("abc");System.out.println(str3 == str4); //false 原因是str3 和 str4 都新new 对象各自开辟的内存空间,不属于同以地址的不同对象所以它俩不同。} }
知识点:深入String -- 字符串频繁拼接问题package com.day13.Test01;public class Test04 {public static void main(String[] args) {//底层默认创建16个长度字符数组//StringBuilder sb = new StringBuilder();//底层创建1000个长度字符数组//StringBuilder sb= new StringBuilder(1000);//底层创建str.length() + 16个长度字符数组//StringBuilder sb = new StringBuilder("莉莉");//sb.append("aaa");//sb.append("bbb");//底层默认创建16个长度字符数组//StringBuffer sb = new StringBuffer();//底层创建10000个长度字符数组//StringBuffer sb = new StringBuffer(10000);//底层创建str.length() + 16 个长度字符数组StringBuffer sb = new StringBuffer("莉莉");sb.append("aaa");sb.append("bbb");System.out.println(sb);} }