第八章 java常用类库

来源:互联网 发布:广告位管理系统源码 编辑:程序博客网 时间:2024/05/17 08:23

8.3 String 类型有什么特点?

String不是基本数据类型,而是一个类,它被用来表示字符序列;

String的特点是一旦赋值,便不能更改其指向的字符对象。如果更改,则会指向一个新的字符对象。

8.4

public class StringEqualTest {  public static void main(String[] args){    String s = new String("Hello");     String t = new String("Hello");    if (s==t){       System.out.println("相等");    }    else{       System.out.println("不相等");    }  }}
public class StringEqualTest1{  public static void main(String[] args){    String s = new String("Hello");     String t = new String("Hello");    if (s.equals(t)){       System.out.println("相等");    }    else{       System.out.println("不相等");    }  }}



8.5

String一旦赋值,便不能更改其指向的字符对象,而StringBuffer对象可以调用其方法动态地进行增加,插入和删除操作。

StringBuffer的构造方法可将一个String对象转化为StringBuffer,而其方法toString()可将一个StringBuffer转化成一个String对象。