java17

string 类
字符串常量池
public boolean equal ( Object obj )
public int length ( ) 方法
分割字符串
SubString 方法
Arrays 类
StaticFiled 方法
StaticMethod 方法
1、string
String 类代表字符串。
Java程序中所有字符串字面值(如:“abc”)都作为此类的实现。也就是说,“abc"都是String 类的对象。
字符串的特点:
1.字符串内容永远不可变(重点)
2.字符串可以共享使用。
3.字符串效果上是相当于一个char[],但是实际底层存储的是byte[]。
常用的三种构造方法:
1.public String() 创建一个空白的字符串,不包含任何内容。
2.public String(char[] array) 根据字符数组来创建字符串。
3.public String(byte[] array) 根据字节数组来创建字符串。
最直接的方式:String str = “class5”;
例一:
运行代码如下:
package demo01;
public class Demo01String {
public static void main(String[] args) {
String str1 = new String();
System.out.println(“第一个字符串:”+str1);
System.out.println(”");
char[] chararray = new char[10];
System.out.println(chararray);
String str2 = new String(chararray);
System.out.println(str2);
System.out.println("
");
byte[] bytearray = {10,20,30};
System.out.println(chararray);
String str3 = new String(chararray);
System.out.println(str3);
//String str4 = new
String str4 = “class5”;
System.out.println(str4);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
运行结果:
2、字符串常量池
字符串常量池
int a = 5;
int b = 6;
对于基本类型 == 比较的是值
对于引用类型 == 比较的是地址值
例二:
运行代码如下:
package demo01;
public class Demo02StringPool {
public static void main(String[] args) {
String str1 = “abc”;
String str2 = “abc”;
System.out.println(str2);
char[] chararray = {‘a’,‘b’,‘c’};
String str3 = new String(chararray);
System.out.println(str1 == str2);//第一个和第二个的地址是一样的
System.out.println(str1 == str3);
System.out.println(str2 == str3);
str2 = “hjsdkfbasjfbasj”;
System.out.println(str2);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
运行结果:
3、public boolean equal(Object obj) 方法
public boolean equal(Object obj)
说明:
1.这个方法具有对称性 a.equal(b) b.equal(b)
2.但建议写成"Why".equals(str1)
例三:
运行代码如下:
package demo02;
public class Demo01StringEquals {
public static void main(String[] args) {
String str1 = “Why”;
String str2 = “Why”;
System.out.println(str2);
char[] chararray = {‘W’,‘h’,‘y’};
String str3 = new String(chararray);

System.out.println(str1.equals(str2));
System.out.println(str2.equals(str3));
System.out.println(str1.equals(str3));

System.out.println(str1.equals("Why"));

System.out.println("Why".equals(str1));
System.out.println("====================");
String str4 = null;
// System.out.println(str1.equals("fdnkfn"));
System.out.println("Why".equals(str4));
System.out.println("====================");

String str5 = "why";
// System.out.println(str1.equals("fnjdjsk"));
System.out.println("why".equals(str5));
System.out.println("====================");

System.out.println("Why");

}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
运行结果:
4、public int length ( ) 方法
例四:
运行代码如下:
package demo02;
//获得
public class Demo02StringGet {
public static void main(String[] args) {
//1.字符串长度
int length = “zxcvbnmasdfghjkl”.length();
System.out.println(“字符串长度”+length);

	//2.字符串拼接
	String str1 = "zhang";
	String str2 = "yanju";
	String str3 = str1.concat(str2);
	System.out.println(str1);//zhang
	System.out.println(str2);//yanju
	System.out.println(str3);//zhangyanju  重新创建了一个字符串,并不是在原来的基础上进行修改。
	//3.获得指定索引位置的单个字符
	char ch = "Heello".charAt(4);
	System.out.println("0号索引位置的字符是:"+ch);
	
	//查找某个字符串第一次出现的位置
	String origan1= "Helloword";
	int index = origan1.indexOf("llo");
	System.out.println(index);

	System.out.println(origan1.indexOf("Hello"));
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
运行结果:

5、分割字符串

分割字符串
public String[] split(String)
例五:
运行代码如下:
package demo02;
public class Demo03StringSplit {
public static void main(String[] args) {
String str1 = “aa bb cc”;
String[] array1 = str1.split(" ");
for(int i=0;i<array1.length;i++) {
System.out.println(array1[i]);
}
}
}
1
2
3
4
5
6
7
8
9
10
运行结果:

6、SubString 方法

例六:
运行代码如下:
package demo02;
/*

  • public string substring (index)

  • public string substring (int begin , int end)
    */
    public class Demo04SubString {
    public static void main(String[] args) {

     String str1="HelloClass5";
     String str2=str1.substring(5);
     System.out.println(str1);
     System.out.println(str2);
     
     String str3=str1.substring(2,5);
     System.out.println(str3);
    

    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    运行结果:

7、Arrays 类
java.util.Arrays 是一个与数组相关的工具类,里面提供了大量的静态方法。
备注:
如果是数值,默认按升序 从小到大
如果是字符串,sort 默认按字母升序
例七:
运行代码如下
package demo03;
import java.util.Arrays;
public class Demo01Arrays {
public static void main(String[] args) {
int[] intArray = {10,20,30};
System.out.println(intArray);
String intStr = Arrays.toString(intArray);
System.out.println(intStr);

int[] array1 = {1,2,3,4,5};
System.out.println(Arrays.toString(array1));
Arrays.sort(array1);
System.out.println(Arrays.toString(array1));

String[] array2 = {"zzz","xxx","ccc"};
Arrays.sort(array2);
System.out.println(Arrays.toString(array2));

}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
运行结果:

Math
运行代码如下:

package demo03;

public class Demo02Math {
public static void main(String[] args) {
//去绝对值
System.out.println(Math.abs(3.14));
System.out.println(Math.abs(0));
System.out.println(Math.abs(-3.14));
System.out.println("=================");

	//向上取整
	System.out.println(Math.ceil(3.91));
	System.out.println(Math.ceil(3.5));
	System.out.println(Math.ceil(3.0001));
	System.out.println("=================");
	
	//向下取整
	System.out.println(Math.floor(3.91));
	System.out.println(Math.floor(3.5));
	System.out.println(Math.floor(3.0001));
	System.out.println("=================");
	
	System.out.println(Math.round(20.4));
	System.out.println(Math.round(20.5));
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
运行结果:

8、StaticFiled 类

①student
package demo04;

public class Student {
private int id;
private String name;
private int age;

private static String room;

public Student(){
	
}

public Student(String name, int age) {
	super();
	this.name = name;
	this.age = age;
}

public Student(int id, String name, int age, String room) {
	super();
	this.id = id;
	this.name = name;
	this.age = age;
	this.room = room;
}
public int getId() {
	return id;
}

public void setId(int id) {
	this.id = id;
}

public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public static String getRoom() {
	return room;
}

public static void setRoom(String room) {
	Student.room = room;
}

public int getAge() {
	return age;
}

public void setAge(int age) {
	this.age = age;
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package demo04;

  • 如果一个成员变量用static关键字修饰,那么这个内容不再属于对象自己,而是属于类的。所以凡是本类的对象,都共享一份。
    public class Demo01StaticFiled {
    public static void main(String[] args) {
    Student one = new Student(“timi”,19);
    one.setRoom(“3306教室”);
    System.out.println(one.getName()+" “+one.getAge()+” "+one.getRoom());

     Student two = new Student("bg",18);
     System.out.println(two.getName()+"  "+two.getAge()+"  "+two.getRoom());
    

    }
    }
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    运行结果:
    9、StaticMethod 方法
    ①Myclass

package demo04;

public class MyClass {
int num;
static int numstatic;

public void method() {
	System.out.println("这是成员方法");
}
public static void methodStatic() {
	System.out.println("这是静态方法");
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
如果用static关键字修饰成员方法 , 变成了静态方法,静态方法不属于对象,属于类。
可以通过对象来调用 也可以通过类直接调用
package demo04;
public class Demo02StaticMethod {
public static void main(String[] args) {
MyClass obj = new MyClass();
obj.method();//普通方法,必须通过对象类调用

	obj.methodStatic();//可以通过对象类调用      正确 不推荐
	MyClass.methodStatic();//可以通过类直接调用  正确 推荐
}

}
1
2
3
4
5
6
7
8
9
10

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值