Java学习第三十八天<包装类><String类><StringBuffer><StringBuilder>
包装类
package chapter16.包装类; public class WrapperType { public static void main(String[] args) { //父类object //boolean>Boolean //char>Character // Number子类 //byte>Byte //short>Short //int>Integer //long>Long //float>Float //double>Double //int >Integer jdk5以前是手动装箱和拆箱 int n1=100; Integer integer = new Integer(n1); Integer integer1 = Integer.valueOf(n1); //Integer>int 手动拆箱 int i=integer.intValue(); //jdk5后自动装箱拆箱 int n2=200; Integer integer2=n2;//底层仍是Integer.valueOf(n2); int n3=integer2;//底层仍用integer.intValue(); //其他包装类用法一致 } }
package chapter16.包装类; public class WrapperVSString { public static void main(String[] args) { //包装类 Integer >> String Integer i=100; String str1=i+""; //方式1 不影响 i 的类型 String str2=i.toString(); //方式2 String str3=String.valueOf(i);//方式3 //String >> Integer String str4="12345"; Integer i2 =Integer.parseInt(str4);//方式1 Integer i3 =new Integer(str4); //方式2 } }
package chapter16.包装类; //右键 show diagram 点m可看方法 public class WrapperMethod { public static void main(String[] args) { System.out.println(Integer.MIN_VALUE);//返回最小值 System.out.println(Integer.MAX_VALUE); System.out.println(Character.isDigit('a'));//判断是否数字 System.out.println(Character.isLetter('a'));//判断是否字母 System.out.println(Character.isUpperCase('a'));//是否大写 System.out.println(Character.isLowerCase('a')); System.out.println(Character.isWhitespace('a'));//是否空格 System.out.println(Character.toUpperCase('a'));//转大写 System.out.println(Character.toLowerCase('A')); } }
package chapter16.包装类; public class WrapperExercise { public static void main(String[] args) { Integer i=new Integer(1); Integer j=new Integer(1); System.out.println(i==j);//F new后对象不同 Integer m=1; Integer n=1;//底层Integer.valueOf(1) 阅读原码 在-128--127之间直接返回,否则new System.out.println(m==n);//T Integer x=128; Integer y=128;//超出范围,会new System.out.println(x==y);//F Integer i9=127;//底层Integer.valueOf(127) Integer i10=new Integer(127); System.out.println(i9==i10);//F 一个是从cache里拿的,一个是new的 Integer i11=127; int i12=127; System.out.println(i11==i12);//T 有基本数据类型判断值是否相等 Integer i13=128; int i14=128; System.out.println(i13==i14);//T } }
String类
package chapter16.String类; public class String01 { public static void main(String[] args) { //String实现 接口 Serializable 可串行化:在网络中传输 // 接口 Comparable 对象可比较大小 //有属性 private final char value[] 用于存放字符串内容 value数组是final类,一旦赋值地址不能修改(不能指向新的对象) 单个字符内容可修改 String a="abc"; String b="abc"; System.out.println(a.equals(b));//T 方法重写,比较每个字符 System.out.println(a==b);//T //内存图 System.out.println("==============================="); String c=new String("abc"); System.out.println(a.equals(c));//T 比较每个字符 System.out.println(a==c);//F 地址不同 System.out.println(a==c.intern());//T intern()返回常量池地址 System.out.println(c==c.intern());//F 一个指向堆 一个是常量池 地址不同 System.out.println("==============================="); Person p1 = new Person(); p1.name="xx"; Person p2 = new Person(); p2.name="xx"; System.out.println(p1.name.equals(p2.name));//T System.out.println(p1.name==p2.name);//T System.out.println(p1.name=="xx");//T System.out.println(p1.name.intern()=="xx");//T System.out.println(p1.name.intern()==p1.name);//T System.out.println("=============================="); String s1=new String("yy"); String s2=new String("yy"); String s3="y"+"y";//会优化成 String s3="yy"; 只创建一个对象 常量相加看池 System.out.println(s1==s2);//F 指向不同堆 System.out.println(s1==s3);//F System.out.println(s1.intern()==s3);//T System.out.println("==============================="); String e="hello"; String f="world"; String g=e+f;//g先指向堆中数组对象,数组再指向常量池“helloworld” 变量相加看堆 //共创建三个对象 String h="helloworld"; System.out.println(g==h);//F System.out.println(g.intern()==h);//T } } class Person{ public String name; }
package chapter16.String类; public class String02 { String str =new String("xx"); final char[] ch={'j','a','v','a'}; public void change(String str,char ch[]){ str ="java";//change里的str直接指向常量池,不影响对象的str指向xx ch[0]='h';//change里的ch指向了对象的ch,并修改了第一个值 } public static void main(String[] args) { String02 ex = new String02(); System.out.println(ex.ch);//java ex.change(ex.str, ex.ch); System.out.print(ex.str+"and");// “xx” + “and” System.out.println(ex.ch);//hava } }
package chapter16.String类; public class StringMethod { public static void main(String[] args) { String str1="hello"; String str2="Hello"; System.out.println(str1.equals(str2));//F 区分大小写 System.out.println(str1.equalsIgnoreCase(str2));//T 不区分大小写 System.out.println(str1.length());//5 System.out.println(str2.indexOf('e'));//1 找字符索引,找不到返回-1 System.out.println(str2.indexOf('y'));//-1 System.out.println(str1.substring(1));//ello 截取制定索引位置后边字符串 System.out.println(str1.substring(1,3));//el [1,3)个字符串 System.out.println(str1.indexOf("llo"));//2 第一次出现的索引 System.out.println(str1.lastIndexOf("lo"));//3 最后一次出现的索引 System.out.println(str1.indexOf("rr"));//-1 System.out.println(str1.toUpperCase());//HELLO 换成大写 System.out.println(str2.toLowerCase());//hello 换小写 System.out.println(str1.concat(str2));//helloHello 拼接 System.out.println(str1.replace('l','u'));//heuuo 将所有l换成u System.out.println(str1.replace("he","HH"));//HHllo 换字符串 System.out.println("====================="); String[] s = str1.split("l");//以 l 为分隔符 将字符串分隔为字符串数组 l变成空格回车 特殊字符首尾要加转义符“\” for (int i = 0; i < s.length; i++) { System.out.println(s[i]); } System.out.println("====================="); char []chs=str1.toCharArray(); for (int i = 0; i < chs.length ; i++) {//转化成字符数组 System.out.println(chs[i]); } System.out.println("======================"); String a="jaui"; String b="jcty"; String c="ja"; String d="gh"; System.out.println(a.compareTo(b));//-2 从前往后比较字母大小,得到差值 System.out.println(a.compareTo(c));//2 前面字母一致,长度不一致,则是长度之差 System.out.println(a.compareTo(d));//3 System.out.println("======================="); String name="John"; int age =10; double score=98.3; char gender='男'; String info=String.format("我的名字是%s年龄%d,成绩是%.2f性别是%c",name,age,score,gender);//占位符填信息 System.out.println(info); } }
StringBuffer
package chapter16.String类; public class StringBuffer01 { public static void main(String[] args) { //StringBuffer 的直接父类是 AbstractStringBuilder //StringBuffer 实现了 Serializable 即StringBuffer对象可串行化 //在父类AbstractStringBuilder中 有属性 char[] value,不是final 存放字符串内容,因此是存放在堆中 //StringBuffer 是final类 不能被继承 //因为StringBuffer 字符内容是存在 char[] value中,增加的字符补上空的空间即可 不用每次更换地址(创建新对象)除非数组空间不够 StringBuffer stringBuffer = new StringBuffer();//创建一个容量为16的char[] 存放字符内容 StringBuffer stringBuffer1 = new StringBuffer(100);//可通过构造器指定空间大小 StringBuffer hello = new StringBuffer("hello");//给一个字符串str创建StringBuffer char[]的大小就是 str+16 //String>StringBuffer String str="hello tom"; StringBuffer stringBuffer2 = new StringBuffer(str);//方式1 返回StringBuffer对象,对str本身没影响 StringBuffer stringBuffer3 = new StringBuffer(); stringBuffer3=stringBuffer3.append(str);//方式2 //StringBuffer>String StringBuffer stringBuffer4 = new StringBuffer("xx");//方式1 使用StringBuffer 提供的toString String s=stringBuffer4.toString(); //使用构造器来搞定 String s1=new String(stringBuffer4);//方式2 用构造器 } }
package chapter16.String类; public class StringBuffer02 { public static void main(String[] args) { StringBuffer s = new StringBuffer("hello"); //添加append s.append(','); s.append("张三丰"); s.append("赵敏").append(100).append(true).append(10.5);//转成字符 System.out.println(s); //删除 s.delete(11,14);//删除[11,14)内容 System.out.println(s); s.replace(9,11,"哈哈");//替换[9,11)内容为哈哈 System.out.println(s); System.out.println(s.indexOf("张三丰"));//查找字符串第一次出现的索引 s.insert(9,"赵敏");//在索引9位置插入字符串 System.out.println(s); System.out.println(s.length());//字符串长度 } }
package chapter16.String类; public class StringBufferExercise01 { public static void main(String[] args) { String str=null; StringBuffer sb = new StringBuffer(); sb.append(str);//底层调用的是AbstractStringBuilder 的appendNull System.out.println(sb.length()); System.out.println(sb); } }
package chapter16.String类; import java.util.Scanner; public class StringBufferExercise02 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); StringBuffer sb = new StringBuffer(scanner.next()); for (int i=sb.lastIndexOf(".")-3; i>0; i-=3) {//先-3后判断 不然会有最前面加, sb=sb.insert(i,","); } System.out.println(sb); } }
StringBuilder
package chapter16.String类; public class StringBuilder01 { public static void main(String[] args) { /* 1 继承AbstractStringBuilder类 2.实现了Serializable, CharSequence 可串行化(网络传输,保存到文件) 3.是final类 不能被继承 4.StringBuilder 对象字符序列仍然是存放在父类 AbstractStringBuilder 的 char[] value 因此字符序列存放堆中 5.StringBuilder 方法没有做互斥处理,即没有synchronized关键字 因此单线程情况下使用StringBuilder 效率最高 */ StringBuilder stringBuilder = new StringBuilder(); //使用同 StringBuffer } }