关键知识点和补充
- 学会自己用API 看构造器,方法,方法的参数,方法的返回值
- 字符串在内存中是以字符数组的形式存储的
- String是类,"aaa"这种就是类的实例,直接写存储在方法区的字符串常量池中,但是new出来的存放在堆中
- String s = new String ("abc") 在方法区中创建了字符串字面常量(只有一份),堆中也创了一个对象,将方法区中的地址传递给堆
- 常用的方法:
- String s = new Srting("abc"); s.toString() 返回内容不返回地址
- 看例题中的几种转化!!!
- //注意会将字符串和可变长的字符串的转化,这样可以使用对方的功能
//将可变长的StringBuilder变为字符串
System.out.println(sb2.toString());
//将不可变长度的字符串变为可变长的StringBuilder
StringBuilder AA = new StringBuilder("ABCD"); - * StringBuilder: 线程不安全的,相对效率高
* StringBuffer : 线程安全的,相对效率低 - 一般我们都用StringBuilder,因为效率高
- StringBuilder|StringBuffer 一般默认的capacity的容量为16,超过这个长度会自动扩容,扩容的长度源码中是<<1+2
- 对于capacity而言,"abc".capacity的长度是19,因为他是可变长的,预留了点位置给append()
- "abc".length()打印出来的长度是3
- 反转是reverse()
- =======================================================================================
- 自动装箱和自动拆箱时候 Integer in1=i; //自动装箱 Integer.valueOf(i); int i2=in1; //自动拆箱 in1.intValue()
- * 1.基本数据类型与对象的包装类型的数据比较,只要值相同,无论是否new都相等,因为会发生自动拆箱
* 2.两个new肯定不相等,因为两个地址
* 3.Integer和new Integer肯定不相等,因为一个是在堆中
* 4.如果两个Integer对象比较,在其范围内 -128~127之间就相等,缓冲区对象,否则返回new - 基本数据类型和常量池 干货: https://blog.csdn.net/weixin_40018934/article/details/81088049
- * enum 定义枚举
* 只要是枚举类都会隐式的继承自java.lang.包下的enum
* 枚举类中的成员,都是该类的一个实例,默认修饰符为public static final - 枚举类的格式:enum Busy{FREE,BUSY,VERY_BUSY} 用类名.属性调用
String,Stringbuffer,StringBuilder三者区别https://blog.csdn.net/qq_37856300/article/details/84340288
常用类: 常用到的类
* String 类代表字符串。Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现
* "haha"-->['h','a','h','a']
*
* String : 不可变长的字符序列
* StringBuilder :可变长的字符序列
* StringBuffer :可变长的字符序列
基本数据类型的包装类
* 基本数据类型----> 引用数据类型
* byte Byte
* short Short
* int Integer
* long Long
* float Float
* double Double
* boolean Boolean
* char Character
* 自动拆装箱
* 自动装箱:从基本数据类型到引用数据类型
* 自动拆箱:从包装类型到基本数据类型
Date 日期类
类 Date 表示特定的瞬间,精确到毫秒
* 日期格式转换类 SimpleDateFormat
* 字符串转为日期对象
* Date parse("字符串") 字符串转为日期对象
* 日期对象转为字符串
* String format(date) 日期对 象转为字符串
*
* SimpleDateFormat() 空构造 以默认格式进行转换
*
* y 年
* M 月
* d 日
*
* H 时(24小時制度) h 12小时制度的
* m 分
* s 秒
* S 毫秒
*
* E 星期几
枚举类
* 枚举类: 一种事物的所有可能,所有情况
* enum 定义枚举
* 只要是枚举类都会隐式的继承自java.lang.包下的enum
* 枚举类中的成员,都是该类的一个实例,默认修饰符为public static final
例题
字符串
public class StringDemo01 {
public static void main(String[] args) throws UnsupportedEncodingException {
String str=new String("abc");
String str2="abc"; //重新赋值
System.out.println(str==str2);
System.out.println(str.equals(str2));
//String()
String s1=new String();
System.out.println(s1.toString());
System.out.println(s1.length()); //字符串中的字符个数
/*
字符串和字节数组的相互转换
*/
String name="因为"; //字符串对象 "字符串字面常量值"
byte[] bs=name.getBytes(); //默认utf-8字符编码格式
System.out.println(Arrays.toString(bs));
//String(byte[] bytes) 根据参数字节数组,构建字符串
String s2=new String(bs);
//String(byte[] bytes, int offset, int length)
String s3=new String(bs,3,3);
System.out.println(s3);
/*
* String(byte[] bytes, int offset, int length, String charsetName)
通过使用指定的字符集解码指定的 byte 子数组,构造一个新的 String。
String(byte[] bytes, String charsetName)
通过使用指定的 charset 解码指定的 byte 数组,构造一个新的 String
*/
bs=name.getBytes("gbk");
System.out.println(Arrays.toString(bs));
System.out.println(new String(bs,"gbk"));
/*
* String(char[] value)
分配一个新的 String,使其表示字符数组参数中当前包含的字符序列。
String(char[] value, int offset, int count)
分配一个新的 String,它包含取自字符数组参数一个子数组的字符。
*/
char[] ch={'s','h','s','x','t'};
System.out.println(new String(ch));
System.out.println(new String(ch,2,3));
/*
* String(String original)
* StringBuffer|StringBuilder 对象转为字符串对象
String(StringBuffer buffer)
String(StringBuilder builder)
*/
System.out.println(new String("hahahaha"));
}
}
public class StringDemo02 {
public static void main(String[] args) {
String str="shsxtverygood";
String str1="Shsxtverygood";
/*
* *** char charAt(int index) 返回指定索引处的 char 值。
*/
System.out.println("charAt():"+str.charAt(3));
// int codePointAt(int index) 返回指定索引处的字符(Unicode 代码点)。
System.out.println("codePointAt():"+str.codePointAt(3));
System.out.println((int)'x');
// int compareTo(String anotherString)
System.out.println("compareTo():"+str1.compareTo(str));
System.out.println("=====================");
//int compareToIgnoreCase(String str)
System.out.println("compareToIgnoreCase():"+str.compareToIgnoreCase(str1));
//***String concat(String str) 将指定字符串连接到此字符串的结尾。
System.out.println("concat():"+str.concat(str1));
//***boolean contains(CharSequence s) 当且仅当此字符串包含指定的 char 值序列时,返回 true。
System.out.println("contains():"+str.contains("sxt"));
/*
* boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结束。
boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开始。
*/
System.out.println("startsWith():"+str.startsWith("shsxt"));
/*
* byte[] getBytes()
* byte[] getBytes(String charsetName)
* 根据字节数组转为字符串
*/
/*
* void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 将字符从此字符串复制到目标字符数组。
*/
char[] ch=new char[10];
str.getChars(5, 9, ch, 3);
System.out.println(ch);
/*
* ***int indexOf(String str) 返回指定子字符串在此字符串中第一次出现处的索引。
* int indexOf(String str, int fromIndex)
*/
System.out.println("indexOf():"+str.indexOf("s",1));
System.out.println("lastIndexOf():"+str.lastIndexOf("s"));
/*
* ***String replace(char oldChar, char newChar)
*/
System.out.println("replace():"+str.replace("s","S"));
System.out.println(str);
/*
* ***String[] split(String regex)
*/
String str2="name=zhangsan";
System.out.println(Arrays.toString(str2.split("=")));
/*
* *** String substring(int beginIndex) 返回一个新的字符串,它是此字符串的一个子字符串。
*/
System.out.println("substring():"+str.substring(5));
/*
* *** String substring(int beginIndex, int endIndex) 返回一个新字符串,它是此字符串的一个子字符串
*/
System.out.println("substring():"+str.substring(5,7));
/*
* *** char[] toCharArray() 将此字符串转换为一个新的字符数组。
*/
System.out.println("toCharArray():"+Arrays.toString(str.toCharArray()));
/*
* *** String toLowerCase() 转小写
* String toUpperCase() 转小写
*/
System.out.println("toUpperCase():"+str1.toUpperCase());
/*
* *** trim() 去收尾空格
*/
String str3=" 哈哈 ";
System.out.println("trim():"+str3.trim());
int i=132;
System.out.println(String.valueOf(i).length());
}
}
public class StringDemo03 {
public static void main(String[] args) {
int a=5;
String s =new String(); //堆中的对象
s=new String();
s="abc"; //常量池
String str="haha"; //1个 对象 在字符串常量池中
String str2=new String("bcd"); //2个 一个堆中new 一个常量池中"bcd"
String str3=new String("haha"); //1个 new的堆中
}
}
/*
* StringBuilder: 线程不安全的,相对效率高
* StringBuffer : 线程安全的,相对效率低
*/
public class StringDemo04 {
public static void main(String[] args) {
//StringBuilder() 默认初始容量为16
StringBuilder sb=new StringBuilder();
//int capacity() 初始容量
System.out.println(sb);
System.out.println(sb.capacity());
System.out.println(sb.length());
//StringBuilder(CharSequence seq)
StringBuilder sb2=new StringBuilder("abc"); //str.length() + 16
System.out.println(sb2);
System.out.println(sb2.capacity());
System.out.println(sb2.length());
//StringBuilder(int capacity)
System.out.println(new StringBuilder(30).capacity());
//append(内容) 追加
StringBuilder ss=sb2.append(true);
System.out.println(ss);
System.out.println(sb2);
System.out.println(sb2==ss); //一个对象
//StringBuilder delete(int start, int end)
ss=sb2.delete(2, 5);
System.out.println(ss);
System.out.println(sb2);
System.out.println(sb2==ss);
// StringBuilder insert(int offset, String str)
ss=sb2.insert(2,"tr");
System.out.println(ss);
System.out.println(sb2);
System.out.println(sb2==ss);
//StringBuilder reverse() 翻转
ss=sb2.reverse();
System.out.println(ss);
System.out.println(sb2);
System.out.println(sb2==ss);
//注意会将字符串和可变长的字符串的转化,这样可以使用对方的功能
//将可变长的StringBuilder变为字符串
System.out.println(sb2.toString());
//将字符串变为可变长的StringBuilder
StringBuilder AA = new StringBuilder("ABCD");
System.out.println(AA.capacity());
//String 与 StringBuilder|StringBuffer对象
System.out.println("1.String的构造器");
System.out.println("1.StringBuilder的构造器");
System.out.println("1.StringBuilder的toString");
}
}
包装类
public class DataDemo01 {
public static void main(String[] args) {
int i=10; //基本数据类型
Integer in1=i; //自动装箱 Integer.valueOf(i);
System.out.println(3+in1); //自动拆箱
int i2=in1; //自动拆箱 in1.intValue()
test(1.1,2.2);
}
static void test(Double d1,Double d2){
System.out.println(d1+d2);;
}
}
日期类:
public class DateDemo01 {
public static void main(String[] args) {
//Date() 当前时间
Date date=new Date();
//默认是Mon Oct 28 16:11:48 GMT+08:00 2019 格式的
//java已经帮我们重写了tostring方法,但是这个格式真的不好看啊!可以用日期格式转换下
System.out.println(date);
//Date(long) 1970 年 1 月 1 日 00:00:00 GMT 以来的指定毫秒数。
//如果将这个L的值改成date.getTime(),通过这个构造器的创建出来的数据和第一个的数据是完全相同的
Date date2=new Date(1572250308312L);
System.out.println(date2);
// long getTime() 毫秒数 依据时间戳到现在的毫秒数
System.out.println(date.getTime());
System.out.println("==================");
//设置时间也是按照时间戳来的
date.setTime(15722490L);
System.out.println(date);
//toString
System.out.println("=======================");
//相当于直接打印date
System.out.println(date.toString());
System.out.println(date.toLocaleString()); //已经不用了
//after,before,equals 等等 比较的是前后的关系
System.out.println(date.after(date2));
}
}
日期格式转换类:
public class SimpleDateFormatDemo02 {
public static void main(String[] args) throws ParseException {
Date date=new Date();
SimpleDateFormat simple=new SimpleDateFormat(); //19-10-28 下午4:00
String str=simple.format(date);
Date d=simple.parse("2020-10-10 下午4:00");
System.out.println(d);
SimpleDateFormat simple2=new SimpleDateFormat("yyyy年MM月dd日 E hh:mm:ss SSS");
System.out.println(simple2.format(date));
}
}