Loading

10/28-29_String类_SrtingBuffer类_Interger类_笔记

API:应用程序编程接口

String功能
public String replace (char oldchar ,char newchar); //符串中某一字符被一新字符替换
public String replace (String oldstr ,String newstr);//字符串中某一字符串被一新字符串替换
public String trim();//去除字符串两端的空格
public int compareTo(String anotherString);//比较两个字符串

package Test_book;

/**
 * @author Aoman_Hao
 *
 */
public class String1 {
    public static void main(String[] args) {
        String s1="WE today lose. ";

        //字符替换
        String s2=s1.replace('W', 'S');

        //字符串替换
        String s3=s1.replace("WE today", "SSG will");
        System.out.println(s2+"\n"+s3);
    }
}

输出:
SE today lose.
SSG will lose.


String :
字符串常量,字符不可改变,同步,线程安全,效率比较低。

StringBuffer :
(buffer-缓冲区)字符串变量,可变字符序列,同步,线程安全,效率相对String比较高。StringBuffer是一个容器,最终会通过toString方法变成字符串

StringBuilder :
字符串变量,可变字符序列,不同步,线程不安全,效率比StringBuffer高。一般优先选择StringBuilder。

注:String因为其定义新变量时,需要在内存中开辟新空间,比较费资源。而StringBuffer和StringBuilder会产生字符缓冲区,字符缓冲区不占内存。

 线程安全与不安全:
 概念:如果你的代码所在的进程中有多个线程在同时运行,而这些线程可能会同时运行这段代码。如果每次运行结果和单线程运行的结果是一样的,而且其他的变量的值也和预期的是一样的,就是线程安全的。
 线程安全的程序,多进程需要访问数据时,只有某个单进程才能更改数据,其他的进程需要等待次进程的结束,然后才能访问或者更改数据。

 经常改变内容的字符串最好不要用String,因为新生成的字符串占据珍贵内存空间,旧字符串成为无引用对象后,JVM的GC的工作量加大,会降低运行速度。

对于三者使用的总结: 
1.如果要操作少量的数据用 = String
2.单线程操作字符串缓冲区 下操作大量数据 = StringBuilder
3.多线程操作字符串缓冲区 下操作大量数据 = StringBuffer

StringBuffer:
public int length;//字符串长度
public int capacity;//获取当前字符串缓冲区的容量,capacity默认容量为16, //小于16则不需扩展,如果大于16则会直接扩展到旧的容量
//(value的长度)*2+2
append (String str);//给缓冲区追加数据
insert (int offset,String str);//插入字符串

package Test_book;

/**
 * @author Aoman_Hao
 *
 */
public class String2 {

    public static void main(String[] args) {
        StringBuffer s1= new StringBuffer("SKT will win!");
        //长度
        int num1=s1.length();
        System.out.println(num1);

        //一般默认缓冲区大小为16  
        StringBuffer sb1 = new StringBuffer();
        System.out.println("sb1的缓冲区容量:"+sb1.capacity());

        //现设置缓冲区容量大小为25
        StringBuffer sb2 = new StringBuffer(30);
        System.out.println("sb2的缓冲区容量:"+sb2.capacity());

        //缓冲区追加字符
        StringBuffer sb3 = s1.append("LOL");
        System.out.println("追加:"+sb3.toString());

        //插入字符串
        StringBuffer sb4 = s1.insert(4,"not SSG ");
        System.out.println("插入:"+sb4.toString());
    }

}

输出:
13
sb1的缓冲区容量:16
sb2的缓冲区容量:30
追加:SKT will win!LOL
插入:SKT not SSG will win!LOL


StringBuffer:
delete (int start,int end);//删除指定位置区域的数据,但end位置处的数据不删除
deleteCharAt(int index);//删除指定位置处的字符
reverse();//字符串中的字符序列反转
toString();//字符缓冲区的数据转换为String类型

package Test_book;

/**
 * @author Aoman_Hao
 *
 */
public class String3 {

    public static void main(String[] args) {
        StringBuffer s1 = new StringBuffer("SKT 123 will win!LOL");

        //删除指定位置的字符
        StringBuffer s2 = s1.deleteCharAt(4);
        System.out.println("删除指定位置字符:"+s2);

        //删除指定位置1及指定位置1与指定位置2之间的字符,指定位置2的字符不删除
        StringBuffer s3 = s1.delete(4, 7);
        System.out.println("删除范围字符:"+s3);

        //反转字符
        StringBuffer s4 = s1.reverse();
        System.out.println("反转:"+s4);

        //字符缓冲区的数据转换为String类
        String s5 =s1.toString();
        System.out.println("转换为String类:"+s5);
    }

输出:
删除指定位置字符:SKT 23 will win!LOL
删除范围字符:SKT will win!LOL
反转:LOL!niw lliw TKS
转换为String类:LOL!niw lliw TKS

注:有以上结果可以看出String类在内存中的数据再引用给其他变量时,会新生成数据;StringBuffer类在字符缓冲区的数据被访问后,直接在数据上进行修改然后把地址符赋予新变量,因为旧变量与新变量的地址指向都是同一个数据,所以旧变量的值在数据修改之后与新变量是一样的。


StringBuffer:
replace (int start,int end,string str);//替换字符串
substring (int strat);//截取字符串
substring (int start,int end);

package Test_book;

/**
 * @author Aoman_Hao
 *
 */
public class Stringbuffer2 {
    public static void main(String[] args){
    StringBuffer s1 = new StringBuffer("Aoman 123");

    //替换
    StringBuffer s2= s1.replace(6, 9, "666!");
    System.out.println("替换:"+s2);

    //截取
    String s3 = s2.substring(0,8);
    System.out.println("截取:"+s3);
    //
    String s4 = s2.substring(0,s2.length()-1).toString();
    System.out.println("截取:"+s4);
    }
}

输出:
替换:Aoman 666!
截取:Aoman 66
截取:Aoman 666


Interger:
把基本数据类型自动封装成引用类型,为了和Sring类做转换。
基本类型–引用类型
int – Integer
char – Character
byte – Byte
boolean –Boolean
double – Double
float – Float
long – Long
short – Short


Integer(int value)int类型数据封装为引用类型
Integer (String str) 字符串类型封装为引用类型
注:字符串仅为纯数字字符串,否则会运行报错。

package Test_book;

/**
 * @author Aoman_Hao
 *
 */
public class Integer1 {

    public static void main(String[] args) {
        Integer s1 = (666);
        Integer s2 = new Integer("123666");
        System.out.println(s1);
        System.out.println(s2);

    }

}

输出:
666
123666


int类型与String类型相互转换:
int转换为String类型

package Test_book;
/**
 * @author Aoman_Hao
 *
 */
public class Integer2 {

    public static void main(String[] args) {

        //字符串拼接
        String s1 = ""+num;
        System.out.println(s1);

        //用Integer类型引用
        Integer s2 = new Integer(num);
        String s3 = s2.toString();
        System.out.println(s3);

        //链式
        String s4 = Integer.toString(num);
        System.out.println(s4);
    }

}

Integer转换为int类型

package Test_book;
/**
 * @author Aoman_Hao
 */
public class Integer3 {

    public static void main(String[] args) {
        String s1 = "7421666";

        //int值返回Integer类型值
        Integer s2 = new Integer(s1);
        int num = s2.intValue();
        System.out.println(num);

        //parseint方法
        int num2=Integer.parseInt(s1);
        System.out.println(num2);
    }

}

Character类:
character(char value);//在对象中包装一个基本类型char的值
//character类型对象包含类型为char的单字符
Character类:
public static boolean isLowerCase (char ch);判断指定字符是否为小写
public static boolean isUpperCase (char ch);判断指定字符是否为大写
public static boolean isDigit (char ch);判断指定字符是否为数字

package Test_book;

import java.util.Scanner;

/**
 * @author Aoman_Hao
 */
public class Character1 {

    public static void main(String[] args) {
        //键盘录入数据
        int Lnum=0,Unum=0,Dnum=0;
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一行字母、数字:");
        String s1 = sc.nextLine();
        char[] arr = s1.toCharArray();
        //判断
        for(int i=0;i<s1.length();i++){
            char arr1 = arr[i];
            if(Character.isLowerCase(arr1)){
                Lnum ++;
            }else if(Character.isUpperCase(arr1)){
                Unum ++;
            }else if(Character.isDigit(arr1)){
                Dnum ++;
            }
        }
        System.out.println("小写字母数:"+Lnum);
        System.out.println("大写字母数:"+Unum);
        System.out.println("数字数目d:"+Dnum);
    }

}

输出:
请输入一行字母、数字:
afafgd234hbfghbdfaGSDFSA
小写字母数:15
大写字母数:6
数字数目:3

posted @ 2017-10-29 21:28  AomanHao  阅读(17)  评论(0)    收藏  举报