Java Type Conversion

Source: Internet
Author: User

A Hands on the brain
(1) Read the example: Enumtest.java, run it, see the results of the run

(2) What conclusions can you get? Have you mastered the basic usage of enum types?
• 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.

Two Binary representation of numeric values
(1) Anti-code, complement and the original code of the concepts, and then write a sample program, positive numbers, negative numbers for various bit operations, observe the output, and the results of the manual calculation to compare, see the number in Java is the above-mentioned code representation.
1. Original code: A binary fixed-point representation method for numbers in a computer. The original code notation adds a sign bit (that is, the highest bit to the sign bit) before the value: a positive number is 0, a negative number is 1 (0 has two representations: +0 and-0), and the remaining bits represent the size of the value.
2. Anti-code: For a positive number, its anti-code representation is the same as the original code, for negative numbers, the original code is bit-wise, but the sign bit except.
3. Complement: For positive numbers, the same as the original code, for negative numbers, it is the inverse code plus 1.
4.Java is in the complement.

Three Hands-On Labs
Please run the following code (Testdouble.java) What kind of output did you see, unexpectedly?


Do you know why?
(1) Why the numeric value of a double type cannot be calculated as a result of "mathematically accurate".
• Because there is a problem with decimal and binary conversion in Java, the value of double type occupies 64bit, that is, 64 binary numbers, minus the highest bit represents the positive and negative sign, the lowest bit will be with the actual data error, in simple terms, we give the value, in In most cases it takes more than 64bit of digits to be accurately represented (even in the case of an infinite number of bits), whereas a double type has a value of only 64bit, and the number of digits behind it will definitely bring an error, and the result of "mathematically accurate" cannot be obtained.

Four Hands on the brain
(1) 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");


(2) Why are there such output results?
1. The first plus sign is the function of the connector.
2. The second plus sign is the meaning of the operation.
Six Java variables follow the "masking principle of variable with the same name"
• The output is 2.
• The output is the value in the main function!

Java Type Conversion

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.