String a="abcd" 相等 String b="abcd"
String a=new String("abcd") 不等于 String b=new String("abcd") 字符串池内存地址不同
对象不可变 常量
"abcd"+"a" 拼接 等于新创建了对象 abcda
面向对象的优点
便于程序模拟现实世界中的实体
隐藏细节
可重用
java对象的内存管理机制
java垃圾回收器:回收堆内存的空间
案例:
package com.tanlei.newer;
public class Employee {
public String name;
public int age;
@Override
public String toString() {
return "我的名字叫"+name+",今年"+age+"岁";
}
/*
* src 朋友啊朋友,你是我最好的朋友
* dst 朋友
*/
//在指定的字符串中查找相应的字符串出现的次数
public int countContent(String src,String dst) {
int count=0;//计算器
int index=0;//保存找到朋友的下标
index=src.indexOf(dst);
//当首次出现的下标不为-1
while(index!=-1) {
count++;
index+=dst.length();//指定从哪个下标找
index=src.indexOf(dst,index);
}
return count;
}
public static void main(String[] args) {
Employee employee=new Employee();
employee.name="张三";
employee.age=30;
System.out.println(employee.toString());
String src= "朋友啊朋友,你是我最好的朋友";
String dst= "朋友";
System.out.println(employee.countContent(src, dst));
}
}