Basics of Java Grammar----after-school practical work

Source: Internet
Author: User

1.Enumtest. Java

Code:

 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 ());//Convert from StringSize u=size.valueof ("SMALL"); System.out.println (S==U);//true//list all of its values for(Size value:Size.values ()) {//TraverseSystem.out.println (value);}}} enumSize{small,medium,large};//enumeration type, defining data with only three range of values

Operation result :

Conclusion: Enum types are reference types! 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, for variables of enum type,"= =" and The result of the equals () method execution is equivalent.

2. Binary representation of numeric values

after-school exercise: Read the corresponding textbook, or use the Internet search engine, to find out the anti-code, complement and the original code of the concepts, and then write a sample program, the positive, negative number of various bit operation, observe the output, and the results of manual calculation, to see The number in Java is represented by which code.

Answer: within the computer, the fixed-point number has 3 seed notation: Original code, inverse code, and 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.

( i ) The original code: The binary fixed-point notation, that is, the highest bit is the sign bit, " 0 "indicates positive," 1 "indicates negative, and the remaining bits represent the size of the numeric value.

( two ) 2 , anti-code: The inverse code of positive number is the same as its original code; the inverse code of negative number is the inverse of its original code, except the sign bit.

( three ) 3 , complement: the complement of positive number is the same as its original code, the complement of negative number is in its inverse code to add 1 ;

Java is the use of complementary of the

3. Scope of the variable

each variable has a " valid " area (called a " scope "), out of this area, The variable will no longer be valid.

take a look at the sample code on the left, what is the output ?

after-school exercises: Java variables Follow the "masking principle of the same name variable", please read the relevant information after class to find out the relevant knowledge, and then write some test code, as in this example, consciously defined in different places with the same name variable, to see exactly what the output is.

 Public class test{       private   staticint value=9;         Public Static void Main (string[]  args)       {          int value =10;          System.out.println (value);}       }

the output is 10.

4. type conversions in Java

hands -on brain: look at this figure, and then look at the number of bits in Java for each data type, and the range of values, what can you conclude?

1.Int 32bit  The value range is-2of the tothe second party to2of the toSub-square minus1any integer between the(-2147483648~2147483647)
2.Short 16bitThe value range isAny integer between the -32768~32767
3.long 64bit  The value range is-2of the thethe second party to2of the theSub-square minus1any integer between the(-9223372036854774808~9223372036854774807)
4.float 32bit The value range is3.402823e+38 ~ 1.401298e-45
5.double 64bit  The value range is1.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 lack of accuracy for different types of data types, so pay attention to the range of values for each type.

5. Please run the following code (Testdouble.java)

What kind of output did you see, accidentally?

Conclusion:

calculated using A numeric value of type double, The result is imprecise.

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

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, is converted into a scientific notation in the form of 5.053e1, its decimal point moves to a new position ( i.e. floating It can be seen that floating-point numbers are used for scientific calculations, and it is too inappropriate for accurate calculations!

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

Code:

 Public class Plus {     publicstaticvoid  main (string[] args) {     int x=100;     int y=200;   System.out.println ("x+y=" +x+Y);   System.out.println (X+y+ "=x+y");}     }

Results :

conclusion : in System.out.println () , if string string is followed by + and variables, the variable is automatically converted to a string type, then the plus sign is connected, The two strings are then concatenated into a new string output, and if the addition and subtraction of the variables are preceded by a string, the addition and subtraction of the variables are computed from left to right and then combined with the subsequent string into a new string.

Basics of Java Grammar----after-school practical work

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.