Java hands-on brain

Source: Internet
Author: User

<1> 1. Read the example carefully: Enumtest.java, run it, analyze the running results?

Program:

results:

Analysis: since S and T are not the same object referenced, the first output is False;s.getclass (). Isprimitive () is used to determine if s is the base data type, because S is an enumerated type, so the second output is false; "SMALL" (string) is converted to an enumeration type, like S, so the third output is true; The enumerated type is traversed and output.

2. What conclusions can you get? Have you mastered the basic usage of enum types?

Conclusion: An enumeration type is a reference type and is not part of the original data type; Each of its specific values refers to a specific object, and the same value references the same object. The same value refers to the same object, and the enumeration type can be converted from a string type to an enumeration type.

Usage: define constants (as shown in the example); Enumerations can be used in a switch statement.

<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 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.

Original code: is the binary fixed-point notation, that is, the highest bit is the sign bit, "0" means positive, "1" is negative, and the remaining bits represent the size of the value. In the case of positive 2, the eight-bit binary representation of 000 0010,-2 is represented as a 2 binary representation of 10000010. The previous one represents the sign bit, where the positive number is 0 and the negative number is 1.

Anti-code: 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.

Complement: the complement of a positive number is the same as its original code, and the complement of a negative number is the minus 1 of its inverse code.

Sample program:

<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, to see exactly what the output is.

masking principle for variable with the same name: in Java, locally defined variables can override variables in the global scope. When a variable is used locally, the definition of the variable with the current position "near" is preferred. If you define a local name that is the same as a global variable, we use the This keyword helper to invoke the global variable, but the this variable cannot be used in a static function type.

Program:

                private static int value = 1;
             int value = 2;
           

Results:

Program:

Public class Text {
private static int value = 1;
public static void Main (string[] args) {
System.out.println (value);
}
}

Results:

Program:

Public class Text {
public static void Main (string[] args) {
int value = 1;
System.out.println (value);
}
}

Results:

<4> look at this diagram, and look at the number of bits in Java for each data type, and the range of values that you can draw.

The number of digits in the data type represents the range of values

Int 32-bit-2 of the 31-time-2 31-Time Square

Short 16-bit-32768 ~ 32678
Long 64-bit-2 of the 63-time Square of the 63-square
Float 32-bit 10^-38~10^38 and -10^-38~-10^38

Double 64-bit 10^-308~10^308 and -10^-308~-10^308

Char 16-bit-2 of the 7-time Square of the 7-time Square
Boolean 1-bit True/false
BYTE 8 bit-128 ~ 127

Conclusion: the solid line represents no precision loss, the dashed lines represent precision loss, low precision to high precision conversion without loss of precision, conversely, from high-precision transmission to low precision will be lost.

<5> Why is the numeric value of a double type not "mathematically accurate" result?

Reason: The result is related to the representation of floating-point numbers in the computer, the problem is how to save the decimal point in binary, that is, the use of scientific notation, so that the position of the decimal point is located after the first number. Since the computer can handle only 0 and 1, the base of the scientific notation is 2, not 10, when a floating-point number is represented in the computer. In the memory of the computer, the actual value of the floating-point calculation is not the exact one, so the floating-point number in the computer is approximate, not the exact value. Computer-derived values, in most cases need to be more than 64bit more digits to accurately represent (even the need for infinity), and the double type of the value of only 64bit, the back of the number of bits will certainly bring error, can not be "mathematically accurate" results.

<6> what is the output of the following code?

Public class text{

Public stastic void Main (String args[]) {

int x=100;

int y=200;

System.out.println ("x+y=" +x+y);

System.out.println (x+y+ "=x+y");

}

}

Operation Result:

x+y=100200
300=x+y

Reason: because the output is a string, the previous one represents the output of two strings, and the latter represents the output of two shaping with a string.

Java hands-on brain

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.