关于对“= =”;“compareTo”;“equals”;三者的对比
“= =” 主要是两个变量值的比较,返回值为true 或者是false;
"equals()" 主要是用来比较两个对象(引用)是否相同
“compareTo”该方法用于判断一个字符串是大于、等于还是小于另一个字符串,判断字符串大小的依据是根据它们在字典中的顺序决定的,若二者相等则返回值等于0,若前者大于后者则返回值大于0。
public class Duibi {
public static void main(String[] args) {
Character a = new Character('a');
Character b = new Character('a');
Character h = new Character('c');
Boolean x = a.compareTo(b)==0;
Boolean g = h.compareTo(b)>0;
Boolean z = a.equals(b);
Boolean v = (a==b);
System.out.print("x值为"+x+" "+"g值为"+g+" "+"z值为"+z+" "+"v值为"+v+" "+"\n");
System.out.print("\\\\\\\\\\\\"+"\n");
String na1 = "nihao";
String na2 = new String("nihao");
int num1 = 35;
int num2 = 35;
Boolean e = (na1==na2); //na1和na2在内存中的地址是不一样的,所以返回的值是一个false
Boolean q = (num1==num2);
Boolean f = na1.equals(na2);System.out.print(e+" "+q+" "+f);
System.out.print("\n"+"\\\\\\\\\\\\"+"\n");
String an= "Niagara. O roar again!";
char aChar = an.charAt(9); // 输出指定位置的字符
System.out.print(aChar);
}
}
运行结果为:
x值为true g值为true z值为true v值为false
\\\\\\
false true true
\\\\\\
O