当前位置 : 主页 > 编程语言 > java >

API常用类,Object,String,Stringbuffer,Stringbuild

来源:互联网 收集:自由互联 发布时间:2023-09-06
一,API 1,概述 -API(Application Programming Interface,应用程序编程接口) 是一些预先定义的函数,目的是提供应用程序与开发人员基于 某软件或硬件得以访问一组例程的能力,而又无需访问

一,API

1,概述

-API(Application Programming Interface,应用程序编程接口)

是一些预先定义的函数,目的是提供应用程序与开发人员基于

某软件或硬件得以访问一组例程的能力,而又无需访问源码,

或理解内部工作机制的细节。

-java API:就是java提供好的类,已经把顶层实现封装起来了。

我们不需要关系它是如何实现的,只需要学习如何使用

-我们通过API说明书来学习即可

二,常用类

1,Object类

-概述

lang包,不用导包 1.0开始就有

是类层次结构中的跟类,

所有类都直接或间接继承这个类

-构造方法

Object()

public class Main { public static void main(String[] args) { //new 它合适吗? 不合适,不是具体事物 //构造方法不是用来new对象的,是给子类用的 Object o = new Object(); System.out.println(o); System.out.println(o.hashCode()); } }

-成员方法(有几个方法是多线程相关的,后面讲,其余的目前都讲)

protected Object clone()

创建并返回此对象的副本。

boolean equals(Object obj)

指示一些其他对象是否等于此。

protected void finalize()

当垃圾收集确定不再有对该对象的引用时,垃圾收集器在对象上调用该对象。

类 getClass()

返回此 Object的运行时类。

int hashCode()

返回对象的哈希码值。

String toString()

返回对象的字符串表示形式。

package com.momo.pojo;

import java.util.Objects;

public class Student {

private String name;

private int age;

public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }

/* public String toString() {

//String s = name+","+age;return s;//

return name+"---"+age;

}*/

@Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; }

/* @Override

public boolean equals(Object obj) {

//Object obj = new Student(); 多态

Student s = (Student) obj;

boolean boo1 = this.name == s.name;

boolean boo2 = boo1?this.age==s.age:boo1;

return boo2;

}*/

/* @Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

Student student = (Student) o; if (age != student.age) return false; return Objects.equals(name, student.name); } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + age; return result; }*/ @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return age == student.age && Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(name, age); }

}

package com.momo.demo;

import com.momo.pojo.Student;

/*

  • int hashCode() 返回哈希码值
  • 哈希值:是根据哈希算法计算出来的值,这个值和地址值有关系,但是不是实际地址值
  • 理解为逻辑地址值

  • 不同对象的哈希值不一样,同一个对象的哈希值肯定一样
  • */

public class Demo1 {

public static void main(String[] args) {

Student s1 = new Student();

System.out.println(s1.hashCode());

Student s2 = new Student(); System.out.println(s2.hashCode()); Student s3 = new Student(); System.out.println(s3.hashCode()); Student s4 = s1; System.out.println(s4.hashCode());

}

}

package com.momo.demo;

import com.momo.pojo.Student;

/*

  • Class getClass()

返回的是当前正在运行的字节码文件对象

Class 类:

  • String getName()

返回由 类对象表示的实体(类,接口,数组类,原始类型或空白)的名称,作为 String 。

  • */

public class Demo2 {

public static void main(String[] args) {

Student s1 = new Student();

Class c1 = s1.getClass();

System.out.println(c1.getName());

Student s2 = new Student(); Class c2 = s2.getClass(); System.out.println(c2.getName());

}

}

package com.momo.demo;

import com.momo.pojo.Student;

/*

  • String toString()

返回对象的字符串表示形式。 默认输出的还是地址值(对于我们来说没有意义)

我们直接输出一个对象名,默认会调用Object中的toString()方法

*

  • public String toString() {

return getClass().getName() + "@" + Integer.toHexString(hashCode());

}

查看原码返现,toString方法默认就是把getClass方法和hashCode方法做了一个拼接

*

Integer类:

  • static String toHexString(int i)

把十进制的数据转换成了十六进制,并且以字符串形式返回

  • 所以我们应该要重写这个方法,让其输出有意义。应该输出什么?
  • 应该输出所有成员变量值
  • 比如:人做自我介绍

  • 手动重写
  • 自动生成即可

  • */

public class Demo3 {

public static void main(String[] args) {

Student s1 = new Student();

System.out.println(s1);

System.out.println(s1.toString());

Student s2 = new Student();

System.out.println(s2);

System.out.println(s2.toString());

Student s3 = new Student("aa",11);

System.out.println(s3);

System.out.println(s3.toString());

}

}

package com.momo.demo;

import com.momo.pojo.Student;

/*

  • boolean equals(Object obj)

比较两个对象是否相等

*

public boolean equals(Object obj) {

return (this == obj);

}

发现原码中用的是== ,比较的是地址值,2个对象比较地址值没有意义。

所以应该重写,应该比较的是所有的属性值(成员变量值)

  • 手动重写:
  • 自动生成:

问:== 和 equals的区别?

  • ==:
  • 基本类型:值
  • 引用类型:地址值
  • equals:
  • 引用类型:默认比较的是地址值,我们一般都会根据情况重写该方法,自动生成
  • 比较的是对象的成员变量值是否相等
  • */

public class Demo4 {

public static void main(String[] args) {

Student s1 = new Student("默默",18);

Student s2 = new Student("小宝",20);

System.out.println(s1);

System.out.println(s2);

System.out.println(s1==s2);

System.out.println(s1.equals(s2));

Student s3 = new Student("默默",18);

System.out.println(s3);

System.out.println(s1==s3);

System.out.println(s1.equals(s3));

// System.out.println("abc".equals("abc"));

}

}

package com.momo.pojo;

import java.util.Objects;

public class Student {

private String name;

private int age;

public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }

/* public String toString() {

//String s = name+","+age;return s;//

return name+"---"+age;

}*/

@Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; }

/* @Override

public boolean equals(Object obj) {

//Object obj = new Student(); 多态

Student s = (Student) obj;

boolean boo1 = this.name == s.name;

boolean boo2 = boo1?this.age==s.age:boo1;

return boo2;

}*/

/* @Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

Student student = (Student) o; if (age != student.age) return false; return Objects.equals(name, student.name); } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + age; return result; }*/ @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return age == student.age && Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(name, age); } @Override protected void finalize() throws Throwable { System.out.println("我被回收了。。。。"); super.finalize(); }

}

package com.momo.demo;

import com.momo.pojo.Student;

/*

  • protected void finalize()

当垃圾收集确定不再有对该对象的引用时,垃圾收集器在对象上调用该对象。

这个方法不需要我们来调用,是虚拟机自己调用的

  • 我们可以使用System.gc() 来建议它尽快回收。。。

针对堆内存

  • */

public class Demo5 {

public static void main(String[] args) {

Student s1 = new Student();

System.out.println(s1);

s1 = null; System.gc(); System.out.println(s1); for (int i = 0; i < 100; i++) { System.out.println(i); }

}

}

package com.momo.pojo;

import java.util.Objects;

public class Student extends Object implements Cloneable {

private String name;

private int age;

public Student() { } public Student(String name, int age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; }

/* public String toString() {

//String s = name+","+age;return s;//

return name+"---"+age;

}*/

@Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; }

/* @Override

public boolean equals(Object obj) {

//Object obj = new Student(); 多态

Student s = (Student) obj;

boolean boo1 = this.name == s.name;

boolean boo2 = boo1?this.age==s.age:boo1;

return boo2;

}*/

/* @Override

public boolean equals(Object o) {

if (this == o) return true;

if (o == null || getClass() != o.getClass()) return false;

Student student = (Student) o; if (age != student.age) return false; return Objects.equals(name, student.name); } @Override public int hashCode() { int result = name != null ? name.hashCode() : 0; result = 31 * result + age; return result; }*/ @Override public boolean equals(Object o) { if (this == o) return true; if (o == null || getClass() != o.getClass()) return false; Student student = (Student) o; return age == student.age && Objects.equals(name, student.name); } @Override public int hashCode() { return Objects.hash(name, age); } @Override protected void finalize() throws Throwable { System.out.println("我被回收了。。。。"); super.finalize(); } @Override public Object clone() throws CloneNotSupportedException { return super.clone(); }

}

package com.momo.demo;

import com.momo.pojo.Student;

/*

  • protected Object clone()

创建并返回此对象的副本。

*

克隆的步骤:

  • 自定义类实现 Cloneable 接口,是一个标记性接口(没有任何方法),实现了这个接口的类才可以克隆
  • 自定义类要重写 clone方法,通过super调用父类方法即可

*注意:克隆和2个引用指向同一个对象的区别

  • 问:对象的浅拷贝和深拷贝(浅克隆和深克隆)
  • /

public class Demo6 {public static void main(String[] args) throws CloneNotSupportedException {Student s1 = new Student("默默",18);System.out.println(s1);/

System.out.println(s2);

System.out.println(s1==s2);

System.out.println(s1.equals(s2));

s2.setName("小宝");

System.out.println(s1);

System.out.println(s2);*/

Object o = s1.clone(); System.out.println(o); Student ss = (Student) o; ((Student) o).setName("钢弹。"); System.out.println(o); System.out.println(s1);

}

}

2,Scanner类

-java.util包,要导包

-从jdk1.5之后用于接收键盘输入的

-构造方法:

Scanner(InputStream source)

构造一个新的 Scanner ,产生从指定输入流扫描的值。

System类:

static InputStream in

“标准”输入流。

-成员方法:很多

boolean hasNextXxx() 判断输入的值是否是某种类型

Xxx nextXxx() 获取输入的某种类型的值

最常用的

int nextInt()

String nextLine()

-注意:先nextInt() 后 nextLine() 的问题。

package com.momo.demo;

import java.util.Scanner;

/*

  • 当我们输入的值不符合数据类型时 InputMismatchException 输入不匹配异常
  • 使用 hasNextXxx() 方法可以解决这类问题 接收前先判断是否满足类型条件。
  • /

public class Demo7 {public static void main(String[] args) {/

System.out.println("请输入一个值:");

if(sc.hasNextInt()){

int i = sc.nextInt();

System.out.println(i);

}else{

System.out.println("输入不匹配。。。");

}*/

Scanner sc = new Scanner(System.in); /* System.out.println("请输入一个int值:"); int a = sc.nextInt(); System.out.println(a); System.out.println("请输入一个int值:"); int b = sc.nextInt(); System.out.println(b);*/ /*System.out.println("请输入一个string值:"); String a = sc.nextLine(); System.out.println(a); System.out.println("请输入一个string值:"); String b = sc.nextLine(); System.out.println(b);*/

/* System.out.println("请输入一个string值:");

String a = sc.nextLine();

System.out.println(a);

System.out.println("请输入一个int值:");

int b = sc.nextInt();

System.out.println(b);*/

/* * 出问题了,输入完int后,字符串输不了了,程序直接结束了。 * 为什么? * 简单理解,输入完成int之后,需要按回车继续,结果回车被 nextLine()接收到了 ,所以就无法再次输入 * * 如何解决? * 在int之后重新创建对象来接收字符串 * 都先以字符串形式接收,然后转换。注意 * */ /*System.out.println("请输入一个int值:"); int b = sc.nextInt(); System.out.println(b); System.out.println("请输入一个string值:"); String a = sc.nextLine(); System.out.println(a);*/ /* System.out.println("请输入一个int值:"); int b = sc.nextInt(); System.out.println(b); sc = new Scanner(System.in); System.out.println("请输入一个string值:"); String a = sc.nextLine(); System.out.println(a);*/ System.out.println("请输入一个int值:"); String b = sc.nextLine(); //b必须是纯数字字符串,否则会出错 NumberFormatException 数字格式化异常 int bb = Integer.parseInt(b); System.out.println(bb); System.out.println("请输入一个string值:"); String a = sc.nextLine(); System.out.println(a);

}

}

3,String 重要(程序中对字符串的操作很多,字符串操作是最常见的操作)

-java本身没有内置字符串类型,所以就在java类库中提供了String类供我们使用

方便我们对字符串进行操作

-java.lang包,不用导包 1.0

-String类代表字符串。 Java程序中的所有字符串文字(例如"abc" )都被实现为此类的实例。

String s = "abc" "abc"

-字符串不变; 它们的 值 在创建后不能被更改。 "abc" 常量

-字符串也可以看成是字符数组

-由多个字符组成的一串数据

package com.momo.demo;

public class Demo8 {

public static void main(String[] args) {

/* String s = "abc";

String ss = new String("abc");

System.out.println(s);

System.out.println(ss);*/

String s = "abc"; System.out.println(s); s = "aaa";//s的指向变了 System.out.println(s); }

}

-构造方法

String()

初始化新创建的 String对象,使其表示空字符序列。

String(byte[] bytes)

通过使用平台的默认字符集解码指定的字节数组来构造新的 String 。

String(byte[] bytes, int offset, int length)

通过使用平台的默认字符集解码指定的字节子阵列来构造新的 String 。

String(byte[] bytes, int offset, int length, String charsetName)

构造一个新的 String通过使用指定的字符集解码指定的字节子阵列。

String(byte[] bytes, String charsetName)

构造一个新的String由指定用指定的字节的数组解码charset 。

String(char[] value)

分配一个新的 String ,以便它表示当前包含在字符数组参数中的字符序列。

String(char[] value, int offset, int count)

分配一个新的 String ,其中包含字符数组参数的子阵列中的字符。

String(String original)

初始化新创建的String对象,使其表示与参数相同的字符序列; 换句话说,新创建的字符串是参数字符串的副本。

package com.momo.demo;

import java.io.UnsupportedEncodingException;

/*

  • String类的构造方法
  • */

public class Demo9 {

public static void main(String[] args) throws UnsupportedEncodingException {

String s = new String();

System.out.println(s);

byte[] bys = {97,98,99,100}; String s2 = new String(bys); System.out.println(s2); String s3 = new String(bys,1,2); System.out.println(s3); char[] chs = {'a','A','0','我','t','5'}; String s4 = new String(chs); System.out.println(s4); String s5 = new String(chs,2,3); System.out.println(s5); String s6 = new String("abc"); System.out.println(s6);

}

}

-String类的特点和相关问题

String s = new String("abc") 和 String = "abc";的区别?

new不仅在堆中开辟空间也会在常量池中开辟空间

直接赋值 只在常量池中开辟空间

package com.momo.demo;

public class Demo10 {

public static void main(String[] args) {

/* String s = "abc";

String ss = new String("abc");

System.out.println(s);

System.out.println(ss);

System.out.println(s.equals(ss));

System.out.println(s==ss);

System.out.println("------------------");

String s1 = new String("aaa");

String s2 = new String("aaa");

System.out.println(s1==s2);

System.out.println("-----------------");

String s3 = "bbb"; String s4 = "bbb"; System.out.println(s3==s4); System.out.println("-----------------");*/

/*

  • 字符串如果是变量相加,先开空间,在拼接
  • 字符串如果是常量相加,先加,然后在常量池中找看有没有,有就直接返回,没有的化才会开辟空间
  • */

String ss1 = "momo";

String ss2 = "shuaige";

String ss3 = "momoshuaige";

System.out.println(ss3==ss1+ss2);//false System.out.println(ss3.equals(ss1+ss2));//true /* * System.out.println(ss3==new StringBuilder(ss1).append(ss2)); System.out.println(ss3.equals(ss1+ss2)); * */ System.out.println(ss3=="momo"+"shuaige");//true System.out.println(ss3.equals("momo"+"shuaige"));//true /* * System.out.println(ss3=="momoshuaige");//true System.out.println(ss3.equals("momoshuaige")); * */

}

}

4,String类的成员方法

-判断

boolean contains(String s)

当且仅当此字符串包含指定的char值序列时才返回true。

boolean endsWith(String suffix)

测试此字符串是否以指定的后缀结尾。

boolean equals(Object anObject)

将此字符串与指定对象进行比较。

boolean equalsIgnoreCase(String anotherString)

将此 String与其他 String比较,忽略案例注意事项。

boolean isEmpty() 判断内容是否为空的

返回 true如果,且仅当 length()为 0 。

注意:String s = ""; String s = null 的区别

boolean startsWith(String prefix)

测试此字符串是否以指定的前缀开头。

boolean startsWith(String prefix, int toffset)

测试在指定索引处开始的此字符串

的子字符串是否以指定的前缀开头。

package com.momo.demo;

import java.sql.SQLOutput;

/*

  • String 类判断功能
  • */

public class Demo11 {

public static void main(String[] args) {

String s = "hello";

String ss = "HELLO";

System.out.println(s.contains("el")); System.out.println(s.contains("eo")); System.out.println(s.endsWith("lo")); System.out.println(s.endsWith("o")); System.out.println(s.endsWith("l")); System.out.println("-----------------"); System.out.println(s.equals(ss)); System.out.println(s.equalsIgnoreCase(ss)); System.out.println("-----------"); /* System.out.println(s.isEmpty()); s = ""; System.out.println(s.isEmpty()); s = null;*/ // System.out.println(s.isEmpty());NullPointerException System.out.println("---------------"); /* System.out.println(s.startsWith("h")); System.out.println(s.startsWith("e"));*/ System.out.println(s.startsWith("e",1));

}

}

练习:模拟用户登录,给3次机会,如果错误,并提示还剩几次机会

-获取

char charAt(int index)

返回 char指定索引处的值。

int indexOf(int ch)

返回指定字符第一次出现的字符串内的索引。

int indexOf(int ch, int fromIndex)

返回指定字符第一次出现的字符串内的索引,以指定的索引开始搜索。

int indexOf(String str)

返回指定子字符串第一次出现的字符串内的索引。

int indexOf(String str, int fromIndex)

返回指定子串的第一次出现的字符串中的索引,从指定的索引开始。

int lastIndexOf(int ch)

返回指定字符的最后一次出现的字符串中的索引。

int lastIndexOf(int ch, int fromIndex)

返回指定字符的最后一次出现的字符串中的索引,从指定的索引开始向后搜索。

int lastIndexOf(String str)

返回指定子字符串最后一次出现的字符串中的索引。

int lastIndexOf(String str, int fromIndex)

返回指定子字符串的最后一次出现的字符串中的索引,从指定索引开始向后搜索。

int length()

返回此字符串的长度。

package com.momo.demo;

/*

  • String类获取功能
  • */

public class Demo12 {

public static void main(String[] args) {

String s = "momoshigedashuaige";

System.out.println(s.charAt(5));

System.out.println(s.indexOf(97));

System.out.println(s.indexOf('o'));

System.out.println(s.indexOf('x'));

System.out.println(s.indexOf('o',2));

System.out.println(s.indexOf("shi"));

System.out.println(s.length());

}

}

练习:遍历获取字符串中的每一个字符

练习:统计一个字符串中大写字母,小写字母,数字字符出现的次数

-转换

static String copyValueOf(char[] data)

相当于 valueOf(char[]) 。

static String copyValueOf(char[] data, int offset, int count)

相当于 valueOf(char[], int, int) 。

byte[] getBytes()

使用平台的默认字符集将此 String编码为字节序列,将结果存储到新的字节数组中。

byte[] getBytes(Charset charset)

使用给定的charset将该String编码为字节序列,将结果存储到新的字节数组中。

byte[] getBytes(String charsetName)

使用命名的字符集将此 String编码为字节序列,将结果存储到新的字节数组中。

char[] toCharArray()

将此字符串转换为新的字符数组。

String toLowerCase()

将所有在此字符 String使用默认语言环境的规则,以小写。

String toUpperCase()

将所有在此字符 String使用默认语言环境的规则大写。

static String valueOf(boolean b)

返回 boolean参数的字符串 boolean形式。

static String valueOf(char c)

返回 char参数的字符串 char形式。

static String valueOf(char[] data)

返回 char数组参数的字符串 char形式。

static String valueOf(char[] data, int offset, int count)

返回 char数组参数的特定子阵列的字符串 char形式。

static String valueOf(double d)

返回 double参数的字符串 double形式。

static String valueOf(float f)

返回 float参数的字符串 float形式。

static String valueOf(int i)

返回 int参数的字符串 int形式。

static String valueOf(long l)

返回 long参数的字符串 long形式。

static String valueOf(Object obj)

返回 Object参数的字符串 Object形式。

package com.momo.demo;

import com.momo.pojo.Student;

/*

  • String类转换功能
  • */

public class Demo13 {

public static void main(String[] args) {

char[] chs = {97,98,99,100};

String s = String.copyValueOf(chs);

System.out.println(s);

String s1 = String.copyValueOf(chs, 1, 2);

System.out.println(s1);

String s2 = "aBc"; /* byte[] bys = s2.getBytes(); System.out.println(bys); for (int i = 0; i < bys.length; i++) { System.out.println(bys[i]); }*/ /* char[] chss = s2.toCharArray(); for (int i = 0; i < chss.length; i++) { System.out.println(chss[i]); }*/ System.out.println(s2.toLowerCase()); System.out.println(s2.toUpperCase()); System.out.println(String.valueOf(123)); System.out.println(String.valueOf(new Student("默默",22)));

}

}

练习:把一个字符串的首字母转成大写,其余都转成小写

-替换

String replace(char oldChar, char newChar)

返回从替换所有出现的导致一个字符串 oldChar在此字符串 newChar 。

String replace(String target, String replacement)

将与字面目标序列匹配的字符串的每个子字符串替换为指定的字面替换序列。

-去两端空格

String trim()

返回一个字符串,其值为此字符串,并删除任何前导和尾随空格。

-比较

int compareTo(String anotherString)

按字典顺序比较两个字符串。

int compareToIgnoreCase(String str)

按字典顺序比较两个字符串,忽略病例差异。

-截取

String substring(int beginIndex)

返回一个字符串,该字符串是此字符串的子字符串。

String substring(int beginIndex, int endIndex)

返回一个字符串,该字符串是此字符串的子字符串。

-拼接

String concat(String str)

将指定的字符串连接到该字符串的末尾。

package com.momo;

/*

  • String 类其他功能
  • /

public class Demo14 {public static void main(String[] args) {String s = "默默是大帅哥";/

System.out.println(s);

System.out.println(ss);*/

//含头不含尾 String ss = s.substring(2, 4); System.out.println(s); System.out.println(ss); /* String ss = s.replace("帅哥", "屌丝"); System.out.println(s); System.out.println(ss); String s2 = " slkdjf sd lk;sdjkf sl;kdjfsd f sdl;kfjsdlf sdk jf sld; "; String s3 = s2.trim(); System.out.println(s3); String s4 = "abc";*/ /* String aaa = s4.concat("aaa"); System.out.println(s4); System.out.println(aaa);*/ /*s4 = s4.concat("aaa"); s4 += "aaa"; System.out.println(s4);*/ /* * 把字符串拆成字符数字,一个个比较的 ascii表中的值进行对比的 * */ String a = "abc"; String b = "ABC"; System.out.println(a.compareTo(b));

}

}

5,String类练习

-把数组中的数据按照指定格式拼接成字符串

int[] arr = {1,2,3} 结果 [1,2,3]

-字符串反转

abc cba

-统计一个大字符串中小字符串出现的次数

"momoshuaige,momoshuaige,momoshigedashuaige" 中"shuaige"出现了3次

二,StringBuffer

1,概述

-我们再对字符串进行拼接时,每一次拼接,都会创建一个新的String对象

既浪费时间,又浪费空间。而字符串缓冲区可以解决这类问题

-线程安全,可变的字符序列。 字符串缓冲区就像一个String ,但可以修改(长度和内容)

-线程(多线程讲)

安全-----同步----保证数据的安全性,但是效率低

不安全---不同步---效率搞一些,可能会出现问题

安全:银行,医院

效率:论坛,博客

-问:StringBuffer和String以及StringBuilder的区别?

String 是内容不可变的,StringBuffer和StringBuilder是可变的

StringBuffer 是安全的,效率低,

StringBuilder 不安全,效率高

2,构造方法

StringBuffer()

构造一个没有字符的字符串缓冲区,初始容量为16个字符。

StringBuffer(int capacity)

构造一个没有字符的字符串缓冲区和指定的初始容量。

StringBuffer(String str)

构造一个初始化为指定字符串内容的字符串缓冲区。

package com.momo.demo;

/*

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

public class Demo14 {public static void main(String[] args) {/

// sb1.append("sfdjlsdkjfldsjfldskjflsdfjsdlfjsdle");

System.out.println(sb1.capacity());

System.out.println(sb1.length());*/

/*StringBuffer sb2 = new StringBuffer(5); sb2.append("sdfdsfdsfsdf"); System.out.println(sb2.capacity()); System.out.println(sb2.length());*/ StringBuffer sb3 = new StringBuffer("abcd"); System.out.println(sb3.capacity()); System.out.println(sb3.length());

}

}

3,成员方法

-添加:返回值返回的是对象本身

StringBuffer append(Object obj)

追加 Object参数的字符串表示。

有很多重载,各种类型都可以添加。

StringBuffer insert(int offset, Object obj)

将 Object参数的字符串表示插入到此字符序列中。

有很多重载,各种类型都可以添加。

package com.momo.demo;

/*

  • StringBuffer的添加功能
  • */

public class Demo15 {

public static void main(String[] args) {

StringBuffer sb = new StringBuffer();

//说明了添加方法返回的时sb本身

/StringBuffer sb2 = sb.append(123);System.out.println(sb);System.out.println(sb2);System.out.println(sb==sb2);/

sb.append(123).append('a').append(true).append(12.3); System.out.println(sb); sb.insert(3,"momo"); System.out.println(sb);

}

}

-删除:返回值返回的是对象本身

StringBuffer delete(int start, int end) 含头不含尾

删除此序列的子字符串中的字符。

StringBuffer deleteCharAt(int index)

删除 char在这个序列中的指定位置。

package com.momo.demo;

/*

  • StringBuffer 删除功能
  • /

public class Demo16 {public static void main(String[] args) {StringBuffer sb = new StringBuffer("默默是个大帅哥");System.out.println(sb);//删除 哥/

System.out.println(sb);*/

//删除 大帅 /* sb.delete(4,6); System.out.println(sb);*/ //删除所有 sb.delete(0,sb.length()); System.out.println(sb);

}

}

-替换:返回值返回的是对象本身

StringBuffer replace(int start, int end, String str)

用指定的String中的字符替换此序列的子字符串中的 String 。

package com.momo.demo;

/*

  • sb 替换功能
  • */

public class Demo17 {

public static void main(String[] args) {

StringBuffer sb = new StringBuffer("默默是个大帅哥");

StringBuffer sb2 = sb.replace(4, 7, "屌丝");

System.out.println(sb==sb2);

System.out.println(sb); System.out.println(sb2);

}

}

-反转:返回值返回的是对象本身

StringBuffer reverse()

导致该字符序列被序列的相反代替。

package com.momo.demo;

/*

  • sb 反转功能
  • */

public class Demo18 {

public static void main(String[] args) {

StringBuffer sb = new StringBuffer("abc");

sb.reverse();

System.out.println(sb);

}

}

-截取:注意返回的是一个字符串,本身没有变化

String substring(int start)

返回一个新的 String ,其中包含此字符序列中当前包含的字符的子序列。

String substring(int start, int end)

返回一个新的 String ,其中包含此序列中当前包含的字符的子序列。

package com.momo.demo;

/*

  • sb 截取功能
  • /

public class Demo19 {public static void main(String[] args) {StringBuffer sb = new StringBuffer("默默是个大帅哥");/

System.out.println(sb);*/

/* sb.setLength(5); System.out.println(sb.capacity()); System.out.println(sb.length()); System.out.println(sb);*/ String s = sb.substring(4); s = sb.substring(4,6); System.out.println(s); System.out.println(sb);

}

}

4,StringBuffer的练习

-String和StringBuffer的相互转换

String转StringBuffer有几种方式

StringBuffer转String有几种方式

-把数组拼接成字符串

-字符串反转

-判断一个字符串是否是对称字符串

5,StringBuilder

-一个可变的字符序列。 此类提供与StringBuffer相同的API,但不保证同步(不安全)

-jdk1.5有的

-问:数组和字符串缓冲区的区别?

2者都可以看做是容器,都可以存储任意类型数据

但是,字符串缓冲区 装任何内容 最终都是字符串

数组也可以存放多种数据,但是一般只存一种类型数据

【感谢龙石为本站提供数据api平台http://www.longshidata.com/pages/exchange.html】
网友评论