17天
一、
String类代表字符串,java程序中的所有字符串字面值(如"abc")都作为此类实例实现
就是说"abc"都是String类的对象
字符串特点:
1、字符串内容不变
2、字符串可以共享使用
字符串效果相当于一个char[],但是实际底层储byte[]
常用的三种构造方法
1、public String(),创建一个空白字符串,不包含任何内容
2、public String(char[] array)根据字符数组创建字符串
3、public String(byte[] array)根据字节数组创建字符串
最直接的方式:
String str=“class5”;
二、
/*分割字符串
public String[] split (String)
/
三、
/
java.util.Arrays 是一个与数组相关的工具类,里面提供了大量的静态方法
备注:
如果是数值,默认按升序排列 从小到大
如果是字符串,
*/
四、
/*如果用static修饰成员方法,变成静态方法,静态方法不属于对象,属于类
可以通过对象调用
也可以直接调用
/
四、
/
如果一个成员变量用static关键字修饰,那么这个变量不属于对象,而属于所在的类 多个对象共享同一份数据
代码:package Demo01;
/*
*
-
String类代表字符串,java程序中的所有字符串字面值(如"abc")都作为此类实例实现
-
就是说"abc"都是String类的对象
-
字符串特点:
-
1、字符串内容不变
-
2、字符串可以共享使用
-
字符串效果相当于一个char[],但是实际底层储byte[]
-
常用的三种构造方法
-
1、public String(),创建一个空白字符串,不包含任何内容
-
2、public String(char[] array)根据字符数组创建字符串
-
3、public String(byte[] array)根据字节数组创建字符串
-
最直接的方式:
-
String str=“class5”;
*/
public class Demo01String {
public static void main(String[] args) {
String str1=new String();//小括号留空,没内容
System.out.println(“第一个字符串:”+str1);System.out.println("================");
char[] chararray= {‘a’,‘b’,‘c’,‘d’};
System.out.println(chararray);
String str2=new String(chararray);
System.out.println(str2);System.out.println("");
byte[] bytearray= {97,98,99};
System.out.println(bytearray);
String str3=new String(bytearray);
System.out.println(str3);
System.out.println("");
String str4=“class5”;
System.out.println(str4);
}
}
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(str1str2);
System.out.println(str1str3);
System.out.println(str2==str3);
str2=“cde”;
System.out.println(str2);
}
}
第2个:
package Demo02;
public class Demo02StringEduals {
public static void main(String[] args) {
String str1=“Hello”;
String str2=“Hello”;
System.out.println(str2);
char[] chararray= {‘H’,‘e’,‘l’,‘l’,‘o’};
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("Hello"));
System.out.println("Hello".equals(str1));
System.out.println("===============");
String str4=null;
System.out.println("Hello".equals(str4));
System.out.println("===============");
String str5="hello";
System.out.println("Hello".equals(str5));
System.out.println("===============");
System.out.println("Hello".equalsIgnoreCase(str5));
}
}
package Demo02;
public class Demo02StringGet {
public static void main(String[] args) {
//1字符串长度
int length="abhjbcwiwqndjiqunfq".length();
System.out.println("字符串长度"+length);
//2字符串拼接
String str1="Hello";
String str2="class5";
String str3=str1.concat(str2);
System.out.println(str1);//Hello
System.out.println(str2);//class5
System.out.println(str3);//Helloclass5 重新创建了一个字符串
//3获得索引位置的单个字符串
char ch="Hello".charAt(4);
System.out.println("0号索引位置字符是:"+ch);
//查找某个字符串第一次出现的位置
String origanl="Helloworld";
int index=origanl.indexOf("llo");
System.out.println(index);
System.out.println(origanl.indexOf("class"));
}
}
package Demo02;
/*分割字符串
- public String[] split (String)
*/
public class Demo02StringSplit {
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]);
}
}
}
package Demo02;
public class Demo03SubString {
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);
}
}
第3个:
package Demo03;
import java.util.Arrays;
/*
- java.util.Arrays 是一个与数组相关的工具类,里面提供了大量的静态方法
- 备注:
- 如果是数值,默认按升序排列 从小到大
- 如果是字符串,
*/
public class demo03Arrays {
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= {3,4,5,6,7,8,9,1,23};
System.out.println(Arrays.toString(array1));
Arrays.sort(array1);
System.out.println(Arrays.toString(array1));
String[] array2= {"ddd","aaa","kkk"};
Arrays.sort(array2);
System.out.println(Arrays.toString(array2));
}
}
package Demo03;
public class demo03Math {
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.0003));
System.out.println("============");
//向下取整
System.out.println(Math.ceil(3.91));
System.out.println(Math.ceil(3.5));
System.out.println(Math.ceil(3.0003));
System.out.println("============");
System.out.println(Math.round(20.4));
System.out.println(Math.round(20.5));
}
}
第4个:
package demo04;
public class demo04StaticFiled {
public static void main(String[] args) {
Student one=new Student(“郭靖”,19);
one.setRoom(“3306教室”);
System.out.println(one.getName()+" “+one.getAge()+” “+one.getRoom());
Student two=new Student(“黄蓉”,16);//没有设置黄蓉的教室
System.out.println(two.getName()+” “+two.getAge()+” "+two.getRoom());
}
}
package demo04;
/*如果用static修饰成员方法,变成静态方法,静态方法不属于对象,属于类
- 可以通过对象调用
- 也可以直接调用
*/
public class demo04StsticMethod {
public static void main(String[] args) {
MyClass obj=new MyClass();
obj.method();
obj.methodStatic();
MyClass.methodStatic();
}
}
package demo04;
public class MyClass {
int num;
static int numstatic;
public void method() {
System.out.println(“这是成员方法”);
}
public static void methodStatic() {
System.out.println("这是静态方法");
}
}
package demo04;
/*
- 如果一个成员变量用static关键字修饰,那么这个变量不属于对象,而属于所在的类 多个对象共享同一份数据
*/
public class Student {
private int id;
private int name;
private int age;
private String room;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public int getName() {
return name;
}
public void setName(int name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getRoom() {
return room;
}
public void setRoom(String room) {
this.room = room;
}
public Student(String string, int i) {
super();
}
public Student(int name, int age) {
super();
this.name = name;
this.age = age;
}
}
学号:2020080605015