A collection of basic Java problems

1. Can a Chinese character be stored in a char variable ? Why ?  

   The char type variable is used to store Unicode-encoded characters. The unicode-encoded character set contains Chinese characters, so Chinese characters can of course be stored in the char type variable. However, if a special Chinese character is not included in the unicode encoded character set,

   Then, this special Chinese character cannot be stored in this char variable. Supplementary note: unicode encoding occupies two bytes, so variables of type char also occupy two bytes.

2. Self-increase and self-decrease problem

int x = 4; 
int y = (x++) + (++x) + (x*10); //5 + 5 + 60
The increment and decrement operations are after the operation, that is, a=a++; ) and then self-increment, self-build
, self-increase, and self-build operation in front of the operation, that is, a=--a; first self-increment and self-decrement before operation (assignment)

3. What is wrong with short s1 = 1; s1 = s1 + 1 ;?   What is wrong with short s1 = 1; s1 += 1;

   For short s1 = 1; s1 = s1 + 1; Since the type of the expression is automatically promoted during the operation of s1+1, the result is an int type, and when it is assigned to a short type s1, the compiler reports an error that the type needs to be cast

   For short s1 = 1; s1 += 1; Since += is an operator specified by the java language, the java compiler will treat it specially, so it can be compiled correctly.

   The correct spelling is:

s = (byte) (s+1);

4. Two types of coercion

float f = 12.3f; 
long g = 12345;
f = g; //implicit conversion
g = (long)f; //cast

5. Byte out-of-bounds problem, are byte b = 130 and byte b = 300 correct?

// byte b = 130; 
// because the range of bytes is: -128 to 127. (-2^7--2^7-1), and 130 is not in this range, so an error is reported.
// We can cast
byte b = (byte)(126 + 4);
System.out.println(b); //-126
byte b2 = (byte)300;
System.out.println(b2); //44

 Get the binary of 130 this data. 00000000 00000000 00000000 10000010 This is the original code of 130, also the inverse code, or the complement code.
 Do the interception operation and cut it into byte type. 10000010 This result is two's complement.
Know the complement to find the original code.

Sign bit Value bit
Complement code: 1 0000010 Complement code
 : 1 0000001
 Original code: 1 1111110

6. Two ways to retain decimals

System.out.println(-10/3.0); 
System.out.println( (float)Math.round( (-10/3.0) *1000 )/1000); //Keep 3 as a decimal, Math.round
System. out.println(new DecimalFormat("0.00").format(-10/3.00)); //Keep 2 decimal places, DecimalFormat.format()

7, evaluate -3 % 5

System.out.println(-3 % 5);//-3

8. Do not introduce a third value or variable, exchange the values ​​of 2 data

int x = 10;int y = 5;
x = x ^ y; // 10 ^ 5
y = x ^ y; // 10 ^ 5 ^ 5 y = 10
x = x ^ y; // 10 ^ 5 ^ 10 x = 5
System.out.println("x = " + x + ",y = " + y);

9, the most efficient calculation of the 3rd power of 2

System.out.println(2<<3); 
Shifting a number to the left by n bits is equivalent to multiplying it by the nth power of 2. Then, multiplying a number by 8 only needs to shift it to the left by 3 bits, and Bit operation CPU directly supports, the highest efficiency


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325151916&siteId=291194637