Some basics about Java

Source: Internet
Author: User

Recently learned some knowledge of Java, some of which have their own understanding, published to Hope to get the help of the great God.

One: About enum classes in Java

This is a small program about enumerating classes:

public class Enumtest {

public static void Main (string[] args)

{Size s=size.small;

Size T=size.large; S and T are not referencing the same object

System.out.println (s==t); The output is false, only int and float are the raw data

System.out.println (S.getclass (). isprimitive ()); Convert from String

Size u=size.valueof ("SMALL");

System.out.println (S==u); True, output to bool type

For (Size value:Size.values ()) {System.out.println (value);}}} List all of its values

Enum Size{small,medium,large};

The result of the operation is: The enumeration is to enumerate each object, the enumeration type can be used in the switch, the enumeration does not belong to the original data type, each of its specific values refers to a specific object. The same values refer to the same object, and you can use the "= =" and the Equals () methods to directly compare the values of the enumeration variables.

Second: The basic knowledge about the anti-code complement and the original code

1. Number of machines: we know that the numbers are 2 in the computer, only 0 and 1, and generally are represented as 8 digits, the highest bit indicates that the sign is positive or negative, 0 is positive, 1 is negative. For example, in the computer to represent -3=10000011, positive 3 is 00000011, here 100000011 and 00000011 on the number of machines.

2. Original code: The original code is the symbol bit plus the absolute value of the true value, that is, with the first symbol, the rest of the bits represent the values, such as the 100000001 loaded into a decimal number is-1, so-1 and 100000001 is the original code, note that the first bit is the symbol of positive or negative, So the 8-bit 2-decimal value range is 11111111 to 01111111.

3. Anti-code: Positive anti-code is itself, negative anti-code is in its original code on the basis of the symbol bit unchanged, the rest of the other bits take the reverse. For example-1 of the original code is 100000001, its anti-code is 111111110. Note that the sign bit cannot be changed.

4. Complement: 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)

For example: [+1] = [00000001] original = [00000001] anti = [00000001] complement [-1] = [10000001] original = [11111110] counter = [11111111] Complement

In Java, you use complement to represent numbers.

Three: basic knowledge of floating-point numbers

Floating-point numbers are represented by scientific notation in a computer, that is, using the mantissa (that is, a valid number), an exponent, a cardinality, and a number representing the symbol bit. For example 128.56 in the computer is expressed as 1.2856*10^2,1.2856 is the mantissa, 10 is the base, and 2 is the exponent.

IEEE floating-point number:

A floating-point number is saved in a computer using a finite number of contiguous bytes. In the IEEE Standard, floating-point numbers divide all bits of a specific length of contiguous bytes into a specific width of a symbol field, an exponential field, and a three field in the Mantissa field, where the saved values are used to represent the symbols, exponents, and mantissa in a given binary floating-point number, respectively. This allows the given value to be expressed by the mantissa and the adjustable exponent.

Two basic floating-point formats:

The IEEE single-precision format has 24 digits of effective digital precision, occupying a total of 32 bits.

IEEE Double precision format has a 53 effective digital precision, occupying a total of 64 bits.

Where float is stored as shown in the following way:

The double is stored in the following way:

Below I have this basic knowledge point to solve one of our doubts, please look at the following procedure, pay attention to observe the output results

float F = 2.2f;

Double d = (double) F;

System.out,println (d.tostring ("0.0000000000000"));

f = 2.25f;

D = (double) F;

System.out,println (d.tostring ("0.0000000000000"));

Perhaps the result of the output is confusing to everyone, The single-precision 2.2 conversion to double precision, after the precision of 13 bits after the decimal point becomes 2.2000000476837, and the single-precision 2.25 converted to double precision, became 2.2500000000000, why 2.2 after the conversion of the value changed and 2.25 did not change it? That's weird, isn't it? In fact, we can probably find the answer through the introduction of the two kinds of storage results above. First, let's look at 2.25 single-precision storage, very simple 0 1000 0001 001 0000 0000 0000 0000 0000, and 2.25 double-precision representation: 0 100 0000 0001 0010 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000, so the value will not change at the time of casting, and we'll look at 2.25, 2.2 with science The number method should be: The decimal decimal is converted to binary decimal method for decimal, take the integer part, so 0.282=0.4, so the binary fractional first bit is the integer part of 0.4 0,0.4x2=0.8, the second bit is 0,0.8*2=1.6, the third bit is 1,0.6x2 = 1.2, the fourth bit is 1,0.2*2=0.4, the fifth bit is 0, so it is never possible to multiply to = 1.0, the resulting binary is an infinite loop arrangement of 00110011001100110011 ... , for single-precision data, the tail number can only represent 24bit accuracy, so 2.2 of the float storage is:

But this way of storage, converted to decimal values, But will not be 2.2, should be decimal in the conversion to binary may be inaccurate, such as 2.2, and double type of data also has the same problem, so in floating-point representation will produce a bit of error, in the single-precision conversion to double precision, there will be errors, for the decimal data can be expressed in binary, such as 2.25 , this error will not exist, so there will be more strange output above.

Some basics about Java

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.