1 String
---->String声明为final,不可被继承
---->String实现了Serializable接口:支持序列化
---->实现了Comparable接口,自然排序
---->内部定义了final char[] value用于存储字符串数据,,表示了不可变的字符序列。不可变性
---->通过字面量的而方式(String str = "string")赋值,此时,字符串生命在字符串常量池中。字符串常量池中不能存储内容相容的字符串
演示说明:1.当对字符串重新赋值时,需要重写指定内存区域赋值,不能使用原有的value进行赋值
2.当对现有的字符串进行连续操作时,也需要重新指定内存区域赋值,不能使用原来的
3.当调用String的repalce()方法修改字符串的属性时,也需要重新指定
---->String对象的创建
---->String str = "String";
---->String s2 = new String();
---->String s3 = new String(String original);
---->String s4 = new String(char[] a);
---->String s5 = new String(char[] a, int startIndex, int count);
---->几种不同拼接的对比
Strings1="jaca";
Strings2="bew";
Strings3="jacabew";
Strings4="jaca"+"bew"; //上面两种都可以看作是字面量方式,常量和常量的拼接结果在常量池,
Strings5= s1+"bew"; //只要右边存在变量,都是在堆空间中进行操作,都相当于是new
Strings6="jaca"+s2;
Strings7=s1+s2;
System.out.println(s3+":"+s4+":"+s5+":"+s6+":"+s7);
System.out.println(s3==s4);//true
System.out.println(s3==s5);//false
System.out.println(s3==s6);//false
System.out.println(s3==s7);//false
System.out.println(s5==s6);//false
Strings8=s5.intern();//此时的s8使用的是常量池中已经存在的"jacabew"声明的
System.out.println(s3==s8);//true
---->一道面试题
---->常用方法
//1.int length()
Strings1="helloworld";
System.out.println(s1.length());
//2.char charAt(int index)
charch=s1.charAt(1);
System.out.println(ch);
//3.boolean isEmpty()
System.out.println(s1.isEmpty());
//4.String toLowerCase()//使用默认语言环境,将String中的所有字符都转换为小写
//5.StringtoUpperCase()//用默认语言环境,将String中的所有字符都转换为大写
System.out.println(s1.toUpperCase());
System.out.println(s1);
//6.String trim()//返回字符串的副本,忽略前导空白和尾部空白
Strings2="helloworld";
System.out.println(s2.trim());//helloworld
//7.boolean equals(Object obj)//比较内容是否一致
//8.boolean equalsIgnoreCase(String anotherString)//与equals方法类似,忽略大小写
//9.String concat(String str)//将指定字符串连接到此字符串的结尾,等价于”+“
//10.int compareTo(String anotherString)//比较两个字符串的大小
//11.String subString(int beginIndex)//从beginIndex开始截取字符串
//12.String subString(intbeginIndex,intendIndex)//
//注意,此处是左闭右开
System.out.println(s1.substring(0,3));
//13.boolean endsWith(String suffix)//判断字符串是否以指定的后缀结束
Strings1="helloworld";
System.out.println(s1.endsWith("rld"));
//14.boolean startsWith(String prefix)//判断字符串是否以指定的前缀开始
System.out.println(s1.startsWith("hell"));
//15.boolean startsWith(Stringprefix,inttoffset)//判断字符串从指定索引开始的子字符串是否以prefix开始
System.out.println(s1.startsWith("ello",1));
//16.boolean contains(CharSequences)//当且仅当字符串包含指定的char值序列时,返回true
System.out.println(s1.contains("weowo"));//false
//17.int indexOf(Stringstr)//返回指定子字符串在此字符串中第一次出现的索引
//返回-1时,表示没有
System.out.println(s1.indexOf("llo"));//2
//18.int indexOf(Stringstr,intfromIndex)//返回指定字符串第一次出现的索引,从fromIndex开始找
System.out.println(s1.indexOf("wor",1));//5
//19.int lastIndexOf(Stringstr)//最右侧出现的索引
System.out.println(s1.lastIndexOf("l"));//8
//20.int lastIndexOf(Stringstr,intfromIndex)//返回指定字符串最右侧出现的索引,从fromIndex开始反向找
System.out.println(s1.lastIndexOf("l",3));//3
//22.String replace(CharSequence target,CharSequence replacement)//使用字面替换序列,替换此字符串中所有
//匹配的序列
2 String / char[]之间的转换
//toChatArray()
//new String(char[] ch)
String ----> byte[]: String str = "abc123"; byte[] bytes = str.getBytes(); sout(Arrays.toString(bytes);//[97,98,99,49,50,51]
byte[] ---->String: new String(byte[] )
3 StringBuffer / StringBuilder
---->三者异同
---->String:不可变的字符序列;底层使用fianl char[] value
---->StringBuffer:可变的字符序列,线程安全;底层使用char[] value
---->StringBuilder:可变的字符序列,JDK1.5,线程不安全;底层使用char[] value
---->三者的效率对比
---->源码分析
>String str = new String(); // new char[0]
String str1 = new String("abc"); // new char[]{'a', 'b', 'c'}
>StringBuffer sb1 = new StringBuffer(); //new char[16];
sb.append('A'); //value[0] = 'A'
sb.append('b'); //value[1] = 'B'
StringBuffer sb2 = new StringBuffer("abc"); //new char["abc".length() + 16]
---->问题一:sout(sb2.length()); //3
---->问题二:扩容为原来的两倍再加2,同时原来数组的元素复制到新的
注意:开发中,建议使用StringBuffer(int capacity)
---->常用方法
>StringBuffer append(xxx);
>StringBuffer delete(int start, int end);删除指定位置上的内容,左开右闭
>StringBuffer replace(int start, int end, String str); 把[start, end)位置上的替换成str
>StringBuffer insert(int offset, xxx);在指定位置插入xxx
>StringBuffer reverse();反转
>int indexOf(String str);
>String subString(int start, int end)
>int length();
>char charAt(int n);
>void setCharAt(int n, char ch);
注意:1 当append或者insert时,原来的 value数组长度不够了,可扩容
4 JDK8之前日期时间API
---->
//1.System.currentTimeMillis()//返回从1970.1.10时0秒到现在的时间差
//一般用于计算程序运行时间
longtime=System.currentTimeMillis();
System.out.println(time);
---->Date
//Date()
/*
java.util.Date
|--->java.sql.Date
1.两个构造器的使用
>Datedate=newDate();
>Datedate1=newDate(16175916160L);
2.两个方法的使用
>toString()显示当前,年月日时分秒
>getTime();返回从1970.1.10时0秒到现在的时间差
3. 如何将java.util.Date对象转化为java.sql.Date对象
*/
atedate=newDate();
System.out.println(date.toString());//MonApr0510:59:00CST2021
System.out.println(date.getTime());//1617591616068(ms)毫秒数
Datedate1=newDate(16175916160L);
System.out.println(date1);
---->java.text.SimpleDateFormat类
---->对Date类的格式化和解析
>格式化:日期 --->字符串
>解析:字符串 ---> 日期
---->实例化:
>
//SimpleDateFormat
//1.格式化:日期--->字符串
Datedate=newDate();
SimpleDateFormatformat=newSimpleDateFormat();
Stringstr=format.format(date);
System.out.println(str);
//2.解析:字符串--->日期(格式有要求)
Stringstr1="21-4-5下午2:58";
Datedate1=format.parse(str1);
System.out.println(date1);
//******************************
//一般用的格式:按照指定的方式
SimpleDateFormatsdf=newSimpleDateFormat("yyyy-MM-ddhh:mm:ss");
Stringformat1=sdf.format(date);
System.out.println(format1);
---->java.util.Calender类(日历)
---->抽象类
//抽象类,实例化1(GregorianCalendar)
//方式二,调用静态方法getInstance()
Calendarcalendar=Calendar.getInstance();
System.out.println(calendar.getClass());
//常用方法
//1.get()//可以获取一些常用的属性
intday=calendar.get(Calendar.DAY_OF_YEAR);
System.out.println(day);
//2.set()
calendar.set(Calendar.DAY_OF_YEAR,68);
System.out.println(calendar.get(Calendar.DAY_OF_YEAR));
//3.add()
//4.getTime():日历类--->和Date之间的转换
Datetime=calendar.getTime();
System.out.println(time);
//5.setTime():Date--->Calendar
---->JDK8中的时间类
---->LocalDate
---->LocalTime
---->LocalDateTime
5 比较器
---->Comparable接口
>像String、包装类等实现了Comparable接口,重写了compareTo方法,进行了从小到大的排列
class A implements Comparable{
@Override
public int compareTo(Object anotherObj){
if(o instanceof Good){
Good good = (Good)o;
}
}
---->Comparator接口
>当元素没有实现Comparable接口,而又不方便修改代码,或者实现了Comparable接口的排序规则不适合,
String[]str=newString[]{"AA","CC","FF","BB"};
Comparator<String>com=newComparator<String>(){
@Override
publicintcompare(Stringo1,Stringo2){
return0;
}
};
Arrays.sort(str,com);
6 System类
---->成员方法
>native long currentTimeMillis():
>void exit(int status):退出程序,status为0表示正常退出,非0表示异常退出
7 Math
---->
8 BigInteger / BigDecimal