Some knowledge about Java

Source: Internet
Author: User

I. Enumeration type

Example code:

public class Enumtest {
public static void Main (string[] args) {
Size S=size.small;
Size T=size.large; //s and T refer to not the same object
System.out.println (s==t); //Run result is false, only int, float data type is raw data type
System.out.println (S.getclass (). isprimitive ());
Size u=size.valueof ("SMALL"); //Convert from String
System.out.println (S==u); //true output to bool type
Foe (Size value:Size.values ()) //list all of its values
{
System.out.println (value);
}
}
}
Enum Size{small,medium,large};

The result of the program's operation is that the enumeration can be used in a switch statement, and the enumeration can also add methods and properties like a generic class to define variables to limit the assignment of variables, and to assign values through the enumeration name. Value "To get the values in the associated enumeration, you can use the" = = "and the Equals () methods to directly compare the values of the enumeration variables.

Second, anti-code, complement and the relevant concepts of the original code

1) Anti-code:

the representation of the inverse code is divided into two situations:

If a number is positive, the inverse code is the same as the original code. And they are the same as the complement.

If a number is negative, the sign bit is unchanged and the rest of you are reversed by the original code.

For example: 1 of the original code is 0000 0001, the inverse code is 0000 0001.

-1 of the original code is 1000 0001, the inverse code is 1111 1110.

The same problem exists in the inverse code: +0 of the anti-code is 0000 0000-0 of the inverse code of 1111 1111

2) Complement: If a number is positive, then the complement, anti-code source code are the same, if a number is negative, then his complement is his anti-code plus 1.

For example: [+1] = [00000001] Original = [00000001] counter = [00000001] complement

3) The original code: the highest bit as the sign bit, 0 for positive, 1 for negative. The rest of you represent the absolute value of the values themselves, for example:

1 is expressed as 0000 0001,-1 is 1000 0001

7 means 0000 0111-7 The original code is represented as: 1000 0111

In Java programs, you use complement to represent numbers.

Third, the number of bits per data type in Java, and the range that represents the value

1.Int 32-bit value range 2 of 31 square to 2 of the 31-square minus 1 any integer ( -2147483648~2147483647)

2.Short 16-bit value range is any integer between -32768~32767

3.long 64-bit value range 2 of 63 square to 2 of the 63-square minus 1 any integer ( -9223372036854774808~9223372036854774807)

4.float 32-bit value range 3.402823e+38 ~ 1.401298e-45

5.double 64-bit value range of 1.797693e+308~ 4.9000000e-324

6.char 8-bit value range of -128~127

7.byte 8-bit value range is any integer between -128~127

Conclusion: There may be a loss of precision when different data types are converted.

Why are the values of double type not "mathematically accurate" results?

First we have to discuss this problem from the computer itself. The computer does not recognize any data other than binary data. No matter what programming language we use, and in what programming environment, we must first translate the source program into binary machine code before it can be recognized by the computer. In fact, floating-point numbers are not suitable for precise calculations and are suitable for scientific calculations. float and double are used to denote numbers with a decimal point, and these numbers are stored in the form of scientific notation. When a number such as 50.534, converted to scientific notation in the form of 5.053e1, its decimal point moved to a new position (that is, floating) visible, floating point is used for scientific calculation, used for accurate calculation is too inappropriate.

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");

The graphs for running the results are:

Conclusion : in System.out.println (), if the string string is followed by a + and variable, the variable is automatically converted to a string type, then the plus sign joins, and the two strings are concatenated into a new string output If the addition and subtraction of a variable is preceded by a string, the addition and subtraction of the variable is computed from left to right and then combined with the string that follows it into a new one.

Some knowledge about Java

Related Article

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.