1,Short s1 = 1; s1 = s1 + 1;What's wrong?? short S1 = 1; s1 + = 1;What's wrong??
For short s1 = 1; s1 = s1 + 1; because s1+1 The operation automatically boosts the type of the expression, so the result is int short type s1
For short s1 = 1; s1 + = 1; because += is a Java language-defined operators, Java it is specially handled by the compiler, so it can be compiled correctly.
2,CharA Chinese character can be stored in a type variable?Why?
Char variables are used to store Unicode -encoded characters, andUnicode - encoded character sets contain kanji, sochar The type variable can of course store Chinese characters. However, if a particular Chinese character is not included in the Unicode encoding character set, then this char variable cannot store this particular character. Supplemental Note:Unicode encoding consumes two bytes, so variables of type char are also two bytes.
Note: The latter part of the answer, although not in the positive answer, but, in order to show their knowledge and performance of their own understanding of the depth of the problem, can answer some relevant knowledge, do sparing, liker.
3, using the most efficient method to calculate2Multiply8Wait a few?
2 << 3,
Because moving a number to the left N -bit is the equivalent of multiplying the n -squared by 2 , a number multiplied by 8 simply shifts it to the left 3 -bit, while the bit computing CPU directly supports the most efficient, so,2 times 8 The most efficient way to wait for a few is 2 << 3.
4, please design a 10 billion calculator
First of all to understand the question of the test point is what, first of all, we have to understand the underlying details of the computer principle, to know the bit operation principle of addition and subtraction, and know that the arithmetic operations in the computer will be out of bounds, the second is to have a certain object-oriented design ideas.
First, the computer uses a fixed number of bytes to store the values, so the computer can be expressed in a certain range of values, in order to facilitate the interpretation and understanding, we firstbyteFor example, an integer of type1bytes to be stored, representing a maximum range of values-128to the+127. -1The binary data corresponding in memory is11111111, if two-1Add, not considerJavathe type promotion of the operation, which generates a carry after the operation, the binary result is1,11111110, because the rounding is more thanbytetype of storage space, so the carry part is discarded, that is, the final result is11111110, namely-2, which is the use of overflow to achieve the operation of negative numbers. -128The binary data corresponding in memory is10000000, if two-128Add, not considerJavathe type promotion of the operation, which generates a carry after the operation, the binary result is1,00000000, because the rounding is more thanbytetype of storage space, so the carry part is discarded, that is, the final result is00000000, namely0That the result is obviously not what we expected, which showsThe arithmetic operations in a computer are considered to be out of bounds, and the result of two numeric values cannot exceed the range of values of that type in the computer. BecauseJavaThe types that are involved in expression arithmetic are automatically promoted and we cannot usebytetype To do experiments that demonstrate this problem and phenomenon, you can use the following example program to experiment with integers to try it out:
int a = Integer.max_value;
int b = Integer.max_value;
int sum = a + b;
System.out.println ("a=" +a+ ", b=" +b+ ", sum=" +sum ");
The long type is not considered first , since the positive range of int is 2 , the maximum value represents approximately equal to 2*1000*1000*1000, that is, the size of billions, so to achieve a 10 billion calculator, we have to design a class can be used to represent a large number of integers, and provides the function of subtraction with another integer, the approximate function is as follows:
() There are two member variables within this class, one representing the symbol and the other using a byte array to represent the binary number of the value
() has a constructor that converts a string containing a multi-digit value into an internal symbol and byte array
() provides subtraction functionality
public class BigInteger
{
int sign;
Byte[] Val;
Public Biginteger (String val)
{
sign =;
val =;
}
Public BigInteger Add (BigInteger Other)
{
}
Public BigInteger Subtract (BigInteger other)
{
}
Public BigInteger Multiply (BigInteger other)
{
}
Public BigInteger Divide (BigInteger other)
{
}
}
Note: To write this class of complete code, is very complex, if interested, you can see the jdk in the java.math.BigInteger class source. The interviewer also knows no one can write this kind of complete code in a short time, he wants to be you have this aspect of concept and consciousness, his most important is to examine your ability, so, you do not because you cannot write the complete final result to give up this problem, you have to do is you write more than others, Prove that you are stronger than others, you have this aspect of the ideological consciousness can be, after all, others may even the meaning of the topic do not understand, what has not written, you have to dare to answer this question, even if only a part, that also with those who do not understand the difference out of the person, opened the distance, is the dwarf in the tall, In addition, the framework code in the answer is also very important, embodies a number of object-oriented design skills, especially the method named very professional, with the English word is very accurate, this is the ability, experience, professionalism, English level and other aspects of the embodiment, will give people a good impression, In the case of programming ability and other conditions, English is good except for you to get more opportunities, salary can be 1000 yuan higher.
Java Basic Syntax _2