Java Interview (1) -- Basic Part (1)

1. Can a ".java" source file include multiple classes (not inner classes)? What are the restrictions?
Answer: There can be multiple classes, but there can only be one public class, and the public class name must be consistent with the file name. .

 

2. Does Java have goto?
Answer: The reserved words in java are not used in java now.

 

3. Talk about the difference between & and &&.
Answer: Both & and && can be used as logical AND operators, which means logical AND (and). When the results of the expressions on both sides of the operator are both true, the entire operation result is true, otherwise, as long as one side is false , the result is false. && also has the ability to short-circuit, that is, if the first expression is false, the second expression is no longer evaluated, for example, for if(str != null && !str.equals("")) expression, when When str is null, the following expression will not be executed, so there will be no NullPointerException If you change && to &, NullPointerException will be thrown. If(x==33 & ++y>0) y will grow, If(x==33&& ++y>0)y will not grow. & can also be used as a bitwise operator. When the expressions on both sides of the & operator are not of boolean type, & represents a bitwise AND operation. We usually use 0x0f to perform & operation with an integer to get the lowest 4 bits of the integer. Bits, for example, 0x31 & 0x0f result in 0x01.

 

4. How to get out of the current multiple nested loops in JAVA?
Answer: In Java, if you want to jump out of multiple loops, you can define a label before the outer loop statement, and then use the break statement with the label in the code of the inner loop body to jump out of the outer loop. E.g,

ok:
for(int i=0;i<10;i++) {
    for(int j=0;j<10;j++) {
        System.out.println(“i=” + i + “,j=” + j);
        if(j == 5) break ok;
    }
}        


In addition, I usually don't use labels, but let the result of the outer loop conditional expression be controlled by the inner loop body code, for example, to find a certain number in a two-dimensional array.

int arr[][] = {{1,2,3},{4,5,6,7},{9}};
boolean found = false;
for(int i=0;i<arr.length && !found;i++) {
    for(int j=0;j<arr[i].length;j++){
        System.out.println(“i=” + i + “,j=” + j);
        if(arr[i][j] == 5) {
            found = true;
            break;
        }
    }    
}

 

5. Can the switch statement act on byte, long, or String?
Answer: In switch(expr1), expr1 can only be an integer expression or an enumeration constant (larger font). ), the integer expression can be an int basic type or an Integer wrapper type. Since byte, short, and char can be implicitly converted to int, these types and the wrapper types of these types are also possible. Obviously, the long and String types do not conform to the syntax of switch and cannot be implicitly converted to int, so they cannot be used in switch statements.

 

6. What's wrong with short s1 = 1; s1 = s1 + 1;? short s1 = 1; s1 += 1
; When the type of the expression is automatically promoted, so the result is of type int, and when it is assigned to the type of short s1, the compiler will report 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.                           

 

7. Can a Chinese character be stored in a char type variable? Why?
Answer: A 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 a char type variable. . However, if a special Chinese character is not included in the unicode coded character set, the 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.                              

 

8. How much is 2 times 8 in the most efficient way?
Answer: 2 << 3,
reason: because shifting a number to the left by n bits is equivalent to multiplying by 2 to the nth power, then, a number To multiply by 8, just shift it to the left by 3 bits, and the bit operation is directly supported by the CPU and has the highest efficiency. Therefore, the most efficient way to multiply 2 by 8 is 2 << 3.

         

Guess you like

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