2021-04-30

day17

Object类的概述
Object是类层次结构的根,每个类都可以将Object作为超类。所有类都直接或者间接的继承自该类

构造方法: public Object()

回想面向对象中,为什么说子类的构造方法默认访问的是父类的无参构造方法?
因为它们的顶级父类只有无参构造方法

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=“class”;
package demo01;

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("str1
str3”);
System.out.println(“str2==str3”);
str2=“cde”;
System.out.println(“str2”);
}

}
package demo02;

public class Demo02StringEquals {
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) {
int length=“agegbagfdygdszdgagdygbsdhdshd”.length();
System.out.println(“字符串长度”+length);

	String str1="hello";
	String str2="class5";
	String str3=str1.concat(str2);
	System.out.println(str1);
	System.out.println(str2);
	System.out.println(str3);
	char ch="hello".charAt(4);
	System.out.println("0号索引位置的字符是:"+ch);
	String origanl="helloworldhelloworldhelloworld";
	int index=origanl.indexOf("llo");
	System.out.println(index);
	System.out.println(origanl.indexOf("class"));
}

}
package demo02;

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 Demo02SubString {
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);
}
}
在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值