Java Classroom Little Practice

Source: Internet
Author: User

Hands on Brain 1:

Package meiju;

public class Meiju {
Private Enum MyEnum
{
One,two,three
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
For (MyEnum value:MyEnum.values ())
{
System.out.println (value);
}

}

}

Read the example carefully: Enumtest.java, run it, analyze the running results? What conclusions can you get?

Enum size{SMALL, MEDIUM, LARGE} type used;

An enumeration type is a reference type, and the enumeration does not belong to the original data type, and each of its specific values refers to a specific object. The same value refers to the same object;

The output of a string can be done by a specific function.

Hands on Brain 2:

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.

1.int          32 bit          The value range is      -2 of the 31 square to 2 of the 31 square minus 1 of any integer ( -2147483648~2147483647);
2.Short      16 bit         value range is any integer between       -32768~32767;
3.long         64 bit         value range is      - Any integer ( -9223372036854774808~9223372036854774807) between 2 of 63 square to 2 of 63 square minus 1;
4.float        32 bit       Value range       3.402823e+38 ~ 1.401298e-45,
5.double     64-bit       take The value range is       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 lack of accuracy for different types of data types, so pay attention to the range of values for each type.

Hands on Brain 3:

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?

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

}

}

If the string string is followed by a + and variable, the variable is automatically converted to a string type, then the plus sign joins, then the two strings are concatenated into a new string output, and if there is a string for the addition and subtraction of the variable, then the addition and subtraction of the variable is calculated from left to right. It then combines a new string with the string that follows it.

After-school Exercise 1:

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 numbers, negative numbers of various bit operations, observe the output, and the results of the manual calculation, to see the number in Java is the above-mentioned code representation.

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.

The so-called original code is the binary fixed-point notation, that is, the highest bit is the sign bit, "0" is positive, "1" is negative, the remaining bits represent the size of the value.

The inverse code notation stipulates that the inverse code of a positive number is the same as its original code, and the inverse of a negative number is a bitwise negation of its original code, except for the sign bit.

Original code 10010 = anti-code 11101 (10010,1 number, it is negative)

(11101) binary =-20 binary

The complement notation stipulates that the complement of positive numbers is the same as the original code, and that the complement of negative numbers is added to the minus 1 of the inverse code.

After-school Exercise 2:

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.

As a general rule, variables defined in a scope are not visible (that is, access) to programs that are outside the scope. Therefore, when you define a variable in a scope, you localize the variable and protect it from unauthorized access and/or modification

After-school Exercise 3:

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

This involves the conversion of binary and decimal.
NThe binary can be understood as: numericalxThe power of a base, such as the decimal number we are familiar with123.4=1x10²+2x10+3x (Tenof the0Second Power) +4x (Tenof the-1Second Power)the same is true of other binaries, such as binary numbers11.01=1x2+1x (2of the0Second Power) +0+1x (2of the-2Second Power)=of decimal3.The child's powder forging, the crazy fist -.
Doublethe value of the type occupies64bit, i.e. -Binary number, except that the highest bit represents the positive and negative sign, and the lowest bit is bound to have an error with the actual data (unless the actual data happens to be2of theNSecond Party)

Java Classroom Little Practice

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.