String类对象实例化
直接赋值的形式进行实例化操作
在Java中并没有字符串这一数据类型的存在,考虑到开发的需求,Java通过设计的形式提供了String类,并且该类的对象可以通过直接赋值的形式进行实例化操作。
public class StringDemo {
public static void main(String[] args) {
String str ="csdn.去扰嶟";
System.out.println(str);
}
}
使用关键字和new来实例化String类对象
String类的构造方法共有十一种
public class StringDemo {
public static void main(String[] args) {
String str =new String("csdn.去扰嶟");
System.out.println(str);
}
}
两种方法赋值的区别:
直接赋值的字符串存储在常量池中,而new创建的字符串对象在指向堆中的地址。
连接字符串
String类提供了两种连接两个字符串的方法:
第一种使用concat方法:
string1.concat(string2);
第二种使用’+'操作符来连接字符串:
sting1 = "csdn." + "去扰嶟";
连接结果:
string1 = "csdn.去扰嶟";