String 类位于 Java lang 包
String s="12345";
String s=new String();
String s=new String("Hello world");
创建对象时 在堆内存创建对象
在占内存中 形成应用
String s = "HelloWorld";
通知双引号直接创建:在字符串常量池中创建字符串
String s2 = s;
String s3 = "HelloWorld";
字符串常量池中的地址
System.out.println(s2 == s3);
String s4 = new String("HelloWorld");
通过new关键字创建 那就跟普通对象创建方式一样
System.out.println(s.equals(s4));
String s5 = "Hello";
String s6 = "World";
String s7 = s5 + s6;
String s8 = "Hello" + "World";
System.out.println(s == s8);
字符串的长度 length()方法
String s=“aaa”;
s.length();
字符串比较的方法 equals()方法
a.equals(b);
b.equals(a);
equals()比较原理
先比较长度
在比较每一位的数字
==和equals()有什么区别
== 基本数据判断值是否一样
引用数据类型判断是否是同一个对象
(根据首地址判断)
equals()
根据里面的值来判断是否为同一个对象
StringBuffer 类 :String 增强版
StringBuffer 声明
StringBuffer 类
StringBuffer 是可以被回收的
sb=null; 出于对内存的考虑建议使用StringBuffer