正文
java第4天:String static Arrays类,Math类
小程序:扫一扫查出行
【扫一扫了解最新限行尾号】
复制小程序
【扫一扫了解最新限行尾号】
复制小程序
1 字符串的概述和特点字符串一旦创建,是不可变的。
有双引号的就是字符串
***
2 字符串的三种构造方法2-1 第一种:
字符串一旦创建,是不可变的。
有双引号的就是字符串
***
2-1 第一种:
格式:String str = new String();|
:-|
2-2 第二种:
格式:char[] chararray = {'a','b'};|
:-|
String str2 = new String(chararray);|
2-3 第三种
格式:byte[] bytearray ={97,98};|
:-|
String str3 = new String(bytearray);|
***
3 字符串的常量池
对于”==“来说,基本类型就是对数值的比较,引用类型则是针对地址的比较。
***
4 —10字符串的常用方法
字符串方法 | 解释 |
---|---|
str1.equals(str2) | 比较两个字符串是否一样,区分大小写,返回布尔值 |
str1.equalsIgnoreCase(str2) | 比较两个字符串是否一样,不区分大小写,返回布尔值 |
str.length | 字符串长度 |
str1.concat(str2) | 两个字符串拼接 |
str1.charAt(0) | 索引位置0的字符 |
str1.indexOf('H') | 查看H字符在字符串首次出现的位置 |
str1.substring(5) | 切片,从索引5到末尾 |
str1.substring(5,10) | 从索引5-9[5,9) |
str1.toCharArray(); | 字符串转换为字符数组 |
str1.getBytes(); | 将字符串转为字节数组 |
str1.replace("hell","soft"); | 字符串的替换,新生成一个字符串 |
char[] res = str2.split(","); | 将字符串按照逗号分隔 |
11 Static静态成员变量内存图
12 Static修饰成员变量静态成员变量内容不属于对象,属于类,通过类调用。
12-1定义一个学生类
静态成员变量内容不属于对象,属于类,通过类调用。
package demo1;public class Student { private String name; private int age; static String room; public Student(String name, int age) { this.name = name; this.age = age; } public Student() { } 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; }}
12-1 创建学生类对象,通过类名直接调用Static成员变量
package demo1;public class demo2 { public static void main(String[] args) { Student.room = "1001"; Student stu1 = new Student("wangsiyu",18); Student stu2 = new Student("wangpei",16); System.out.println("姓名:"+stu1.getName()); System.out.println("年龄:"+stu1.getAge()); System.out.println("教室:"+Student.room); System.out.println("姓名:"+stu2.getName()); System.out.println("年龄:"+stu2.getAge()); System.out.println("教室:"+Student.room); }}
13 static修饰成员方法
1.有static的成员方法可以直接通过类来调用成员方法,不需要通过对象调用。
2.对于本类中的静态方法,可以省略类名称
3.静态方法可以访问静态成员变量,不能访问非静态成员变量
13-1 创建学生类
package demo1;public class Student { String name = "wangsiyu"; static int room = 1024; public static void sleep(){// 静态成员方法不能调用非静态成员变量 System.out.println(room); }}
13-2 实例化类
package demo1;public class myclass { public static void main(String[] args) {// 静态成员变量,通过类直接调用 System.out.println(Student.room);// 静态成员方法,通过类直接调用 Student.sleep(); }}
14 静态代码块
1.当用到本类,静态代码执行唯一的一次
2.静态代码块先于构造方法执行,执行唯一的一次。
package demo1;public class staticcode { static{ System.out.println("hello"); } public static void Student(){ System.out.println("我是学生方法"); }}
15 数组工具类Arrays
Arrays是一个和数组相关的工具类,里面提供了大量关于数组的静态方法。
将数组转为特定格式字符串|Arrays.toString(数组);
:-|:-
package demo1;import java.util.Arrays;public class test { public static void main(String[] args) { int[] array1 = {1,2,3,4,56,67}; String res = Arrays.toString(array1); System.out.println(res);//[1, 2, 3, 4, 56, 67] }}
将数组进行排序 | Arrays.sort(数组); |
---|
package demo1;import java.util.ArrayList;import java.util.Arrays;public class test { public static void main(String[] args) { int[] array1 = {1,2,88,4,56,67}; Arrays.sort(array1); System.out.println(array1); for (int i = 0; i < array1.length; i++) { System.out.println(array1[i]); } }}
16 字符串倒序排列
package demo1;import java.util.ArrayList;import java.util.Arrays;public class test { public static void main(String[] args) {// 声名一个字符串 String str1 = "hsfljfoisdjtksd";// 将字符串转为字符数组 char[] res = str1.toCharArray();// 对数组进行排序 Arrays.sort(res);// 将数组转为字符串 String str3 = Arrays.toString(res); System.out.println(str3); }}
17 Math类
绝对值 | Math.ads(); | double |
---|---|---|
派 | Math.PI | double |
向上取整 | Math.ceil() | double |
向下取整 | Math.floor() | double |
四舍五入 | Math.round() | int |