Java classroom-hands on Brain 1

Source: Internet
Author: User

Enum: commonly used to represent a set of constants of the same class Type. The benefits of using constants for these properties are obvious, not only to guarantee a singleton, but also to replace equals with "= =" when compared, and the values within the enumeration object must be Unique.

Code:

public class Enumtest

{

public Static void main (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 String

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

System.out.println (s==u); True to list all of its values

for (Size value:Size.values ())

{// Traversal

System.out.println (value);

}

}

}

Enum size{small,medium,large}; enumeration type definition data has only three range of values

Operation result :

conclusion: enum type is a reference type! 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.

two. binary representation of numeric values

read the corresponding textbook, 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 and negative numbers of various bit operations, observe the output, and the results of the manual calculation, to see The number in Java is represented by which Code.

Note : 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.

( 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 ;

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 ?

4. type conversions in Java

look at this picture, check it Out. What conclusions can you draw about the number of bits per data type in Java, and the range of values that are represented?

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:

The result of a calculation using a numeric value of type double 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

{

public Static void 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 the string string is followed by a + and variable, it is 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, and if the addition and subtraction of the variables are preceded by a string, the addition and subtraction of the variables are calculated from left to Right. It then combines a new string with the string that follows it.

Java classroom-hands on Brain 1

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.