String 是一个字符串,但是字符串的内容不可改变,改变的只是内存地址的批晌 ,那么如果现在要想让字符串的内容可以修改,则必须使用StringBuff
StringBuffer 字符串连
package org.stringbufferdemo; public class StringBuefferDemo01 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub StringBuffer buf = new StringBuffer(); buf.append("Hello").append(" World"); System.out.println(buf); } }
一般情况下StringBufffer 往往都用在频繁修改字符串的地方
package org.stringbufferdemo; public class StringBuefferDemo01 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub StringBuffer buf = new StringBuffer(); fun(buf); for (int i=0;i<1000;i++){ buf.append(i); } System.out.println(buf); } public static void fun(StringBuffer buf){ buf.append("hello world!!!").append("\n"); } }
StringBuffer 常用方法
在指定位置处添加内容
默认情况下所有的内容都是在StringBuffer 最后添加的
使用Insert()方法完成
package org.stringbufferdemo; public class StringBufferDemo02 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub StringBuffer buf = new StringBuffer(); buf.append("Hello World"); buf.insert(0,"aabbcc"); System.out.println(buf); } }
删除指定范围的内容
public StringBuffer delete(int start,int end)
package org.stringbufferdemo; public class StringBufferDemo03 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub StringBuffer buf = new StringBuffer(); buf.append("hello world").delete(0, 5); System.out.println(buf); } }
反转字符串内容
public StringBuffer reverse()
package org.stringbufferdemo; public class StringBufferDemo03 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub StringBuffer buf = new StringBuffer(); buf.append("Hello World"); buf.reverse(); System.out.println(buf); } }
深入引用传递
范例一:
package org.staticdemo; class A{ int x = 10; } public class StringDemo03 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub A a = new A(); a.x = 30; fun(a); System.out.print(a.x); } public static void fun(A a){ a.x = 100; } }
范例二:
package org.staticdemo; public class StringDemo04 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub String str = "hello"; fun(str); System.out.println(str); } public static void fun(String temp){ temp = "world"; } }
范例三:
package org.staticdemo; class A{ String str = "mldn"; } public class StringDemo03 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub A a = new A(); a.str = "hello"; fun(a); System.out.print(a.str); } public static void fun(A a){ a.str = "world"; } }
package org.stringdemo; class Person{ private String name; private int age; private Book book; private Person child; public Person(String name,int age){ this.setName(name); this.setAge(age); } public void setChild(Person child){ this.child = child; } public void setBook(Book book){ this.book = book; } public void setName(String name){ this.name = name; } public void setAge(int age){ this.age = age; } public Person getChild(){ return this.child; } public Book getBook(){ return this.book; } public String getName(){ return this.name; } public int getAge(){ return this.age; } } class Book{ private String title; private float price; private Person person; public Book(String title,float price){ this.setTitle(title); this.setPrice(price); } public void setPerson(Person person){ this.person = person; } public void setTitle(String title){ this.title = title; } public void setPrice(float price){ this.price = price; } public Person getPerson(){ return this.person; } public String getTitle(){ return this.title; } public float getPrice(){ return this.price; } } public class StringDemo08 { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Person per1 = new Person("张三",40); Person child = new Person("张四",20); per1.setChild(child); Book book1 = new Book("java",80.6f); Book book2 = new Book("童话",30.6f); per1.setBook(book1); book1.setPerson(per1); child.setBook(book2); System.out.println(per1.getBook().getTitle()); // 一个人有一本书 System.out.println(per1.getChild().getName()); // 一个儿童属于一个人 System.out.println(child.getBook().getTitle()); // 一个人有一本书 System.out.println(book1.getPerson().getName()); // 一本书属于一个人 } }