一、String类
1.什么是字符串
被双引号括起来、由多个字符串在一起组成的一个串子 就是字符串
字符串内容不可变的
字符串的相同内容是共享的
2.构造方法
public String():创建一个空白字符串,不含有任何内容。
public String(char[] array):根据字符数组的内容,来创建对应的字符串。
public String(byte[] array):根据字节数组的内容,来创建对应的字符串。
示例代码:
public class Demo01String {
public static void main(String[] args) {
// 使用空参构造
String str1 = new String(); // 小括号留空,说明字符串什么内容都没有。
System.out.println("第1个字符串:" + str1);
// 根据字符数组创建字符串
char[] charArray = { 'A', 'B', 'C' };
String str2 = new String(charArray);
System.out.println("第2个字符串:" + str2);
// 根据字节数组创建字符串
byte[] byteArray = { 97, 98, 99 };
String str3 = new String(byteArray);
System.out.println("第3个字符串:" + str3);
// 直接创建
String str4 = "Hello";
System.out.println("第4个字符串:" + str4);
}
}
3.直接赋值字符串和new出来的字符串对象的区别
直接赋值:保存的是堆内存中的字符串常量池中的地址值
new出来:直接保存的是堆内存中的地址值
示例代码:
public class Demo02StringPool {
public static void main(String[] args) {
String str1 = "abc";
String str2 = "abc";
char[] charArray = {'a', 'b', 'c'};
String str3 = new String(charArray);
System.out.println(str1 == str2); // true
System.out.println(str1 == str3); // false
System.out.println(str2 == str3); // false
}
}
4.比较方法
boolean equals(Object obj); 比较字符串内容是否相同。区分大小写
boolean equalsIgnoreCase(String s); 比较字符串内容是否相同。忽略大小写(字母)
注意事项:如果一个常量和变量比较,推荐将常量放在前面
示例代码:
public class Demo01StringEquals {
public static void main(String[] args) {
String str1 = "Hello";
String str2 = "Hello";
char[] charArray = {'H', 'e', 'l', 'l', 'o'};
String str3 = new String(charArray);
System.out.println(str1.equals(str2)); // true
System.out.println(str2.equals(str3)); // true
System.out.println(str3.equals("Hello")); // true
System.out.println("Hello".equals(str1)); // true
String str4 = "hello";
System.out.println(str1.equals(str4)); // false
System.out.println("=================");
String str5 = null;
System.out.println("abc".equals(str5)); // 推荐:false
// System.out.println(str5.equals("abc")); // 不推荐:报错,空指针异常NullPointerException
System.out.println("=================");
String strA = "Java";
String strB = "java";
System.out.println(strA.equals(strB)); // false,严格区分大小写
System.out.println(strA.equalsIgnoreCase(strB)); // true,忽略大小写
// 注意,只有英文字母区分大小写,其他都不区分大小写
System.out.println("abc一123".equalsIgnoreCase("abc壹123")); // false
}
}
5.String类的获取方法
int length(); 获取字符串的长度
String concat(String s); 拼接字符串
char charAt(int index); 获取指定索引上的字符
int indexOf(String s); 查找传入的字符串在当前字符串中第一次出现的索引位置
示例代码:
public class Demo02StringGet {
public static void main(String[] args) {
// 获取字符串的长度
int length = "asdasfeutrvauevbueyvb".length();
System.out.println("字符串的长度是:" + length);
// 拼接字符串
String str1 = "Hello";
String str2 = "World";
String str3 = str1.concat(str2);
System.out.println(str1); // Hello,原封不动
System.out.println(str2); // World,原封不动
System.out.println(str3); // HelloWorld,新的字符串
System.out.println("==============");
// 获取指定索引位置的单个字符
char ch = "Hello".charAt(1);
System.out.println("在1号索引位置的字符是:" + ch);
System.out.println("==============");
// 查找参数字符串在本来字符串当中出现的第一次索引位置
// 如果根本没有,返回-1值
String original = "HelloWorldHelloWorld";
int index = original.indexOf("llo");
System.out.println("第一次索引值是:" + index); // 2
System.out.println("HelloWorld".indexOf("abc")); // -1
}
}
6.截取功能
String substring(int index); 截取字符串。从指定索引开始,一直到字符串结尾
String substring(int start,int end); 截取字符串。从start开始,到end-1结束
示例代码:
public class Demo03Substring {
public static void main(String[] args) {
String str1 = "HelloWorld";
String str2 = str1.substring(5);
System.out.println(str1); // HelloWorld,原封不动
System.out.println(str2); // World,新字符串
System.out.println("================");
String str3 = str1.substring(4, 7);
System.out.println(str3); // oWo
System.out.println("================");
// 下面这种写法,字符串的内容仍然是没有改变的
// 下面有两个字符串:"Hello","Java"
// strA当中保存的是地址值。
// 本来地址值是Hello的0x666,
// 后来地址值变成了Java的0x999
String strA = "Hello";
System.out.println(strA); // Hello
strA = "Java";
System.out.println(strA); // Java
}
}
7.String类的转换方法
char[] toCharArray(); 将字符串转成字符数组
byte[] getBytes(); 将字符串转成字节数组
String replace(String oldStr,String newStr); 将字符串中所有的老字符串替换成新的字符串
8.String类的切割方法
String[] split(String regex); 将字符串按照指定的规则进行切割
实例代码:
public class Demo05StringSplit {
public static void main(String[] args) {
String str1 = "aaa,bbb,ccc";
String[] array1 = str1.split(",");
for (int i = 0; i < array1.length; i++) {
System.out.println(array1[i]);
}
System.out.println("===============");
String str2 = "aaa bbb ccc";
String[] array2 = str2.split(" ");
for (int i = 0; i < array2.length; i++) {
System.out.println(array2[i]);
}
System.out.println("===============");
String str3 = "XXX.YYY.ZZZ";
String[] array3 = str3.split("\\.");
System.out.println(array3.length); // 0
for (int i = 0; i < array3.length; i++) {
System.out.println(array3[i]);
}
}
}
9.String字符串的练习-按照指定格式打印字符串
public class Demo06StringPractise {
public static void main(String[] args) {
int[] array = {1, 2, 3, 4};
String result = fromArrayToString(array);
System.out.println(result);
}
public static String fromArrayToString(int[] array) {
String str = "[";
for (int i = 0; i < array.length; i++) {
if (i == array.length - 1) {
str += "word" + array[i] + "]";
} else {
str += "word" + array[i] + "#";
}
}
return str;
}
}
10.String字符串的练习-统计一个字符串中各种字符出现的次数
public class Demo07StringCount {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入一个字符串:");
String input = sc.nextLine(); // 获取键盘输入的一个字符串
int countUpper = 0; // 大写字母
int countLower = 0; // 小写字母
int countNumber = 0; // 数字
int countOther = 0; // 其他字符
//将字符串转成一个字符数组
char[] charArray = input.toCharArray();
//遍历字符数组
for (int i = 0; i < charArray.length; i++) {
// 当前单个字符
char ch = charArray[i];
if (ch >= 'A' && ch <= 'Z') {
countUpper++;
} else if (ch >= 'a' && ch <= 'z') {
countLower++;
} else if (ch >= '0' && ch <= '9') {
countNumber++;
} else {
countOther++;
}
}
System.out.println("大写字母有:" + countUpper);
System.out.println("小写字母有:" + countLower);
System.out.println("数字有:" + countNumber);
System.out.println("其他字符有:" + countOther);
}
}
二、静态
1.什么是静态
static关键字,他是一个修饰符。被他所修饰的成员就是静态的了。可以被这个类的所有的对象所共享!
2.静态有哪些特点
静态成员是属于类的
静态成员被类中的所有对象所共享
静态成员优先于对象存在
静态成员可以直接通过类名来去调用
3.静态和非静态的访问特点&注意事项
静态要比非静态先存在。所以静态的成员只能访问静态的。
非静态的成员是属于对象的,也就是说既然对象都已经创建好了,那么这个类肯定也加载完毕了。
所以非静态的成员既可以访问静态的成员,也可以访问非静态的成员
4.使用场景
什么时候该用static去修饰成员变量?
当一个变量需要被所有的对象所共享的时候,那么就可以将这个变量设置成静态的
public class Student {
private String name;
private int age;
private String gender;
//学费 16980
static int money;
public Student() {
}
public Student(String name, int age, String gender) {
this.name = name;
this.age = age;
this.gender = gender;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
"money='" + money + '\'' +
'}';
}
}
什么时候该用static去修饰成员方法?
一些工具类当中,提供的方法全都是静态的方法
public class MyArrays {
//私有构造方法
private MyArrays(){
}
public static int getMax(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++) {
if(arr[i] > max) {
max = arr[i];
}
}
return max;
}
}
5.静态修饰成员变量
public class Demo01StaticField {
public static void main(String[] args) {
Student.room = "102教室";
Student two = new Student("黄蓉", 16);
//two.room = "101教室";
System.out.println("姓名:" + two.getName()
+ ",年龄:" + two.getAge() + ",教室:" + two.room
+ ",学号:" + two.getId());
Student one = new Student("郭靖", 19);
System.out.println("姓名:" + one.getName()
+ ",年龄:" + one.getAge() + ",教室:" + one.room
+ ",学号:" + one.getId());
}
}
6.静态修饰成员方法
public class MyClass {
int num; // 成员变量
static int numStatic; // 静态变量
// 成员方法
public void method() {
System.out.println("这是一个成员方法。");
// 成员方法可以访问成员变量
System.out.println(num);
// 成员方法可以访问静态变量
System.out.println(numStatic);
}
// 静态方法
public static void methodStatic() {
System.out.println("这是一个静态方法。");
// 静态方法可以访问静态变量
System.out.println(numStatic);
// 静态不能直接访问非静态【重点】
// System.out.println(num); // 错误写法!
// 静态方法中不能使用this关键字。
// System.out.println(this); // 错误写法!
}
}
7.静态代码块
定义格式:
在类当中,写一个静态代码块:
public class 类名{
static {
//要执行的代码
}
}
执行特点:
只要这个类一加载(或者是这个类被用到的时候),静态代码块就会执行了。
静态代码块只执行一次。而且是要优先于构造方法执行的。
三、数组工具类
1.Arrays常用的方法
String toString(int[] arr); 将数组内容转成一个字符串
void sort(int[] arr); 对数组进行升序排序
示例代码:
public class Demo01Arrays {
public static void main(String[] args) {
int[] intArray = {10, 20, 30};
// 将int[]数组按照默认格式变成字符串
String intStr = Arrays.toString(intArray);
System.out.println(intStr); // [10, 20, 30]
int[] array1 = {2, 1, 3, 10, 6};
Arrays.sort(array1);
System.out.println(Arrays.toString(array1)); // [1, 2, 3, 6, 10]
/*for(int i = array1.length-1; i >= 0; i--) {
System.out.print(array1[i]+ " ");
}
System.out.println();*/
String[] array2 = {"bbb", "aaa", "ccc"};
Arrays.sort(array2);
System.out.println(Arrays.toString(array2)); // [aaa, bbb, ccc]
}
}
2.排序的小案例
public class Demo02ArraysPractise {
public static void main(String[] args) {
String str = "asv76agfqwdfvasdfvjh";
// 如何进行升序排列:sort
// 必须是一个数组,才能用Arrays.sort方法
// String --> 数组,用toCharArray
char[] chars = str.toCharArray();
Arrays.sort(chars); // 对字符数组进行升序排列
// 需要倒序遍历
for (int i = chars.length - 1; i >= 0; i--) {
System.out.println(chars[i]);
}
}
}
四、数学工具类
1.Math类的常用方法
double abs(double num); 获取绝对值
double ceil(double num); 向上取整
double floor(double num); 向下取整
long round(double num); 四舍五入
2.Math的静态常量
PI: 圆周率
3.示例代码:
public class Demo03Math {
public static void main(String[] args) {
// 获取绝对值
System.out.println(Math.abs(3.14)); // 3.14
System.out.println(Math.abs(0)); // 0
System.out.println(Math.abs(-2.5)); // 2.5
System.out.println("================");
// 向上取整
System.out.println(Math.ceil(3.9)); // 4.0
System.out.println(Math.ceil(3.1)); // 4.0
System.out.println(Math.ceil(3.0)); // 3.0
System.out.println("================");
// 向下取整,抹零
System.out.println(Math.floor(30.1)); // 30.0
System.out.println(Math.floor(30.9)); // 30.0
System.out.println(Math.floor(31.0)); // 31.0
System.out.println("================");
System.out.println(Math.round(20.4)); // 20
System.out.println(Math.round(10.5)); // 11
}
}
4.Math的小案例
public class Demo04MathPractise {
public static void main(String[] args) {
int count = 0; // 符合要求的数量
double min = -10.8;
double max = 5.9;
// 这样处理,变量i就是区间之内所有的整数
for (int i = (int) min; i < max; i++) {
int abs = Math.abs(i); // 绝对值
if (abs > 6 || abs < 2.1) {
System.out.println(i);
count++;
}
}
System.out.println("总共有:" + count); // 9
}
}