Java hands-on and experimental finishing

Source: Internet
Author: User

1 enum Types

In Java , an enumeration type is essentially a class, and a constant in an enumeration is an instance of that enumeration type. 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, for variables of enum type, "= =" and The result of the Equals () method execution is equivalent.

2 Anti-code, complement the original code

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

Anti-code: The inverse code of positive number is the same as its original code; the inverse code of negative number is the inverse of its original code, but the sign bit outside.

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

The number is represented in the computer as a machine, the computer can only recognize 0 and 1, the use of binary, and in daily life, people use the decimal. The value is positive or negative, and the computer uses the highest bit of a number to store the symbol (0 is positive and 1 is negative).   This is the original code of the number of machines. Assume that the machine can handle the number of bits is 8. That is, the word length is 1byte, the original code can represent a range of values ( -127~-0 +0~127) a total of 256. With the representation of numeric values, arithmetic operations can be performed on the logarithm. But it was soon discovered that the multiplication operation was correct when using the original code with the sign bit, and there was a problem with the addition and subtraction operation, as follows:

Assuming a word length of 8bits

(1) 10-(1) 10 = (1) 10 + (-1) 10 = (0) 10 (00000001) original + (10000001) original = (10000010) original = (-2)

is obviously not correct. Because there is no problem in the addition of two integers, it is found that the problem appears in the sign bit of negative body, the other than the sign bit out of the rest of you take the inverse of the counter code. The inverse Code value space and the original code is the same and one by one corresponds. Here is the subtraction operation of the inverse code:

(1) 10-(1) 10 = (1) + + (-1) 10 = (0) 10 (00000001) inverse + (11111110) counter = (11111111) counter = (-0)

There is a problem.

(1) 10-(2) 10 = (1) 10 + (-2) 10 = (-1) 10 (00000001) inverse + (11111101) counter = (11111110) counter = (-1)

That's right

The problem arises on (+0) and (-0), and 0 is not positive or negative in people's computational concepts. (Indians first put 0 as a marker and put it into operation, and the Indian mathematics and decimal counts of number 0th are a great contribution to human civilization). The concept of complement is then introduced. The complement of negative numbers is to add one to the inverse code, and the positive number of the original code is the same. In the complement (-128) instead of (-0), so the complement of the expression range is: ( -128~0~127) a total of 256.

Note: (-128) does not correspond to the original code and the inverse code, (-128) = (10000000) The complement of the addition and subtraction operation is as follows:

(1) 10-(1) 10 = (1) 10 + (-1) 10 = (0) 10 (00000001) complement + (11111111) complement = (00000000) complement = (0)

That's right

(1) 10-(2) 10 = (1) 10 + (-2) 10 = (-1) 10 (00000001) complement + (11111110) complement = (11111111) complement = (-1)

That's right

So the complement is designed to: ⑴ the symbol potential energy and the RMS part to participate in the operation, thus simplifying the operation rules. ⑵ the subtraction operation into addition, further simplifying the circuit design of the computing device in the computer

3 Scope of variables

Each variable has a "valid" area (called a "scope"), out of which the variable will no longer be valid. Java variables follow the "masking principle of variable with the same name"

4 read user input at run time

(1) method One: Use the showinputdialog method of the Joptionpane class .

(2) Method two: Use Scanner class (JDK5.0 or above)

5 type conversions in Java

The result of a calculation using a numeric value of type double is imprecise .

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

In fact, floating-point arithmetic is rarely accurate, as long as it is more than the range of precision can be expressed error. Often the error is not due to the size of the number, but because of the precision of the number. Therefore, the resulting result is close but not equal to the desired result. Especially in the usefloatand theDoublebe very careful when doing precise calculations.
There are some alternatives that you might consider implementing. such as throughStringcombinedBigDecimalor by usingLongtype to convert. can also be used withBigDecimalclass to implement. (Note: in buildingBigDecimalobject should use a string instead ofDoublevalue)

6- Word serial connection operation

Java hands-on and experimental finishing

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.