11. The difference between String and StringBuffer

String str = "abc"; str.replace('a','d'); Syso(str); //abc StringBuffer sbf = new StringBuffer("abc"); SBF. Replace (0, 1, "DE"); Syso(sbf); //debc str1 + str2 //String concatenation sbf1. appEnd (sbf2) //StringBuffer concatenation of strings with higher efficiencyCopy the code

The ** String class is immutable; once assigned, it cannot be modified. A StringBuffer is a mutable string. Strings are appended using +, whereas stringBuffers can be appended using the append method, which is more efficient in most cases. **

12. What is the difference between StringBuffer and StringBuilder?

** StringBuilder is a new class added to JDK5 that has almost the same functionality as StringBuffer. The difference is that StringBuffer is thread-safe and can be synchronized if multiple threads operate it at the same time. StringBuilder is thread-unsafe. StringBUilder is even more efficient when used only in a single-threaded environment. **

13. Private modifiers

Title: Which line of the following code causes a compilation error, and why? public class TestPrivate{ void doSomething(){ private String s = ""; int I = s.length(); }} // Invalid modifierCopy the code

** Because the string s is a local variable defined in a method. Local variables cannot use permission access modifiers. Permission access modifiers include public, protected, default, and private. Classes can only use public or default modifiers, and class properties, methods, and constructors can use any of the four modifiers. Method local variables cannot use any permission modifiers. **

14. Do while loop with ++, —

public static void main(String[] args){ int a = 1; int b = 10; do{ b-=a; a++; }while(b--<0); Syso("a="+a+" b="+b); } a=2,b=8 b= 0Copy the code

** The do while loop executes the body at least once and exits until the while condition is false. In this case, the loop exits after executing the body once. Where a increments itself once and has a value of 2. B is changed in the body of the loop to 10-1=9, and b– is executed in the while, after a decrement, so 8. Note that the value of the variable itself is added or subtracted by 1, regardless of whether it is preceded or followed by the expression. **

15.? The: = operator

Title: int a = 5; Syso(" +((a<5)? 9.9:9); 9.0Copy the code

* *? : is the only ternary operator in Jav, similar to simple if-else logic. If the condition returns true, the first value is returned, otherwise the second value is returned. **

16: Float and double

Double d = 1.0; Float f = 1.0; // error float f = (float) 1.0/1.0f;Copy the code

** Floating point numbers are stored as 64-bit double by default, while floats are 32-bit. Assigning a high-precision value to a low-precision variable causes a compilation error. You can use the suffix f, or use (float) for type conversions. **

17. Use == to compare basic data types

Syso (= = 0.3 0.3 f); Syso (= = 0.5 0.5 f);Copy the code

** false true Uses == to compare base data types, comparing binary values of numeric values. By default, decimals are stored as 64-bit double, so the binary values for 0.3 and 0.3f are different, while the binary values for 0.5 and 0.5f are the same. **

18. Use a char to store Chinese characters

Title: Can the Java char type store Chinese characters?Copy the code

** Java uses Unicode encoding by default, requiring two bytes for a Chinese character. The char type is 16-bit, double-byte, representing a character, and thus can store a Chinese character. But you can’t store two letters. **

19. Logical operators

Both && are logical "and" operators in Java that return true if both sides of the operator are true, and false otherwise. The difference is that && is short-circuited. That is, when the expression to the left of && returns false, the expression to the right is not evaluated.Copy the code

20. Shift operators

Title: Which method is the most efficient for calculating 2*8? In simple code. r1 = m<<3; r2 = m * 8;Copy the code

** There are three shift operators in the Java language: left shift <<, signed right shift >>, and unsigned right shift >>>. Moving one bit to the left is the same thing as multiplying by 2, moving one bit to the right is the same thing as dividing by 2, and using the shift operator, it’s doing the binary directly, it’s more efficient. **