Java程序中测试两个变量是否相等有两种方式:一种是利用==运算符,另一种是利用equals()方法。当使用==来判断两个变量是否相等时,如果两个变量是基本类型变量,且都是数值类型(不一定要求数据类型严格相同),则只要两个变量的值相等,就将返回true。
但对于两个引用类型变量的时候,只有它们指向同一个对象时,==判断才会返回true。==不可用于比较类型上没有父子关系的两个对象。
package knowledge;
public class B {
public static void main(String[] args) {
String s1=new String("a")+"bc";
String s2="ab"+new String("c");
String s3="abc";
String s4=new String("abc");
String s5="ab"+'c';
String s6=new String("ab")+new String("c");
String s7="AbC";
//==运算符只能检测s1和s2是否指向同一个对象
//不能判断是否具有相同的内容
System.out.println("--------------");
System.out.print((s1==s2)+"\t"); //false
System.out.print((s1==s3)+"\t"); //false
System.out.print((s1==s4)+"\t"); //false
System.out.print((s1==s5)+"\t"); //false
System.out.println();
System.out.print((s2==s3)+"\t"); //false
System.out.print((s2==s4)+"\t"); //false
System.out.print((s2==s5)+"\t"); //false
System.out.print((s3==s4)+"\t"); //false
System.out.println();
System.out.print((s3==s5)+"\t"); //true
System.out.print((s4==s5)+"\t"); //false
System.out.print((s1==s6)+"\t"); //false
System.out.println((s4==s6)+"\t"); //false
//equals()方法对对象的内容进行比较
//equalsIgnoreCase方法,忽略大小写,检查字符是否相等
System.out.println("--------------");
System.out.print(s1.equals(s2)+"\t"); //true
System.out.print(s1.equals(s3)+"\t"); //true
System.out.print(s1.equals(s4)+"\t"); //true
System.out.print(s1.equals(s5)+"\t"); //true
System.out.println();
System.out.print(s2.equals(s3)+"\t"); //true
System.out.print(s2.equals(s4)+"\t"); //true
System.out.print(s3.equals(s4)+"\t"); //true
System.out.println(s3.equalsIgnoreCase(s7)); //true
System.out.println("--------------");
System.out.print((s1.intern()==s2.intern())+"\t"); //true
System.out.print((s1.intern()=="abc")+"\t"); //true
System.out.print((s2.intern()==s4.intern())+"\t"); //true
System.out.println((s4.intern()==s5.intern())+"\t"); //true
System.out.println("--------------");
String a1="abc";
String a2="abg";
String a3="deg";
String a4=" Hellow World! ";
//compareTo方法返回值的大小实际上依赖于s1和s2从左到右第一个不同字符之间的距离
//注意只计算第一个字符之间的距离 返回0、正整数和负整数
//length计算字符串中字符的个数
//charAt返回字符串中指定下标的字符值
//toUpperCase 将所有字符转换为大写的新字符串
//toLowerCase 将所有字符转换为小写的新字符串
//trim返回将字符串两侧空白字符删除后的字符串
//toString返回包含自身的新字符串
//replaceAll(old,new) 用新的子串替换该字符串中所有匹配的子串
//replaceFirst(old,new) 用新的子串替换该字符串中第一个匹配的子串
System.out.print(a1.compareTo(a2)+"\t"); //-4
System.out.println(a3.compareTo(a2)); //3
System.out.println("-----------");
System.out.print(a1.length()+"\t"); //3
System.out.println(a1.charAt(2)); //c
System.out.println("-----------");
System.out.println(a1.toUpperCase()+"\t"); //ABC
System.out.println(a4.toLowerCase()+"\t"); // hellow world! 注意:前面有空格
System.out.println("-----------");
System.out.println(a4.trim()+"\t"); //hellow world!
System.out.println(a4.toString()); // hellow world! 注意:前面有空格
System.out.println("-----------");
System.out.println(a4.replaceFirst("e", "E!")); // HE!llow World! 注意:前面有空格
System.out.println(a4.replaceAll("e", "EE")); // HEEllow World! 注意:前面有空格
//toCharArray方法可以将字符串转换成一个字符的数组
System.out.println("-----------");
String w1="Welcome to China!";
char [] chars=w1.toCharArray();
for(int i=0;i<chars.length;i++){
System.out.print(chars[i]+"\t");
if(i%7==0){
System.out.println();
}
}
System.out.println();
System.out.println("-----------");
//Integer.parseInt(s) 方法用于将字符串3转化为int值
//Double.parseDouble(s) 方法用于将字符串3转化为int值
String w2="23";
int a=Integer.parseInt(w2);
System.out.print(a+"\t\t"); //23
System.out.println(a-2); //21
String w21="23.45";
double a22=Double.parseDouble(w21);
System.out.println(a22+"\t\t"); //23.45
System.out.println(a22-2.0); //21.45
//String.valueOf(dst)将字符数组转换为字符串
char [] dst={'J','A','V','A',' ','N','i','c','e'};
String dst1=String.valueOf(dst);
System.out.println(dst1); //JAVA Nice
System.out.println("-----------");
//concat方法和+都可以实现字符串的连接
//+可以连接字符串和数字。在这种情况下,先把数字转换为字符串,在连接
//注意:要实现连接,至少有一个操作数应必须为字符串
String b1="How Are You!";
String b2="45";
System.out.println(b1+b2); //How Are You!45
System.out.println(b1.concat(b2)); //How Are You!45
//substring(a,b) 返回一个新的字符串,他是该字符串的子串。
//子串从指定的a开始,扩展到下表为b-1的字符 子串的长度为b-a
String message=b1.substring(0,7)+"Mary!";
System.out.println(message); //How AreMary!
}
}
运行截图如下: