String—StringBuffer、StringBuilder、基本数类型

 

[html]  view plain copy
  1. /*  
  2. StringBuffer是字符串缓冲区。是一个容器。  
  3. 特点:  
  4. 1,而且长度是可变化的。  
  5. 2,可以直接操作多个数据类型。  
  6. 3,最终会通过toString方法变成字符串。  
  7.   
  8. C create U update R read D delete  
  9.   
  10. 1,存储  
  11.      StringBuffer append(); 将指定数据添加到已有数据末尾处  
  12.      StringBuffer insert(index,数据);可以将数据插入到指定index位置  
  13.   
  14. 2,删除  
  15.      StringBuffer delete(start, end);删除缓冲区中数据,包含start,不包含end  
  16.      StringBuffer deletCharAt(index);删除指定位置字符  
  17.   
  18. 3,获取  
  19.      char charAt(int index);  
  20.      int indexOf(String str)  
  21.      int lastIndexOf(String str)  
  22.      int length()  
  23.      String substring(int start, int end);  
  24.   
  25. 4,修改  
  26.      StringBuffer replace(start,end,string);  
  27.      void setCharAt(int index, char ch);  
  28.        
  29. 5,反转  
  30.      StringBuffer reverse();  
  31.        
  32. 6,  
  33.      将缓冲区中的数据存储到指定字符数组中  
  34.      void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin);  
  35.   
  36. JDK1.5版本之后出现了StringBuilder.  
  37. StringBuffer是线程同步。安全,多线程使用  
  38. StringBuilder是线程不同步,多线程不安全,单线程效率高  
  39.   
  40. 以后开发建议使用StringBuilder  
  41.   
  42. 升级三个因素:  
  43. 1,提高效率  
  44. 2,提高安全性  
  45. 3,简化书写  
  46. */  
  47.   
  48. class StringBufferDemo  
  49. {     
  50.     public static void main(String[] args)  
  51.     {  
  52.         //method_del();  
  53.         //method_update();  
  54.         StringBuffer sb = new StringBuffer("abcdef");  
  55.           
  56.         char[] chs = new char[4];  
  57.           
  58.         sb.getChars(1,4,chs,1);  
  59.           
  60.         for(int x=0; x<chs.length; x++)  
  61.         {  
  62.             sop("char["+x+"]="+chs[x]);   
  63.         }  
  64.     }     
  65.       
  66.     public static void method_update()  
  67.     {  
  68.         StringBuffer sb = new StringBuffer("abcde");  
  69.         //sb.replace(1,4,"java");     
  70.         sb.setCharAt(2,'k');  
  71.           
  72.         sop(sb.toString());  
  73.     }  
  74.       
  75.     public static void method_del()  
  76.     {  
  77.             StringBuffer sb = new StringBuffer("abcde");  
  78.             //sb.delete(1,3);  
  79.             //sb.delete(0,sb.length());//清空缓冲区  
  80.             //sb.delete(2,3);  
  81.             sb.deleteCharAt(2);  
  82.             sop(sb.toString());  
  83.     }  
  84.       
  85.     public static void method_add()  
  86.     {  
  87.         StringBuffer sb = new StringBuffer();  
  88.           
  89.         sb.append("abc").append(true).append(34);  
  90.           
  91. //      StringBuffer sb1 = sb.append(34);  
  92. //      sop("sb==sb1: "+(sb==sb1));//true  
  93.   
  94.         sb.insert(1,"qq");  
  95.         sop(sb.toString());  
  96. //      sop(sb1.toString());  
  97.                   
  98.     }  
  99.     public static void sop(String str)  
  100.     {  
  101.         System.out.println(str);      
  102.     }  
  103. }  

------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[html]  view plain copy
  1. /*  
  2. 基本数据类型对象包装类。  
  3.   
  4. byte            Byte  
  5. short           Short  
  6. int             Integer  
  7. long            Long  
  8. boolean         Boolean  
  9. float           Float  
  10. double          Double  
  11. char            Character  
  12.   
  13. 基本数据类型对象包装类的最常见作用,  
  14. 就是用于基本数据类型和字符串类型之间做转换。  
  15.   
  16. 基本数据类型转成字符串  
  17. 1,基本数据类型+""  
  18. 2,基本数据类型.toString(基本数据类型值);  
  19.      如:Integer.toString(34);//将34整数变成"34"  
  20.   
  21. 字符串转成基本数据类型  
  22.  xxx a = Xxx.parseXxx(String);  
  23.  int a = Integer.parseInt("123");  
  24.  double b = Double.parseDouble("12.34");  
  25.    
  26.  boolean b = Boolean.parseBoolean("true");  
  27.    
  28.  Integer i = new Integer("123");//非静态的  
  29.  int num = i.intValue();  
  30.    
  31. 十进制转成其他进制  
  32.     toBinaryString();  
  33.     toHexString();  
  34.     toOctalString();  
  35.   
  36. 其他进制转成十进制  
  37.   parseInt(string,radix);  
  38.   int x = Integer.parseInt("3c",16);  
  39.   
  40. */  
  41. class IntegerDemo  
  42. {  
  43.     public static void sop(String str)  
  44.     {  
  45.         System.out.println(str);      
  46.     }     
  47.     public static void main(String[] args)  
  48.     {  
  49.         //sop("int max: "+Integer.MAX_VALUE);//整数类型的最大值  
  50.           
  51.         //将一个字符串转成整数。  
  52.         //int num = Integer.parseInt("123");//必须传入数字格式的字符串  
  53.         //long x = Long.parseLong("123");  
  54.           
  55.         //sop("num="+(num+4));  
  56.           
  57.         //sop(Integer.toBinaryString(6));  
  58.         //sop(Integer.toHexString(60));  
  59.           
  60.         int x = Integer.parseInt("3c",16);  
  61.         sop("x="+x);  
  62.     }  
  63. }  

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

[html]  view plain copy
  1. /*  
  2. JDK1.5 版本的新特性  
  3. */  
  4.   
  5. class IntegerDemo1  
  6. {  
  7.     public static void main(String[] args)  
  8.     {  
  9.         Integer x = 4;// 自动装箱  
  10.           
  11.         x = x + 2;//x+2:自动拆箱,调用x.intValue(),变成int类型,与2进行运算  
  12.                             //再将和装箱赋给x  
  13.         sop(x);  
  14.           
  15.         Integer m = 128;  
  16.         Integer n = 128;  
  17.         sop("m==n: "+(m==n));//false  
  18.           
  19.         Integer a = 127;  
  20.         Integer b = 127;  
  21.           
  22.         sop("a==b:"+(a==b));//true,因为a和b指向了同一个Integer对象。  
  23.         //因为当数值在byte范围内,对于新特性,如果该数值已经存在,就不会开辟新空间  
  24.     }  
  25.       
  26.     public static void method()  
  27.     {  
  28.         Integer x = new Integer("123");  
  29.           
  30.         Integer y = new Integer(123);  
  31.           
  32.         sop("x==y: "+(x==y));//false  
  33.         sop("x.equals(y): "+x.equals(y));//true  
  34.     }  
  35.     public static void sop(Object str)  
  36.     {  
  37.         System.out.println(str);      
  38.     }  
  39. }  

-

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值