These days of Java training class, constantly wake up their understanding of Java (too long, the University of the Quick Forget clean), some of the interesting questions, are in the Java details (alas, I am the kind of people who see the pit on the jump, a Do wrong), the following to share:
1) write in Java program when you spend 1.1 yuan in the supermarket shopping, you give the Cashier 2 yuan, the cashier to find you 0.9 yuan process;
At that time a look at the topic, very simple Ah! Write quickly:
public class sell{
public static void Main (string[] args) {
Double total = 2.0;
Double used = 1.1;
Double result = total-used;
SYSTEM.OUT.PRINTLN ("Result:" + result);
}
}
The result is tragic, because the floating-point operation in Java only gets the approximate value, the result is: 0.8999999999999999
To get precise results, you need to invoke the API class BigDecimal that Java provides in the Java.math package. (float and double can only be used for scientific calculations or engineering calculations, using Java.math.BigDecimal in commercial calculations)
The correct result is:
Import Java.math.BigDecimal;
public class sell1{
public static void Main (string[] args) {
BigDecimal bd1 = new BigDecimal ("2.0");
BigDecimal bd2 = new BigDecimal ("1.1");
System.out.println (Double) bd1.subtract (BD2));
}
}
Run Result: 0.9
2) with coercion type conversion (int) (char) (byte)-1, what is the final result?
At that time saw this problem instantaneous a Leng, later a think not still-1, but not sure, ran a bit, the result is: 65535 ((⊙o⊙) ... More masked), later only know char character type, store character constants, the underlying use of 16-bit unsigned integer representation, and signed the number of symbols to expand, the number of unsigned 0 extension (the computer, the number is in the form of a complement), so the process of calculation is:
100000000000000000000001
111111111111111111111111
1111,1111 byte
1111,1111,1111,1111 Char
0000,0000,0000,0000,1111,1111,1111,1111 int
The final result is: 65536
3) int a = 1,b = 2,c = 3;
if (a > B && C + + > B)
int a = 1,b = 2,c = 3;
if (a > B & C + + > B)
What is the output of the C value in the two-segment program? (Haha, this question I also know the first answer C = 3, the second one is blindfolded) originally:
&&: logical operator with logic short-circuit function (two conditions of the connection, if the first condition is false, subsequent conditions do not operate)
&: bitwise operator, but can connect a condition, but no short circuit function
The result is: the first c = 3, the second C = 4
4) String str1 = ' a ' + 3 + "Hello";
String str2 = "Hello" + ' a ' + 3;
What is the result of the STR1,STR2 output? (This time I dare not to careless, there must be a pit, but think, will not ah, or jump pit bar)
In fact, "+ for string type is to do strings connection operation", the programmer almost all know, but "char character type, store character constants, the underlying use of 16-bit unsigned integer expression" notice less people, the result of this problem is: str1 = 100Hello str2 = Helloa3
Now this, after some words to update, Java to pay attention to detail AH ah ah ah ah ah ah ah!
Some interesting face questions in Java