Java after-school experiments and hands -on brain
1. Read the example carefully: Enumtest.java, run it, analyze the running results?
What conclusions can you get? Have you mastered the basic usage of enum types?
Public class enumtest {
Public Static void Main (string[] args) {
Size S=size.small;
Size T=size.large;
S and T refer to the same object?
System.out.println (s==t); //
is the original data type?
System.out.println (S.getclass (). isprimitive ());
Convert from String
Size u=size.valueof ("SMALL");
System.out.println (S==u); True
List all of its values
for (Size value:Size.values ()) {
System.out.println (value);
}
}
}
Enum Size{small,medium,large};
Experimental conclusion: Enum type is a reference type!
An enumeration is not part of the original data type, and each of its specific values refers to a specific object. The same value refers to the same object. You can use the "= =" and the Equals () methods to directly compare the values of the enumeration variables, in other words, the result of the "= =" and the Equals () methods are equivalent for variables of enum types.
2. Read the corresponding textbook, or use the Internet search engine, find out the anti-code, complement and the original code of the concepts, and then write a sample program, the positive numbers, negative numbers of the various bit operations, observe the output, and the results of the manual calculation, to see the number in Java is the above-mentioned code representation?
The original code: the so-called original code is the binary fixed-point notation, that is, the highest bit is the sign bit, "0" is positive, "1" is negative, the remaining bits represent the size of the value.
The inverse code notation stipulates that the inverse code of a positive number is the same as its original code, and the inverse of a negative number is a bitwise negation of its original code, except for the sign bit.
Original code 10010 = anti-code 11101 (10010,1 number, it is negative)
(11101) binary =-20 binary
The complement notation stipulates that the complement of positive numbers is the same as the original code, and that the complement of negative numbers is added to the minus 1 of the inverse code.
3. Java variables follow the "masking principle of the same name variable", please read the relevant information after class to find out the relevant knowledge, and then write some test code, as in this example, consciously defined in different places with the same name variable, see the output is exactly what value?
Public class Test {
/**
* @param args
*/
Private Static int value= 1;
Public Static void Main (string[] args) {
TODO auto-generated Method stub
int value=2;
System. out. println (value);
}
}
The output value is 2.
Type conversions in 4.Java
Hands-on brain: Look at this figure, and then look at the number of bits in Java for each data type, and the range of values, what can you conclude?
1.Int 32-bit value range 2 of 31 square to 2 of the 31-square minus 1 any integer ( -2147483648~2147483647)
2.Short 16-bit value range is any integer between -32768~32767
3.long 64-bit value range 2 of 63 square to 2 of the 63-square minus 1 any integer ( -9223372036854774808~9223372036854774807)
4.float 32-bit value range 3.402823e+38 ~ 1.401298e-45
5.double 64-bit value range of 1.797693e+308~ 4.9000000e-324
6.char 8-bit value range of -128~127
7.byte 8-bit value range is any integer between -128~127
Conclusion: There may be a lack of accuracy for different types of data types, so pay attention to the range of values for each type.
5. Why is the numeric value of a double type not "mathematically accurate"?
This involves the conversion of binary and decimal.
n binary can be understood as: the power of the numerical x cardinality, for example, we are familiar with the decimal number 123.4=1x10²+2x10+3x (10 of the 0 power) +4x (10-1 power); the other binary is the same, such as the binary number 11.01=1x2+1x (2 0 Power) +0+1x ( 2-2 power) = 3.25 in decimal. A value of type double takes 64bit, or 64 binary numbers, except that the highest bit represents the positive and negative sign, and the lowest bit is bound to have an error with the actual data (unless the actual data is exactly 2 of the n-th square).
For example, for example, to use 4bit to represent decimal 3.26, from high to low to correspond to 2 1,0,-1,-2 power, according to the top of the analysis, should be in the binary number 11.01 (corresponding to the decimal 3.25) and 11.10 (corresponding to the decimal 3.5) between the selection.
In short, we give the value, in most cases need more than 64bit more digits to accurately represent (even need infinity), and the double type of the value of only 64bit, the back of the number of bits will definitely bring error, can not get "mathematically accurate" results.
6. What is the output of the following code?
int x=100;
int y=200;
System.out.println ("x+y=" +x+y);
System.out.println (x+y+ "=x+y");
Why is there such an output result?
The first sentence outputs the "+" connection literal, and the result is that x and Y will connect the two data.
The "+" in the two outputs of the statement is the operator, and the result is the sum of x and Y.
Java hands-on brain and post-class experimental problems