Java hands-on brain

Source: Internet
Author: User

One

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

The function of this program is to compare the enumeration type objects, judgingSwith theTwhether to refer to an object that is an outputtruenot outputfalse, apparently one forSmallone forLargeis not a reference object so the outputfalse. The two enumeration type is not the base data type, so the outputfalse. The string is also converted to an enumeration type, and the stringuconverted to an enumeration type and assigned to a value ofSmall, and then again withscomparison, youswith theuequal outputtrue, and finally all values of the enumerated type are outputSMALL,MEDIUM,LARGE.

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

An enumeration type is a reference type, and the enumeration does not belong to the original data type, and each of its specific values refers to a specific object. The same value refers to the same object. The enumeration type uses the conversion of a string type to an enumeration type.

3, read the corresponding textbooks, 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 for a variety of bit operations, observe the output, and the results of the manual calculation, to see the Java The number in the above is expressed by which code.

Original code: The original code represents the signed number , the highest bit is the sign bit , the value bit part is the absolute value of the number

, also the highest bit is the symbol but the value part is its absolute value for positive numbers. ( 1 change 0,0 1"

complement: The signed number of the complement , for positive numbers, the same as the original code, the same as the inverse code , but the negative value of the number of bits of its absolute value is reversed after the bottom plus 1 gain .

in the computer, the fixed-point number has 3 kinds of notation: the original code, the inverse code and the complement. Anti-code is a kind of numerical storage, but because the complement is more effective in the representation of the number in the computer form, so most computers generally do not use anti-code representation number.

4, look at this diagram, and then look at the number of bits in Java for each data type, and the range of values, you can draw what conclusion?

No loss of precision

Char 8bits binary number range is-2of the7the second party to2of the7Sub-party-1,Byte 8bit signed integer-128to the127any integer between the Shortbit unsigned integer-32768to the32767any integer between theintbit signed integer-2of the tothe second party to2of the toSub-party-1any integer between theLongbit signed integer-2of the thethe second party to2of the theSub-party-1any integer between thefloatbit single-precision floating-point number,Doublebit double-precision floating-point number.

The conclusion is: Java automates the conversion from a low-level type to a high-level type, where a data type with a relatively small range of values is converted to a data type with a relatively large range of values.

5. Please run the following code:

What kind of output did you see, accidentally?

0.05 + 0.01 = 0.060000000000000005
1.0-0.42 = 0.5800000000000001
4.015 * 100 = 401.49999999999994
123.3/100 = 1.2329999999999999

The result of the output is different from the precision of the output result of the normal double type.

6.Why is the value of double type not "mathematically accurate" result?

This involves the conversion of binary and decimal. NThe binary can be understood as: a power of numeric x cardinality, such as the decimal number we are familiar with123.4=1xTen²+2x10+3x(Tenof the0Second Power) +4x(Tenof the-1Second Power)the same is true of other binaries, such as binary numbers11.01=1x2+1x(2of the0Second Power) +0+1x(2of the-2Second Power)=of decimal3.25. Doublethe value of the type occupies64bit, i.e. -Binary number, 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 happens to be2of theNSecond party).  For example, for example , to use a4bitto represent decimals3.26, from high to low in turn corresponding2of the1,0,-1,-2power, according to the top analysis, should be in the binary number11.01 (corresponding to the decimal3.25)and the11.10 (corresponding to the decimal3.5)choose between.  In a nutshell, the values we give, in most cases, require more than64bitmore digits to be accurately expressed (even in the need of an infinite number of bits), andDoublethe value of the type is only64bit, the number of digits in the back is bound to bring an error, and the result of "mathematically accurate" cannot be obtained.

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

x+y=100200

300=x+y

in the System.out.println () , if the string string is followed by a + and variable, the variable is converted to string type, the plus sign connects, and the two strings are concatenated into a new string output, and if the addition and subtraction of the variables are preceded by a string, then the addition and subtraction of the variables are calculated from left to right, and then with the subsequent string to combine into a new string. that is, the plus sign is connected only if the two string type or one of them is a string type, otherwise it is still an operator.

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.