Chinaunix首页 | 论坛 | 博客
  • 博客访问: 535616
  • 博文数量: 151
  • 博客积分: 7010
  • 博客等级: 少将
  • 技术积分: 1405
  • 用 户 组: 普通用户
  • 注册时间: 2008-04-22 14:32
文章分类

全部博文(151)

文章存档

2011年(1)

2010年(23)

2009年(1)

2008年(126)

我的朋友

分类: Java

2008-05-12 16:48:10

一、String类对象中的内容一旦被初始化就不能再改变
二、StringBuffer类用于封装内容可以改变的字符串
    1、用toString方法转换成String类型
    2、String x="e" + "343" 编译时等效于:
       String= new StringBuffer().append("e").append("343").toString();
三、字符串常量(如"hello world")实际上是一中特殊的匿名String对象。
    1、String s1= "hello world";
       String s2= "hello world";
       s1和s2实际上是指向同一个对象,也就是s1=s2;
    2、String s1=new String("hello world"); String s2=new String("hello world");
       这里是创建了两个"hello world"对象,所以s1<>s2
四、String类的常用成员方法
    1、构造方法:
       String(byte[] bytes,int offset,int length);
    2、equalsIgnorCase(String anotherString);
    3、indexOf(int ch)
    4、subString(int beginIndex);
       subString(int beginIndex,int endIndex);
五、在开发中推荐使用StringBuffer,因为String类型在进行字符串合并等操作时要先转换成StringBuffer类型,所以在效率上相差很大,明显不如StringBuffer。举个例子
public class TestString
{
 public static void main(String[] args)
 {
  long currentDate = new Date().getTime();
  String sb = new String();
  for (int j = 0; j < 20000; j++)
  {
   sb = sb + "*";
  }
  //System.out.println(sb);
  long pastDate = new Date().getTime();
  long interval = pastDate - currentDate;
  System.out.println("String运行时间(ms):"+ interval);
 
  currentDate = new Date().getTime();
  StringBuffer sb2 = new StringBuffer();
  for (int j = 0; j < 20000; j++)
  {
   sb2.append("*");
  }
  //System.out.println(sb2);
  pastDate = new Date().getTime();
  interval = pastDate- currentDate;
  System.out.println("StringBuffer运行时间(ms):"+ interval);
 }
}
 
运行结果如下:
String运行时间(ms):843
StringBuffer运行时间(ms):0
阅读(655) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~