String reference type
1. The String type is a reference type.
1. String s = "aaa";
In this code, JVM creates a variable reference S, creates an object aaa in the heap, puts aaa into the constant pool, and s points to aaa.
2 , the difference between " == " and equals
1. == can be used to compare basic types and reference types, determine content and memory addresses
2. equals can only be used to compare reference types , it only judges the content. This function exists in the ancestor class java.lang.Object
3. The data types in java can be divided into two categories: 1. Basic data types, also known as primitive data types. The comparison between byte, short, char, int, long, float, double, boolean uses the double equal sign ( == ) to compare their values. 2. When comparing composite data types ( classes ) with ( == ), they compare their storage addresses in memory,
Code representation in the JVM
1. String str1="Tulun"; 2. String str2="Tulun"; 3. System.out.println(str1==str2);//true 4. System.out.println("-----------");
Because Str1 and str2 point to the same address , the code outputs true
1. String str3=new String("Tulun"); 2. String str4=new String("Tulun"); 3. System.out.println(str3==str4); //false
Because str3 and str4 do not point to the same address
1. String str5="Tulun"; 2. String str6=new String("Tulun"); 3. System.out.println(str5==str6);//false
1. String str7="Tu"+"lun"; 2. String str8=new String("Tulun"); 3. System.out.println(str7==str8);//false
1. String str7="Tu"+new String("lun"); 2. String str8=new String("Tulun"); 3. System.out.println(str7==str8);//false
1. char[] array={'T','u','l','u','n'}; 2. String str9=new String(array); 3. String str10="Tulun"; 4. System.out.println(str11==str22);//false
The original code of the equals method 1. public boolean equals(Object anObject) { 2. if (this == anObject) { //Determine whether it is the same reference 3. return true; 4. } 5. if (anObject instanceof String) { //Determine whether the types are the same 6. String anotherString = (String)anObject; 7. int n = value.length; 8. if (n == anotherString.value.length) { //Determine whether the length is the same 9. char v1[] = value; 10. char v2[] = anotherString.value; 11. int i = 0; 12. while (n-- != 0) { //Check whether each character is the same 13. if (v1[i] != v2[i]) 14. return false; 15. i++; 16. } 17. return true; 18. } 19. } 20. return false; 21. }
1. Compare the reference first, the reference equality is directly true
if not:
1. Compare Types
2. Compare lengths
3. Convert the string to char[] , compare each character in turn, if all are equal, it is true , otherwise it is false .
1. Suppose there is the following code----Tencent 2. String s = "hello"; 3. String t = "hello"; 4. char c[] = {'h', 'e', 'l', 'l', 'o'}; 5. 6. s.equals(t); 7. t.equals(c);//false 8. s==t; 9. t.equals(new String ("hello"))
The output value of t.equals(c) is negative
1 , the reference addresses of t and c are different
Then judge whether their types are the same, because one is String and the other is char[] array. so the output is false
Type Conversion Interview Questions
Choice: B
A: There is no need to add S after 99 , because this sentence needs to perform automatic boxing and call the shortValue () method, which obviously cannot or the value of 99S
You need to add f after C:1.0 because the default floating point number of the system is double type
No need to add c after D:17
Choice: D
You can't have 8 in hexadecimal
Choice: A
S+1 is int and cannot be directly assigned to short
+= It will first convert the latter to the same type as the former and then sum