StringBuffer:
字符串缓冲区
特点:
1.相当于一个可变的序列 操作的是原字符串
2.线程安全 耗费资源
拼接:
/**
* 无参创建StringBuffer
*/
public static void fun() {
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity());
System.out.println(sb.length());
System.out.println(sb.toString());
sb.append("wangsong");
System.out.println(sb);
}
插入:
/**
* 插入 修改
*/
public static void fun() {
StringBuffer sb = new StringBuffer("wangsong");
sb.insert(3, false);
System.out.println(sb);
sb.setCharAt(6, 'k');
System.out.println(sb);
char c = sb.charAt(7);
System.out.println(c);
char[] charArray = sb.toString().toCharArray();
System.out.println(charArray);
}
删除:
/**
* 删除
*/
public static void fun() {
StringBuffer sb = new StringBuffer();
sb.append("wangsong");
sb.reverse();
System.out.println(sb);
}
反转:
/**
* 从键盘输入一个字符串 反转
*/
public static void fun() {
System.out.println("请输入一个字符串:");
Scanner scanner = new Scanner(System.in);
String string = scanner.nextLine();
StringBuffer stringBuffer = new StringBuffer(string);
stringBuffer.reverse();
System.out.println(stringBuffer.toString());
}
替换:
/**
* 替换
*/
public static void fun() {
StringBuffer sb = new StringBuffer();
sb.append("wangsong");
System.out.println(sb);
String substring1 = sb.substring(0, 4);
System.out.println(substring1);
}
拼接练习:
需求:
把int[] array = new int[]{1,2,3,4};
输出 [1, 2, 3, 4];
public static void fun() {
int[] array = new int[]{1,2,3,4};
String string = "[";
for (int i = 0; i < array.length; i++) {
if (i < array.length - 1) {
string = string + array[i] + ",";
}else {
string = string + array[i] + "]";
}
}
System.out.println(string);
}
public static void fun() {
int[] array = new int[]{1,2,3,4};
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("[");
for (int i = 0; i < array.length; i++) {
if (i < array.length - 1) {
stringBuffer.append(array[i]).append(",");
}else {
stringBuffer.append(array[i]).append("]");
}
}
System.out.println(stringBuffer);
}
public static void fun1 (String string) {
string = "fangsong";
}
public static void fun2 (StringBuffer sBuffer) {
sBuffer.append("znb");
}
public static void fun3(StringBuilder sBuilder) {
sBuilder.append("haha");
}
public static void main(String[] args) {
String string = "wangsong";
fun1(string);
System.out.println(string);
StringBuffer sBuffer = new StringBuffer("wangsong");
fun2(sBuffer);
System.out.println(sBuffer);
StringBuilder sBuilder = new StringBuilder("wangsong");
fun3(sBuilder);
System.out.println(sBuilder);
}
据上总结:
StringBuffer和StringBuilder区别
StringBuffer 线程安全 效率低(耗费系统资源)
StringBuilder 线程不安全 效率高(相对于StringBuffer消耗资源少)
StringBuffer和StringBuilder使用方法一样
String和StringBuffer的区别
String 是不可变得 线程不安全
StringBuffer 是可变的 操作的时对象本身
基本数据类型包装类:
基本数据类型 | 包装类 |
---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
boolean | Boolean |
char | Character |
包装类的作用:
类中可以声明方法 可以使用对象调用方法
举例:
public static void fun() {
Integer integer1 = new Integer(10);
System.out.println(integer1);
Integer integer2 = new Integer("100");
System.out.println(integer2);
int a = 60;
System.out.println(Integer.toBinaryString(a));
System.out.println(Integer.toOctalString(a));
System.out.println(Integer.toHexString(a));
Integer num1 = Integer.valueOf(10);
int num2 = num1.intValue();
Integer num3 = Integer.valueOf("100");
String string = num3.toString();
int num4 = Integer.parseInt("100");
}
/**
* JDK1.5 自动装箱 和 自动拆箱
*/
public static void fun() {
Integer num1 = 10;
int rel = num1 + 5;
System.out.println(rel);
}
public static void fun() {
Integer num1 = 150;
Integer num2 = 150;
System.out.println(num1 == num2);
System.out.println(num1.equals(num2));
}
权限修饰符:
权限修饰符 | 类别 |
---|
public | 公开的 |
private | 私有的 |
protected | 受保护的 |
default | 默认的 |
default 什么都不要写
权限修饰符在不同类中的实现:
参数 | 本类 | 同包类 | 同包子类 | 不同包类 | 不同包子类 |
---|
public | ✔️ | ✔️ | ✔️ | ✔️ | ✔️ |
private | ✔️ | ✘ | ✘ | ✘ | ✘ |
protected | ✔️ | ✔️ | ✔️ | ✘ | ✔️ |
default | ✔️ | ✔️ | ✔️ | ✘ | ✘ |