Java Study Questions

Source: Internet
Author: User

1. Read the example carefully: Enumtest.java, run it, analyze the running results? What conclusions can you get? Have you mastered the basic usage of enum types?

 Public classEnumtest { Public Static voidMain (string[] args) {Size s=Size.small; Size T=Size.large; //S and T refer to the same object? System.out.println (s==t);//        //is the original data type? System.out.println (S.getclass (). isprimitive ());//determine whether the data for a class package//Convert from StringSize u=size.valueof ("SMALL"); System.out.println (S==U);//true//list all of its values         for(Size value:Size.values ()) {System.out.println (value); }    }} enumSize{small,medium,large};

Operation Result:

The first false is to determine if s and T refer to the same object, the second false is to determine whether it is a class package of data, the third ture is to determine whether S equals U, and finally to list all the elements inside it, from which the enumeration type is a reference type, the enumeration does not belong to the original data type, 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 () method directly compared to the value of the enumeration variable, in other words, for the enumeration type of the variable, the "= =" and the Equals () method to execute the result is equivalent.

2. Read the corresponding textbook, or use the Internet search engine to find out the concepts of anti-code, complement and original code

The original code is the absolute value of the symbol bit plus the truth, that is, the first digit to represent the symbol, the remaining bits represent the values,

The complement of the expression method is: a positive complement is its own negative complement is in its original code on the basis of the symbol bit unchanged, the rest of you take the counter, the last +1. (That is, on the basis of the anti-code +1)

The inverse code is represented by: the inverse of a positive number is its own negative anti-code is in its original code based on the symbol bit unchanged, the remaining bits are reversed.

3.Java variables follow the "shielding principle of variable with the same name", please read the relevant information after class to clarify the relevant knowledge

Run the code shown, the output is 2, if the int value=2 is removed, the result is 1, in this instance, the first value is a member variable, is also a global variable, the second value is a local variable, in Java The local variable can overwrite the global variable, When a variable is used locally, the JVM prioritizes the definition of the variable near the current location.

4.

No loss of precision

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.

Integer: Byte (8), short (+), int (+), long (64) Float type: float (+), double (64),

Java automates the conversion from a low-level type to an advanced 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.

What kind of output did you see, accidentally?

Why is the numeric value of a double type not "mathematically accurate" result? Please find information through the Internet, or read the relevant computer textbooks to solve this problem.

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 careful when using float and double for precise calculations. There are some alternatives that you might consider implementing. such as by using the long type to convert. We know that the representation of a floating-point number in a computer is obtained by multiplying an integer (that is, the mantissa) by a radix (usually 2 in the computer) to the full power. (like scientific notation, the scientific notation Base is ten) the memory structure of float is: The sign bit represents positive and negative, 1-bit exponent, 8-bit tail bit, 32-bit (sign bit 1 is negative, 0 means positive) the exponent is the base of 2, the range is 128 to 127, if more than 127, then from-128 counts. That is: The 127+1=-128 mantissa is omitted the 1th bit of 1, so in the restore should first in the first bit plus 1. It may contain both integers and decimal fraction, or only some of them, depending on the size of the numbers. For floating-point numbers with integral parts, there are two representations of integers, when integers are greater than 16777215 of the decimal, the scientific notation is used, and if less than or equal, the general binary notation is used directly. The notation of scientific notation is the same as that of decimals. The decimal part is the direct use of the scientific notation, in the form of X * (2 ^ n). This results in a floating-point type that cannot be accurately represented in the computer's storage. (Baidu Search)

6.

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?

Output Result: x+y=100200

300=x+y

Cause: System.out.println ("x+y=" +x+y) string string is followed by a plus variable, the plus sign joins, and the two strings are concatenated into a new string output.

The "+" in System.out.println (x+y+ "=x+y") is the addition operator, which is the sum of two numbers.

Java Study Questions

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.