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 references to the same object, the first output is false, with S.getclass (). Isprimitive () to determine if S is the base data type, because S is an enumerated type, so the second output is false; Convert "SMALL" (string) to enum type , like S, so the third output is true; Finally, the enumeration 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; Enumerations can also be used in switch statements.

<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 is represented as a 0000010,-2 2-tab

Shown as 10000010. The previous one represents the sign bit, where the positive number is 0 and the negative number is 1.

Anti-code : The inverse of a positive number is the same as its original code; 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.

Program:

Results:

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

Program:

Results:

Program:

Results:

Program:

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 the loss of precision, the dashed lines represent the loss of precision, low precision to high-precision conversion without loss of precision, and from high-precision transmission to low precision will lose precision.

<5>

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

Program:

Results:

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?

Program:

Results:

Reason:
The output is a string, but the previous one indicates the output of two strings, and the last one represents the output of two integers 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.