String
特点:1.字符串内容永不可变
2. 字符串效果上相当于是char[]字符数组,但是底层原理是byte[]字节数组。
// 这种不叫字符串改变
String st4 = "hello";
st4 = "world";
System.out.println(st4);// 字符没有改变,st4中保存的是地址值,
本来是“hello”的0x666,变成了“world”的0x999
创建的3+1种方式:
三种构造方式:
1.public String()
2.public String(char[ ] array)
3.public String(byte[ ] array)
String str1 =new String();
System.out.println(str1);// 第一种,输出为空
char[] array1 = {'A'};
String array2 = new String(array1);
System.out.println(array2);// 第二种
byte[] array3 = {98};
String array4 = new String(array3);
System.out.println(array4);// 第三种
注意:
对于基本类型来说,“==” 是数值大小的比较,对于引用类型来说, 它是地址值的比较
=字符串内容的比较:
public boolean equals(Object obj):区分大小写
public boolean equalsIgnoreCase(String str):忽略大小写。
注意:1. equals方法具有对称性,也就是a.equals(b)和b.equals(a)效果一样。
2. 如果比较双方一个常量一个变量,推荐把常量字符串写在前面。
推荐:“abc”.equals(str) 不推荐:str.equals(“abc”)
String st1 = "abc";
String st2 = "abc";
char[] arr1 = {'a','b','c'};
String st3 = new String(arr1);
System.out.println(st1 == st2);// true
System.out.println(st1 == st3);// false
System.out.println(str2.equals(str3)); // true
与获取相关的常用方法:
public int length():获取字符串当中含有的字符个数,拿到字符串长度。
public String concat(String str):将当前字符串和参数字符串拼接成为返回值新的字符串。
public char charAt(int index):获取指定索引位置的单个字符。(索引从0开始。)
public int indexOf(String str):查找参数字符串在本字符串当中首次出现的索引位置,如果没有返回-1值。
public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符串结尾。
转换方法:
public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到endIndex截取字符串。含beginIndex,不含endIndex。
public char[] toCharArray () :将此字符串转换为新的字符数组。
public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
public static void main(String[] args) {
String s = "abcde";
// char[] toCharArray():把字符串转换为字符数组
char[] chs = s.toCharArray();
for(int x = 0; x < chs.length; x++) {
System.out.println(chs[x]);
}
}
分割方法:
public String[] split(String regex)
public static void main(String[] args) {
//创建字符串对象
String s = "aa|bb|cc";
String[] strArray = s.split("|"); // ["aa","bb","cc"]
for(int x = 0; x < strArray.length; x++) {
System.out.println(strArray[x]); // aa bb cc
}
}
static
当 static 修饰成员变量时,该变量称为类变量。该类的每个对象都共享同一个类变量的值。任何对象都可以更改该类变量的值,但也可以在不创建该类的对象的情况下对类变量进行操作。
当 static 修饰成员方法时,该方法称为类方法 。建议使用类名来调用,而不需要创建类的对象。
推荐:
类名.类变量名;
类名.静态方法名(参数);
public class StuDemo2 {
public static void main(String[] args) {
// 访问类变量
System.out.println(Student.numberOfStudent);
// 调用静态方法
Student.showNum();
}
}
注意:
1.静态方法 不能直接访问普通成员变量或成员方法。反之,成员方法可以直接访问类变量或静态方法。
2.静态方法中,不能使用 this关键字。
Arrays
(java.util.Arrays )
常用的:
public static String toString(int[] a) :返回指定数组内容的字符串表示形式。
public static void sort(int[] a) :对指定的 int 型数组按数字升序进行排序。
public static void main(String[] args) {
// 定义int 数组
int[] arr = {2,34,35,4,657,8,69,9};
// 数组内容转为字符串
String s = Arrays.toString(arr);
// 打印字符串,输出内容
System.out.println(s); // [2, 34, 35, 4, 657, 8, 69, 9]
}
Math
(java.lang.Math)
public static double abs(double a) :返回 double 值的绝对值
public static double ceil(double a) :返回大于等于参数的最小的整数
public static double floor(double a) :返回小于等于参数最大的整数。
public static long round(double a) :返回最接近参数的 long。(相当于四舍五入方法)
double d1 = Math.abs(‐5); //d1的值为5
double d2 = Math.ceil(‐3.3); //d2的值为 ‐3.0
double d3 = Math.floor(5.1); //d3的值为 5.0
long d4 = Math.round(5.5); //d1的值为6.0