StringBuffer:
线程安全的可变字符串。

StringBuffer和String的区别?
前者长度和内容可变,后者不可变。
如果使用前者做字符串的拼接,不会浪费太多的资源。

StringBuffer的构造方法:
public StringBuffei():无参构造方法
public StringBuffer(int capacity):指定容量的字符串缓冲区对象
public StringBuffer(String str):指定字符串内容的字符串缓冲对象

StringBuffer的方法:
public int capacity():返回当前容量。 理论值
public int length():返回长度(字符数)。 实际值

public static void main(String[] args) {
//public StringBuffei():无参构造方法
StringBuffer sb = new StringBuffer();
System.out.println("sb:"+sb);//sb:
System.out.println("sb.capacity():"+sb.capacity());//16
System.out.println("sb.length():"+sb.length());//0

//public StringBuffer(int capacity):指定容量的字符串缓冲区对象
StringBuffer sb2 = new StringBuffer(50);
System.out.println("sb2:"+sb2);//sb2:
System.out.println("sb2.capacity():"+sb2.capacity());//50
System.out.println("sb2.length():"+sb2.length());//0

//public StringBuffer(String str):指定 字符串内容的字符串缓冲对象
StringBuffer sb3 = new StringBuffer("hello");
System.out.println("sb3:"+sb3);//sb3:hello
System.out.println("sb3.capacity():"+sb3.capacity());//21
System.out.println("sb3.length():"+sb3.length());//5
}

/**StringBuffer的添加功能:*/
//public StringBuffer append(String str):可以把任意类型添加到字符串缓冲区里面

public static void main(String[] args) {
//创建字符串缓冲区对象
StringBuffer sb = new StringBuffer();

//public StringBuffer append(String str)
StringBuffer sb2 = sb.append("hello");
System.out.println("sb:"+sb);//hello
System.out.println("sb2:"+sb2);//hello
System.out.println(sb == sb2);//true

//使用append一步一步的添加数据
sb.append("hello");
sb.append("true");
sb.append("12");
sb.append("34.56");
System.out.println(sb);//hellohellotrue1234.56

//使用链式编程
sb.append("hello").append("true").append("12").append("34.56");
System.out.println(sb);//hellohellotrue1234.56

//public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
sb.insert(5,"world");
System.out.println(sb);//helloworldhellotrue1234.56
sb.insert(2,"world");
System.out.println(sb);//heworldllohellotrue1234.56
}

/**StringBuffer的删除功能*/
//public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
//public StringBuffer delete(int start,int end):删除从指定位置开始到指定位置结束的功能,并返回本身

public static void main(String[] args) {
//创建对象
StringBuffer sb = new StringBuffer();

//先对空的sb进行添加
sb.append("hello").append("world").append("java");
System.out.println("sb:"+sb);//helloworldjava

//public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
//需求:删除e字符
sb.deleteCharAt(1);//hlloworldjava
//如果这个时候我还要删除第一个l 因为值已经变成了hlloworldjava 所以 还是索引值还是1
sb.deleteCharAt(1);//hloworldjava

//使用public StringBuffer delete(int start,int end):删除从指定位置开始到指定位置结束的功能,并返回本身
StringBuffer sb = new StringBuffer("helloworldjava");
sb.delete(5,10);//hellojava //包左不包右
sb.delete(0,sb.length());//没有值 都删除了
}
/**StringBuffer的替换功能*/
//public StringBuffer replace(int start,int end,String str):从start开始到end用str替换

public static void main(String[] args) {
//创建字符串缓冲区对象
StringBuffer sb = new StringBuffer();

//添加数据
sb.append("hello");
sb.append("world");
sb.append("java");

//public StringBuffer replace(int start,int end,String str):从start开始到end用str替换
//需求:我要将world替换为"节日快乐"
sb.replace(5,10,"节日快乐");//hello节日快乐java
}
/**StringBuffer的反转功能*/
//public StringBuffer reverse():反转字符串

public static void main(String[] args) {
//创建字符串缓冲区对象
StringBuffer sb = new StringBuffer();

//添加数据
sb.append("我爱java,java不爱我"):
System.out.println(sb);//我爱java,java不爱我

//public StringBuffer reverse()
sb.reverse();
System.out.println(sb);//我爱不avaj,avaj爱我
}
/**StringBuffer的截取功能*/
//public String substring(int start):输入索引 索引之前的元素将会被截取调 索引本身不会
//public String substring(int start,int end):截取索引之间的数,包左不包右 这两个都不会改变原来的值
public static void main(String[] args) {
//创建字符串缓冲区对象
StringBuffer sb = new StringBuffer();
System.out.println(sb);//helloworldjava

//添加数据
sb.append("hello").append("world").append("java");

//截取功能
//public String substring(int start)
String s = sb.substring(5);
System.out.println(s);//worldjava
System.out.println(sb);//helloworldjava
}
public static void main(String[] args) {
//创建字符串缓冲区对象
StringBuffer sb = new StringBuffer();
System.out.println(sb);//helloworldjava

//添加数据
sb.append("hello").append("world").append("java");

//截取功能
//public String substring(int start,int end)
String s = sb.substring(5,10);
System.out.println(s);//world
System.out.println(sb);//helloworldjava
}

/**StringBuffer和String的相互转换*/
//那我们为什么要用类之间的转换呢?
A -- B的转换
我们把A转换为B,其实是为了使用B的功能。
B -- A的转换
我们可能要的结果是A类型,所以还得转回来。

public static void main(String[] args) {
//String -- StringBuffer
String s = "hello";

//注意:不能把字符串的值直接赋值给StringBuffer
//StringBuffer sb = "hello";
//StringBuffer sb = s;//应该用下面两种方式

//方式1:通过构造方法
StringBuffer sb = new StringBuffer(s);
//方式2:通过append()方法:
StringBuffer sb2 = new StringBuffer();
sb2.append(s);

System.out.println(sb);//hello
System.out.println(sb2);//hello
//StringBuffer -- String
StringBuffer buffer = new StringBuffer("java");

//将StringBuffer类型的buffer转换为String类型有下面两种方法
//String(StringBuffer buffer)
//方式1:通过构造方法
String str = new String(buffer);
//方式2:通过toString()方法
String str2 = buffer.toString();
System.out.println(str);//java
System.out.println(str2);//java
}

StringBuffer的面试题:

1.//String,StringBuffer,StringBuilder的区别?
答:A:String是内容是不可变的,而StringBuffer,StringBuilder都是内容可变的。
B:StringBuffer是同步的,数据安全,效率低,StringBuilder是不同步的,数据不安全,效率高。

2.//StringBuffer和数组的区别?
答:二者都可以看出是一个容器,装其他的数据。
但是,StringBuffer的数据最终是一个字符串数据。
而数组可以安置多种数据,但必须是同一种类型。

3.//形式参数问题 String作为参数传递 StringBuffer作为参数传递?
形式参数:
基本类型:形式参数的改变不影响实际参数
引用类型:形式参数的改变直接影响实际参数
注意:
String作为参数传递,效果和基本类型作为参数传递是一样的。

代码:
public static void main(String[] args) {
String s1 = "hello";
String s2 = "world";
System.out.println(s1 + "---" + s2);// hello world
change(s1, s2);
System.out.println(s1 + "---" + s2);// hello world

StringBuffer sb1 = new StringBuffer("hello");
StringBuffer sb2 = new StringBuffer("world");
System.out.println(sb1 + "---" + sb2);// hello world
change(sb1, sb2);
System.out.println(sb1 + "---" + sb2);// hello worldworld

}

private static void change(StringBuffer sb1, StringBuffer sb2) {
sb1 = sb2;
sb2.append(sb1);
}

private static void change(String s1, String s2) {
s1 = s2;
s2 = s1 + s2;
}

/**字符串判断对称功能的案例*/
public class Atest {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数据");
String a = sc.nextLine();

boolean falg = isScme(a);
System.out.println(falg);
}

public static boolean isScme(String a) {
char[] arr = a.toCharArray();
for (int start = 0, end = arr.length - 1; start <= end; start++, end--) {
if (arr[start] != arr[end]) {
return false;
}
}
return true;
}
}
改进:
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入数据");
String a = sc.nextLine();

boolean falg = isScme(a);
System.out.println(falg);
}

public static boolean isScme(String a) {

return new StringBuffer(a).reverse().toString().equals(a);

}

Java中StringBuffer类的更多相关文章

  1. JAVA中StringBuffer类常用方法详解

    String是不变类,用String修改字符串会新建一个String对象,如果频繁的修改,将会产生很多的String对象,开销很大.因此java提供了一个StringBuffer类,这个类在修改字符串 ...

  2. JAVA中StringBuffer类常用方法

    String是不变类,用String修改字符串会新建一个String对象,如果频繁的修改,将会产生很多的String对象,开销很大.因此java提供了一个StringBuffer类,这个类在修改字符串 ...

  3. Java中StringBuffer类的常用方法

    StringBuffer:StringBuffer类型 描述:在实际应用中,经常回遇到对字符串进行动态修改.这时候,String类的功能受到限制,而StringBuffer类可以完成字符串的动态添加. ...

  4. Java中StringBuffer类append方法的使用

    public static void testAppend() { StringBuffer sb = new StringBuffer("This is a StringBuffer!&q ...

  5. 【转】JAVA的StringBuffer类

    [转]JAVA的StringBuffer类    StringBuffer类和String一样,也用来代表字符串,只是由于StringBuffer的内部实现方式和String不同,所以StringBu ...

  6. Java基础-StringBuffer类与StringBuilder类简介

    Java基础-StringBuffer类与StringBuilder类简介 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.StringBuffer类 在学习过String类之后 ...

  7. 在java中String类为什么要设计成final?

    大神链接:在java中String类为什么要设计成final? - 程序员 - 知乎 我进行了重新排版,并且更换了其中的一个例子,让我们更好理解. String很多实用的特性,比如说“不可变性”,是工 ...

  8. Java中String类为什么被设计为final?

    Java中String类为什么被设计为final   首先,String是引用类型,也就是每个字符串都是一个String实例.通过源码可以看到String底层维护了一个byte数组:private f ...

  9. 基础知识(05) -- Java中的类

    Java中的类 1.类的概念 2.类中的封装 3.对象的三大特征 4.对象状态 5.类与类之间的关系 ------------------------------------------------- ...

随机推荐

  1. VBScript 打开含有"空格"的路径 (Open Path with Space)

    记录,VBScript 如何打开,含有"空格"的路径.这个问题和常见,却总是忘! 直接上代码了,多说无益. Option Explicit Dim obj Dim path Set ...

  2. 模块 shutil_zipfile_tarfile压缩解压

    shutil_zipfile_tarfile压缩解压 shutil 模块 高级的 文件.文件夹.压缩包 处理模块 shutil.copyfileobj(fsrc, fdst[, length]) #将 ...

  3. [vijos]1066弱弱的战壕<线段树>

    题目链接:https://www.vijos.org/p/1066 这道题没什么难度,只是要一个排序然后就是线段树的基本套路模版了 但是我还是讲一讲思路吧: 给出的是坐标x,y,当一个点的x,y都小于 ...

  4. Javascript/Jquery实现日期前一天后一天

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/stri ...

  5. SQL Server 创建链接服务器的脚本,自定义链路服务器的简短名称

    USE [master]GO /****** Object:  LinkedServer [SQL01]    Script Date: 2020/4/9 11:51:17 ******/EXEC m ...

  6. 使用IDEA创建SpringBoot项目

    SpringBoot学习第一步:搭建基础 IDEA对SpringBoot的项目支持可以说是点击就能完成基础的搭建,方便的不得了, 流程如下 1.左上角File选项,New project,选择Spri ...

  7. python:用cv2简单实现图片的水平、垂直翻转

    原图片的本地地址:D:/360Downloads/test.jpg 代码实现: # 导入cv2模块 import cv2 # 给出本地图片的地址 img_dir="D:/360Downloa ...

  8. Jmeter 压力测试笔记(2)--问题定位

    事情已经出了,是该想办法解决的时候了. 经过运维和DBA定位: 数据库读写分离中,读库延时超过了30秒,导致所有请求都压在主库.另外所有数据库都连接数都被占满,但活跃请求数量缺不多. 数据库16K的连 ...

  9. Git常用命令(一)

    $ git init 初始化仓库(会生成一个隐藏文件.git) $ git add + (文件名) 实现对指定文件的跟踪 $ git commit 提交更新$ git clone + URL 克隆远程 ...

  10. MTK Android 读取SIM卡参数,获取sim卡运营商信息

    android 获取sim卡运营商信息(转)   TelephonyManager tm = (TelephonyManager)Context.getSystemService(Context.TE ...