一、String类
概述:程序中所有的双引号字符串,都是String类的对象。(就算没有new,照样算是)
特点:
1、字符串的内容用不可变【重点】
2、因为字符串【String对象】是不可变的,所以它们可以被共享
3:字符串效果上相当于char[]字符数组 例:"abc" 效果上等效于 char[] data={ 'a' , 'b' , 'c' }
例:String str="abc"; 相当于 char data[] = {'a', 'b', 'c'}; String str = new String(data); 只是对象是由JVM自动创建的
4、用过双引号直接创建的对象存放到字符串常量池中,而通过关键字new获得的对象存放到栈内存中,两者不同
二、String类的构造方法
1、无参构造:String str = new String()
2、通过字符数组构造 String str = new String(char[] chars)
3、通过字节数组构造 String str = new String(byte[] bytes)
三、==号比较的解释
对于基本类型来说, ==是进行数值的比较
对于引用类型来说, ==是进行内存地址的比较
public class StringTest2 {
public static void main(String[] args) {
String str1="abc";
String str2="abc";
char[] chars={'a','b','c'};
String str3 = new String(chars);
System.out.println(str1==str2); //true
System.out.println(str1==str3); //false
System.out.println(str2==str3); //false
}
}
四、 常用方法
1、equals():只有参数是一个字符串且内容相同时返回true,否则返回false
2、equalsIgnoreCase():忽略大小写
3、int length () :返回字符串长度
4、char charAt (int index):根据字符串的索引找到相应的值
5、int indexOf (String str):根据字符找到相应的索引
6、String substring (int beginIndex, int endIndex):截取字符串片段
7、String concat (String str):将指定的字符串连接到该字符串的末尾。
8、char[] toCharArray ():将字符串转换为char[]型数组
9、byte[] getBytes ():将字符串转换为byte[]型数组
10、String replace (CharSequence target, CharSequence replacement):将target替换为replacement
11、String[] split(String regex):将此字符串按照给定的regex(规则)拆分为字符串数组
public class StringTest3 {
public static void main(String[] args) {
String str1="abc";
String str2="abc";
char[] chars={'a','b','c'};
String str3 = new String(chars);
String str4="ABC";
String str5="thisisworld";
String s="helloworld";
s.concat("JAVABase");
System.out.println(str1.equals(str2)); //true
System.out.println(str1.equals(str3)); //true
System.out.println(str3.equals(str4)); //false
System.out.println(str3.equalsIgnoreCase(str4)); //true
System.out.println(str1.length()); //3
System.out.println(str1.charAt(1)); //'b'
System.out.println(str1.indexOf('b')); //1
System.out.println(str5.substring(3,6)); //sis
System.out.println(s.concat("JAVABase")); //helloworldJAVABase
System.out.println(".........<==>..........");
char[] chars1 = str2.toCharArray();
for (int i = 0; i < chars1.length; i++) {
System.out.println(chars1[i]);
}
String str6 = new String(chars1);
System.out.println(str6);
System.out.println(".........<==>..........");
String str7="itcast itheima";
System.out.println(str7.replace("it","TT"));//TTcast TTheima
System.out.println(".........<==>..........");
System.out.println("将a-b-c-d转化abcd");
String str8="a-b-c-d";
String[] splitStr = str8.split("-");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < splitStr.length; i++) {
sb.append(splitStr[i]);
}
System.out.println(sb.toString());
}
五、Arrays类
1、Arrays.toString(arr):数组内容转为字符串
public class ArraysTest {
public static void main(String[] args) {
int[] arr = {1,2,4,3,7};
String string = Arrays.toString(arr);
System.out.println(string);
}
}
2、sort(int[] a):对指定的 int 型数组按数字升序进行排序
public class ArraysTest1 {
public static void main(String[] args) {
int[] arr = {1,2,4,3,7};
System.out.println("....<排序前>...");
System.out.println(Arrays.toString(arr));
System.out.println("....<排序后>...");
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
}
}
六、 Math类
1、double abs(double a):返回 double 值的绝对值。
double d1 = Math.abs(‐5); //d1的值为5
double d2 = Math.abs(5); //d2的值为5
2、double ceil(double a):返回大于等于参数的最小的整数
double d1 = Math.ceil(3.3); //d1的值为 4.0
double d2 = Math.ceil(‐3.3); //d2的值为 ‐3.0
double d3 = Math.ceil(5.1); //d3的值为 6.0
3、public static long round(double a):返回最接近参数的 long。(相当于四舍五入方法)
long d1 = Math.round(5.5); //d1的值为6.0
long d2 = Math.round(5.4); //d2的值为5.0