Summary of Java Systematic Knowledge Points (1)
Review the knowledge points encountered when learning java in the sophomore year, and take the time to take a note, so that you can go back and learn when you forget.
package 基础问题练习;
public class 优先级1 {
public static void main(String[] args) {
String s1 = "ABC";
String s2 = "ABC";
System.out.println("s1 == s2 is:" + s1 == s2);
}
}
结果:false
Reason: In Java, the priority of the + operation method is greater than ==, so the output part of the expression is equal to "s1 == s2 is:ABC" == "runoob", and the result of the expression is false.
As shown in the figure below, the comparison of the execution results of System.out.println("s1 == s2 is:" + s1); and System.out.println("s1 == s2 is:" + s1 == s2);:
Reference to the same object:
public class 优先级 2{
public static void main(String[] args) {
String s1 = "ABC";
String s2 = "ABC"; // s1和s2是同一个字符串
System.out.println(s1==s2); //语句 1
System.out.println("s1 == s2 is:" + s1 == s2); // 语句2 中“+”的级别优先于“==”,所以,这句相当于"s1 == s2 is:" == s2,即判断"s1 == s2 is:ABC"=="ABC",结果为false。
System.out.println("s1 == s2 is:" + (s1 == s2)); // 语句 3
//语句1和语句3实际上是一样的。
}
}
结果:
true
false
s1 == s2 is:true
Open up new objects:
public class 优先级 2{
public static void main(String[] args) {
String s1 = "ABC";
String s2 = new String("ABC"); // 新开辟一个对象,地址不同,所以对比是false。
System.out.println(s1==s2);
System.out.println("s1 == s2 is:" + s1 == s2);
System.out.println("s1 == s2 is:" + (s1 == s2));
}
}
结果:
false
false
s1 == s2 is:false
Further graphic explanations here:
Comparison of basic data:
public class 优先级3 {
public static void main(String[] args) {
int i = 10;
int k = new Integer(10);
int j = k;
System.out.println(i==j);
System.out.println(i==k); //基本数据不受影响的。
}
}
结果:
true
true