I have a friend, called Old Liu, wearing a degree than I am also high myopia mirror, looks particularly “programmer”; Dress is also very “eclectic”, the upper body is a shirt suit, the lower body is jeans sneakers.

Lao Liu and I are very close. We have dinner together every weekend. This week, we ate knife-cut noodles from the famous Wu family in Luoyang. During the meal, he talked about an interesting interview experience. I listened with relish.

When the banquet was over, Lao Liu specially told me to sort out his conversation with the interviewer, because he thought this conversation was very wonderful and worth recommending to more young people who just learn Java.

Note: The following is a real conversation between Lao Liu and dongfeng, the interviewer. If there are similarities, please do not take a seat.

Old liu: “dongfeng, you are engaged in the development of financial software for a long time, the relevant data of record deposit and amount and so on use which kind of data type?”

Dongfeng: “of course float ah, precision is higher than double.”

Liu: “Dongfeng, are you sure double is less accurate than float?”

Dongfeng: “Of course, double is only accurate to two decimal places, the meaning of the word double is not the meaning of two?”

Old liu: “Dongfeng, your right hand side just have a” Java core technology volume 1 “, you turn to page 35, have a look.”

Dongfeng special: “… Oh, Manager Liu, no need. I’m sorry. I was just joking, to ease the tension of the interview. See your thick glasses hidden under a pair of deep eyes, I think you must have a lot of knowledge. In financial calculations, you must use BigDecimal; double and float are not suitable. Because a single accuracy problem can knock you out.”

“I remember one time I happened to be trying to compute an expression a-b, where A is 2 and B is 1.1. My niece, when she was five and a half years old, knew that the answer should be 0.9, and the program came up with 0.89999… “I was angry and excited that computers were not as reliable as my niece’s, and excited that I had found a Java bug for the first time.”

“I quickly reported the bug to the Bronze Age group of Silent King II, thinking I was going to get a thumbup. And received unanimous and merciless ridicule from the bosses!”

“Fortunately, the second brother of the group leader comforted me in time. He texted me and said, ‘First of all, computers do binary arithmetic, and the decimal numbers we put in are converted to binary, then converted to decimal output. Double and float provide fast arithmetic, but the problem is that when converted to binary, some numbers can’t be converted completely, only getting infinitely close to the original value, which results in incorrect results. ‘”

“My depressed mood was greatly relieved when I saw my second brother’s message. I delved into the problems with floating point and decimal numbers.”

“BigDecimal can represent decimals with arbitrary precision and evaluate them. Be careful with the BigDecimal(double) constructor, however, as it can cause rounding errors in the calculation. It is best to create BigDecimal objects using integer or String based constructors.”

Lao Liu: “Wow, that’s a good answer. Let’s move on to the next question. You should know that 2/0 of the program will be submitted to the Java lang. ArithmeticException mistakes, then you know the result of the 2.0/0?”

Dongfeng: “Manager Liu, I can’t answer your question. The result is Infinity. Sorry, my oral English is limited. It just means infinite. There is not only positive infinity, there is negative infinity, and there is even a special value called NaN. NaN stands for ‘not a number’. These values exist so that in the event of an error condition, the program can also use specific values to represent the results produced. These include arithmetic overflows, square roots of negative numbers, and dividing by zero as you said.”

Old liu: “dongfeng, your pronunciation is better than me, quite accurate.”

Dongfeng: “Manager Liu you smiled.”

Old liu: “I this still have a question about array, you wait a moment, I write on the paper once.”

int[] a = {1, 2, 3, 4}int[] b = {2, 4}int[] c = {1, 3}int[] d = {2}Copy the code

“There are four arrays with only one unique element in each array. That is, the elements of arrays A, B, C, and D cannot be the same. What are you going to do?”

Dongfeng: “Manager Liu, may I use your Lingmei pen?”

Old liu: “can, you please use.”

Dongfeng: “I made a rough calculation. Let me tell you what I think. D can only be 2, B can only be 4, A can only be 1 or 3, and C can only be 3 or 1. Iterates through the long array, eliminating the shortest array element contained in the long array. If you subtract the 2 from D from B, you’re left with 4, if you subtract the 2 from D from A, you’re left with 1, 3, 4, and c doesn’t have anything in D, so you don’t have to subtract it. You’re left with a 4 in B and a 2 in D. Iterate again to eliminate the 4 in A. We’re down to 1 and 3 in a and C, and we’re just going to get rid of the differences.”

“I feel more stupid practice, manager Liu you think feasible?”

Lao Liu: “Feasible, no problem. So, what do you think about naming variables and methods? Feel free to do whatever you want.”

Dongfeng: “I once saw an interesting poll on the blog park — to vote for the most difficult task in my daily work. The choices were roughly as follows:”

  • Write various documents

  • Communicate with customers

  • Estimated workload

  • Name variables

“To my complete surprise, the number one vote was’ Name variables’! Naming variables is one of the most common things in software development, but it’s not easy to do well.”

“Alibaba Java Development manual” mandatory “that method names, parameter names, member variables, local variables should use the lowerCamelCase style, must follow the camel form.

localValue // variable getHttpMessage() // methodCopy the code

“For a long time, I was torn between using pinyin and Using English words. Then I made up my mind: either use pinyin or English words, just see the name and know the meaning of the variable or method.”

“Sometimes it’s really hard to give variables a good name. In this case, I choose to save time and effort by naming the variable name as the type name. For example:”

Map map; List list;Copy the code

“Preferably, declare variables close to where they were first used. Otherwise, the code would be difficult to read because the human eye has a limited screen height.”

Old Liu: “Dongfeng, you are very good. Congratulations, you’ve had your interview. Go back and get ready to start work next Monday.”

Note: The above is a real conversation between Liu and dongfeng, the interviewer. If there are similarities, please do not take a seat.



Java Geek technology public account, is set up by a group of technical people who love Java development, focus on sharing original, high quality Java articles. If you think our article is good, please help to appreciate, read, forward support, encourage us to share better articles.

Pay attention to the public account, you can reply “nuggets” in the background of the public account, to obtain the author Java knowledge system/interview must see information.