String类对象的创建使用,雇员对象的操作

来源:互联网 发布:java rmi 无继承 框架 编辑:程序博客网 时间:2024/06/04 18:31
public class StringDemo{public static void main(String[] args){//类中每一个看来会修改String值得方法,其实都是创建新的String对象(包含修改后的字符串内容)//"abcd"+"e"="abcde",看起来没变,其实内存中又开辟了新的内存空间//String的只读特效带来效率优化可能//字符串字面值存储于字符串池,避免反复生成重复的字符串实例//系统对String的非修改处理效率很高,远远超过了另外两个字符串类StringBuffer和StringBuilder(后续讲解)//假设相对字符串频繁的修改最好用后面两个类。String content="Hello,My Friend,You are my best friend";System.out.println(content.charAt(2));System.out.println(content.compareTo("hello"));content=content.concat("I lied");System.out.println(content);}}


public class Employee{public String name;public int age;//返回个人信息public String toString(){return "我的名字叫:"+name+",今年:"+age+"岁";}//在制定的字符串中查找相应字符串出现的次数public int countContent(String src,String dst){int count=0;//计数器int index=0;//下标index=src.indexOf(dst);while(index!=-1){count++;index+=dst.length();index=src.indexOf(dst,index);}return count;}public static void main(String[] args){Employee emp=new Employee();emp.name="张三";emp.age=30;System.out.println(emp.toString());String src="朋友啊朋友,你永远是我的朋友";String dst="朋友";System.out.println(emp.countContent(src,dst));}}




原创粉丝点击