一.String类是final的,是不可变的。
String类型的变量是不可修改的,每次修改都会创建一个新的对象。
二.equales
1.equals比较的是两个对象的实际内容是不是相同(equals()的默认行为是比较引用,而java内部类已经对其进行了修改,改为比较的是两个对象的实际内容)。
2.对于java内部的类 如String new String().equals(new String()) 就是true,自己定义的类要想new出来的值equals为true,必须重写hashCode()。
3.==比较的是两个引用是不是指向同一个内存地址。
/* 时间:2016年1月22日16:41:37 目的:String类的equals方法的使用。 */ public class String_1 { public static void main(String[] args) { String str1 = new String("china");//new 出来的位于堆中,str1指向一个空间,str2指向另一个空间。 String str2 = new String("china"); System.out.println(str1.equals(str2));//判断指向的内容是否相等 if (str1 == str2) //判断是否指向同一地址 System.out.println("str1 == str2"); else System.out.println("str1 != str2"); String str3 = "china";//此时的china在数据区存放,str3和str4共同指向一个china。 String str4 = "china"; if (str3 == str4) System.out.println("str3 == str4"); else System.out.println("str3 != str4"); } } /* ***************结果*************** true str1 != str2 str3 == str4 */
4.equales方法的重写。
/* 时间:2016年1月22日08:43:36 目的:equals方法的重写。 */ class A { public int i; public A(int i) { this.i = i; } public boolean equals(Object obj) { A aa = (A)obj; if (this.i == aa.i) return true; else return false; } } public class Equals_2 { public static void main(String[] args) { A aa1 = new A(2); A aa2 = new A(2); System.out.println(aa1.equals(aa2)); } } /* **************结果*************** true */
三.常用方法(查api)
1.valuOf()
2.length()、charAt()/* 时间:2016年1月22日17:08:24 目的:String类的valuOf方法和Integer的parseInt方法。 */ public class String_2 { public static void main(String[] args) { int i = 123; String str = "465"; //i = str//错误 //str = i//错误 str = String.valueOf(i);//将整型转换成字符串型 System.out.printf("str = %s\n", str); i = Integer.parseInt(str); System.out.printf("i = %d\n", i); } } /* ***************结果*************** str = 123 i = 123 */
3.以某种编码格式输出/* 时间:2016年1月24日08:21:04 目的:String常用方法举例。 */ public class String_3 { public static void main(String[] args) { String str = "abAM1,!23"; int cntU = 0;//大写字母的个数。 int cntL = 0;//小写字母的个数。 int cntOther = 0; int i; for (i=0; i<str.length(); i++) { char ch = str.charAt(i); if (ch>='A' && ch<='Z')//或者 if (Character.isUpperCase(ch)) { cntU++; } else if (ch>='a' && ch<='z')//或者 if (Character.isLowerCase(ch)) { cntL++; } else { cntOther++; } } System.out.printf("大写字母个数:%d\n", cntU); System.out.printf("小写字母个数:%d\n", cntL); System.out.printf("其他字母个数:%d\n", cntOther); } } /* ***************结果*************** 大写字母个数:2 小写字母个数:2 其他字母个数:5 */
import java.io.UnsupportedEncodingException; /** * date:2017年6月1日10:55:31 * target:以某种编码格式输出。 * @author ypy */ public class String_5 { public static void main(String[] args) { String str = "海阔天空"; try { String str2 = new String(str.getBytes("ISO-8859-1"));//输出ISO-8859-1编码格式 //String str2 = new String(str.getBytes("ISO-8859-1"),"UTF-8");//将ISO-8859-1替换 为UTF-8编码 //str2 = str2.trim(); //复制一个副本,并去除开头,结尾的空白 System.out.println(str2); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } } /* *************** jdk1.8运行结果*************** ???? */
四.StringBuffer、StringBuilder、String区别
1.String是不可变的,是字符串常量,操作少量数据时使用,尽量少用"+或+="。
2.StringBuffer是可变的,是线程安全的。
3.StringBuilder是可变的,是非线程安全,适用于单线程,效率高。
/** * date:2017年5月26日09:03:13 * target:String,StringBuffer,StringBuilder。 * @author ypy * conclusion: * String是不可变的,是字符串常量,操作少量数据时使用,尽量少用"+或+="。 * StringBuffer是可变的,是线程安全的。 * StringBuilder是可变的,是非线程安全,适用于单线程的,效率高。 */ public class Str_StrBuf_StrBud { public static void main(String[] args) { String s = "m"; //String类中的成员用final声明,是不可变的 System.out.println(s.hashCode()); s = "y"; //此时重新赋值实际是重新创建了一个String对象 System.out.println(s.hashCode()); } } /* *************** jdk1.8运行结果*************** 109 121 */