常用类-->字符类
String 不可变字符序列
StringBuilder 可变长字符序列,线程不安全|不同步,效率较高
StrigBuffer 可变长字符序列,线程安全|同步,效率低下
String 类表示字符串。Java程序中的所有文字(例如"abc")都实现为此类的实例
String str="abc"; 创建一个字符串对象"abc",字符串常量-->字符串常量池中
String str2=new String ("haha"); 两个对象 第一个new -->在堆中"abc"-->上面
已经使用过,字符串常量池中已经存在
字符串底层:
jdk11-->使用private final byte[] value; 字节数组存储字符串中的字符数据
jdk8-->private final char[] value;字符数组
//例子:
//String() 初始化新创建的 String对象,使其表示空字符序列。
String str1 = new String();
String str2 = "";
System.out.println(str1==str2); //false
//String(String original) 初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本
String str3 = new String("abc");
System.out.println(str3);
//String(char[] value) 分配新的 String ,使其表示当前包含在字符数组参数中的字符序列。
//字符数组转为字符串
char[] arr = {'y','j','x','x','t'};
String str4 = new String(arr);
System.out.println(str4);
//String(char[] value, int offset, int count) 分配一个新的 String ,其中包含字符数组参数的子数组中的字符。
//字符数组中某一部分字符转为字符串
String str5 = new String(arr,1,3);
System.out.println(str5);
//String (byte[] bytes) 通过使用平台的默认字符集解码指定的字节数组构造新的 String 。
//不同的字符编码格式(转换方式参照不同的字符集) 1个汉字->gbk->2个字节 1个汉字->utf-8->3个字节
//字符串转为字节数组
byte[] arr2 = "你好".getBytes(); //默认字符编码格式utf-8
byte[] arr3 = "你好".getBytes("gbk"); //字符编码格式gbk
System.out.println(arr2.length);
System.out.println(arr3.length);
//要保证编码解码字符编码一致才不会出现乱码
//字节数组转为字符串-->注意编码格式问题
String str6 = new String(arr2);
System.out.println(str6);
//String(byte[] bytes, String charsetName) 构造一个新的String由指定用指定的字节的数组解码charset 。
String str7 = new String(arr3,"gbk");
System.out.println(str7);
//字节数组中某一部分字节数据转为字符串
//String(byte[] bytes, int offset, int length) 通过使用平台的默认字符集解码指定的字节子阵列来构造新的 String 。
//String(byte[] bytes, int offset, int length, String charsetName) 通过使用指定的字符集解码指定的字节子 String构造新的 String 。
String str8 = new String(arr2,3,3);
System.out.println(str8);
String常用方法:
String str1 = new String("jintiantainqihenre");
String str2 = new String("jintiantainqihenre");
//char charAt(int index) 返回指定索引处的 char值。
System.out.println(str1.charAt(5));
//int codePointAt(int index) 返回指定索引处的字符(Unicode代码点)。
System.out.println(str1.codePointAt(5));
//int compareTo(String anotherString) 按字典顺序比较两个字符串。
//str1.compareTo(str2) 返回值 : 0-->str1=str2 >0--> str1>str2 <0-->str1<str2
System.out.println(str1.compareTo(str2));
//int compareToIgnoreCase(String str) 按字典顺序比较两个字符串,忽略大小写差异。
System.out.println(str1.compareToIgnoreCase(str2));
//String concat(String str) 将指定的字符串连接到此字符串的末尾。
System.out.println(str1.concat(str2));
System.out.println(str1);
//boolean contains(CharSequence s) 当且仅当此字符串包含指定的char值序列时,才返回true。
System.out.println(str1.contains("jin"));
//boolean endsWith(String suffix) 测试此字符串是否以指定的后缀结尾。
System.out.println(str1.endsWith("hao"));
//boolean startsWith(String prefix) 测试此字符串是否以指定的前缀开头。
System.out.println(str1.startsWith("jin"));
//字符串转为字节数组,可以指定编码格式
//byte[] getBytes()
//byte[] getBytes(String charsetName)
//void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) 将此字符串中的字符复制到目标字符数组中。
char[] arr = new char[6];
str1.getChars(3,7,arr,1);
System.out.println(Arrays.toString(arr));
//int indexOf(String str) 返回指定子字符串第一次出现的字符串中的索引。
System.out.println(str1.indexOf("a"));
//int indexOf(String str, int fromIndex) 从指定的索引处开始,返回指定子字符串第一次出现的字符串中的索引。
System.out.println(str1.indexOf("a",6));
//int lastIndexOf(int ch) 返回指定字符最后一次出现的字符串中的索引。
//int lastIndexOf(int ch, int fromIndex) 返回指定字符最后一次出现的字符串中的索引,从指定的索引开始向后搜索。
System.out.println(str1.lastIndexOf("a"));
//String intern() 返回字符串对象的规范表示。
System.out.println(str1==str2);
System.out.println(str1.intern()==str2.intern());
//boolean isBlank() 如果字符串为空或仅包含 white space代码点,则返回 true ,否则 false 。
System.out.println(str1.isBlank());
System.out.println("".isBlank());
System.out.println(" ".isBlank());
System.out.println(" ".isBlank());
System.out.println(" ".isBlank());
//boolean isEmpty() 返回 true ,当且仅当, length()是 0 。
//int length() 返回此字符串的长度。
System.out.println();
//String repeat(int count) 返回一个字符串,其值为此字符串的串联重复 count次。
System.out.println(str1.repeat(3));
//String replace(CharSequence target, CharSequence replacement) 将此字符串中与文字目标序列匹配的每个子字符串替换为指定的文字替换序列。
System.out.println(str1.replace("a","A"));
//String[] split(String regex) 将此字符串拆分为给定 regular expression的匹配 项 。
System.out.println(Arrays.toString(str1.split("a")));
//去除前后空格 半角空格" " 全角空格" "
//String trim() 返回一个字符串,其值为此字符串,删除了所有前导和尾随空格,其中space被定义为其代码点小于或等于 'U+0020' (空格字符)的任何字符。 --> 只针对于半角空格
System.out.println(" abc ".trim());
//String strip() 返回一个字符串,其值为此字符串,并删除了所有前导和尾随 white space 。 --> 全角以及半角空格
System.out.println(" abc ".strip());
//String stripLeading() 返回一个字符串,其值为此字符串,并删除了所有前导 white space 。
//String stripTrailing() 返回一个字符串,其值为此字符串,并删除所有尾随 white space 。
//String substring(int beginIndex) 返回一个字符串,该字符串是此字符串的子字符串。
System.out.println(str1.substring(5));
//String substring(int beginIndex, int endIndex) 返回一个字符串,该字符串是此字符串的子字符串。 一般结束索引不包含
System.out.println(str1.substring(5,10));
//char[] toCharArray() 将此字符串转换为新的字符数组。
System.out.println(Arrays.toString(str1.toCharArray()));
System.out.println(str1.charAt(str1.length()-1));
//String toLowerCase() 使用默认语言环境的规则将此 String所有字符转换为小写。
//String toUpperCase() 使用默认语言环境的规则将此 String所有字符转换为大写。
System.out.println(str1.toUpperCase());
System.out.println(str2.toLowerCase());
///static String valueOf(boolean b) 返回 boolean参数的字符串表示形式。
System.out.println(String.valueOf(false));
Math:数学相关类
静态工厂--工具包
java.lang下不需要导包
//static double abs(double a) 返回 double值的绝对值。
System.out.println(Math.abs(-300));
//static double ceil(double a) 返回大于或等于参数且等于数学整数的最小值(最接近负无穷大) double 。
System.out.println(Math.ceil(2.0));
System.out.println(Math.ceil(-2.2));
//static double floor(double a) 返回小于或等于参数且等于数学整数的最大值(最接近正无穷大) double 。
System.out.println(Math.floor(2.9));
//static double max(double a, double b) 返回两个 double值中较大的 double 。
//static int min(int a, int b) 返回两个 int值中较小的 int 。
System.out.println(Math.max(18,17));
//static double pow(double a, double b) 返回第一个参数的值,该值是第二个参数的幂。
System.out.println(Math.pow(2,3));
System.out.println(Math.pow(3,3));
//static double random() 返回带有正号的 double值,大于或等于 0.0且小于 1.0 。 [0.0,1.0)
/*
随机整数:
[min,max) (int)(Math.random()*(max-min)+min)
[0.0,1.0) * (max-min)
[0.0,max-min) + min
[min,max)
[min,max] (int)(Math.random()*(max-min+1)+min)
*/
//[5,10]
System.out.println((int)(Math.random()*(10-5+1)+5));
System.out.println((int)(Math.random()*(10-5+1)+5));
System.out.println((int)(Math.random()*(10-5+1)+5));
System.out.println((int)(Math.random()*(10-5+1)+5));
//static long round(double a) 返回与参数最接近的 long ,并将关系四舍五入为正无穷大。 只针对小数点后一位
System.out.println(Math.round(3.5));
System.out.println(Math.round(3.45));
//static double sqrt(double a) 返回 double值的正确舍入正平方根。
System.out.println(Math.sqrt(9));
System.out.println(Math.sqrt(4));
System.out.println(Math.sqrt(2));
System.out.println(Math.PI);
可变长字节序列;
StringBuilder : 线程不安全,但不保证同步,相对效率较高
适合使用在单线程下大量操作字符串,效率高
StringBuffer : 线程安全的,相对效率较低
多线程下大量操作字符串建议使用StringBuffer
String : 少量修改字符串,适合使用String,因为表示字符串String对象简单,功能强大的API
效率: StringBuilder > StringBuffer > String
扩容:
append--> int newCapacity = (oldCapacity << 1) + 2; 每次扩容原容量的2倍+2
//StringBuilder() 构造一个字符串构建器,其中不包含任何字符,初始容量为16个字符。
StringBuilder sb = new StringBuilder();
System.out.println(sb);
System.out.println(sb.length()); //字符个数
System.out.println(sb.capacity()); //内部存储数据的字节数组的长度
//StringBuilder(int capacity) 构造一个字符串构建器,其中没有字符,并且具有 capacity参数指定的初始容量。
StringBuilder sb1 = new StringBuilder(10);
System.out.println(sb1);
System.out.println(sb1.length());
System.out.println(sb1.capacity());
//StringBuilder(String str) 构造一个初始化为指定字符串内容的字符串构建器。
StringBuilder sb2 = new StringBuilder("abc");
System.out.println(sb2);
System.out.println(sb2.length());
System.out.println(sb2.capacity());
//StringBuilder append(boolean b) 追加
StringBuilder stringBuilder = sb.append(false);
System.out.println(sb);
System.out.println(stringBuilder==sb);
//StringBuilder delete(int start, int end) 删除此序列的子字符串中的字符。
System.out.println(sb.delete(1,4));
System.out.println(sb);
System.out.println(sb.length());
System.out.println(sb.capacity());
//测试扩容问题
sb.append("1234567890");
System.out.println(sb.length());
System.out.println(sb.capacity());
sb.append("1234");
System.out.println(sb.length());
System.out.println(sb.capacity());
sb.append("1");
System.out.println(sb.length());
System.out.println(sb.capacity());
//StringBuilder insert(int offset, String str) 将字符串插入此字符序列。
System.out.println(sb.insert(3,"haha"));
//StringBuilder reverse() 导致此字符序列被序列的反向替换。
System.out.println(sb.reverse());
System.out.println(sb);
//String 与 StringBuffer|StringBuilder转换问题:
//1.new StringBuilder|StringBuffer(String)
//2.toString() | new String(StringBuilder|StringBuffer)
System.out.println(sb.toString());
int ,Integer,new Integer() 三者之间比较是否相等
1.两个基本数据类型只要数据值相等,==比较结果就相等
2.两个new 包装类比较,无论数据值是否相等,都不相等,因为new在堆中创建新的地址空间
3.Integer i3,与Integer i4,两个自动装箱的对象比较,在[-128,127]范围之内相等,否则返回new Integer不相等
4.int与Integer|new Integer() ,只要数据值相等就相等,因为会发生自动拆箱
*/
**基本数据类型的包装:
基本 包装
byte Byte
short Short
int Integer
long Long
char Character
boolean Boolean
float Float
double Double
包装类的优点:
1.类可以提供很多成员,功能...
2.集合中之能存储引用数据类型,想要存储基本数据类型数据的时候,可以先转为对应的包装类型,再存储
3.基本数据类型与对应包装类型数据的默认值不同,当在具体业务下,比如区分账户余额的0与null两种状态,可以使用包装类型表示账户余额
基本数据类型的优点:
有利于节约内存
自动拆装箱:
1.自动装箱: 基本-->包装
2.自动拆箱: 包装-->基本
int i = 1;
//自动装箱 Integer.valueOf(int)
Integer in = 1;
//自动拆箱 in.intValue()
int i2 = in;
test(1.1,2.2); //自动装箱
}
static void test(Double d1,Double d2){
System.out.println(d1+d2); //自动拆箱
}
}