String的概述及其构造方法

      package package1;
    //基本类
   class Student{
  public Student(){ 
  }
  }    
  //测试类
  public class StringDemo {
  public static void main(String[] args){

//1.public String();无参构造
String a=new String();
System.out.println(a); //空的
System.out.println(a.length()); //0
System.out.println(“——————-“);
//2.public String(byte[] bytes):将字节数组转换成字符串
byte[] bys={65,66,67,68,69};
String aa=new String(bys);
System.out.println(aa);//ABCDE
System.out.println(aa.length());//5
System.out.println(“———————–”);
//3.public String(byte[]bs,int offset, int length):将字节的数组一部分转化为字符串
byte[] bs={65,66,67,68,69};
String b=new String(bs, 0, 3);
System.out.println(b);//ABC
System.out.println(b.length());//3
System.out.println(“————————-“);
//4.public String(char[]ch):将字符数组转换成字符串
char[]ch={‘a’,’b’,’c’,’d’,’e’,’我’,’爱’,’你’};
String bb=new String(ch);
System.out.println(bb);//abcde我爱你
System.out.println(bb.length());//8
System.out.println(“—————”);
//5.public String(char[]c,int offset,int count):将字符数组中的部分转化成字符串
char[]ch1={‘a’,’b’,’c’,’d’,’e’,’我’,’爱’,’你’};
String c=new String(ch1, 5, 3);
System.out.println(c);//我爱你
System.out.println(c.length());//3
System.out.println(“———”);
//6.public String(String original):将一个常量转换成字符:String是不可变的,所以无需使用构造方法
String cc=new String(“hello”);
System.out.println(cc);//hello
System.out.println(cc.length());//5
System.out.println(“——————”);
String d=”hello”;
System.out.println(d);
}
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值