Java's hands-on brain

Source: Internet
Author: User

Hands-on brain and after-school real

Read the example carefully: Enumtest.java, run it, analyze the running results?

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

List all of its values

For (Size value:Size.values ()) {

System.out.println (value);

}

}

}

Enum Size{small,medium,large};

The first two are not the same object so the execution result is false, the last one references the same address, so the same execution is true.

We can get an experimental conclusion from this experiment: The enumeration 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. You can use the "= =" and the Equals () methods to directly compare the values of the enumeration variables, in other words, the result of the "= =" and the Equals () methods are equivalent for variables of enum types.

Two:

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

A: The original code: 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.

Three:

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.

Example: public class Test {

/**

* @param args

*/

Private Static int value= 1;

Public Static void Main (string[] args) {

TODO auto-generated Method stub

int value=2;

System. out. println (value);

}

}

The last output value is 2

You can see the application of the "masking principle of the same name variable" in Java.

Four:

Please run the following code (TESTDOUBLE.JAVA)

public class Testdouble {

public static void Main (String args[]) {

System.out.println ("0.05 + 0.01 =" + (0.05 + 0.01));

System.out.println ("1.0-0.42 =" + (1.0-0.42));

SYSTEM.OUT.PRINTLN ("4.015 * 100 =" + (4.015 * 100));

System.out.println ("123.3/100 =" + (123.3/100));

}

}

Conclusion: The result of the calculation using a double type of numerical value is imprecise.

Five:

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

Related to how floating-point numbers are represented inside a computer

n binary can be understood as: the power of the numerical x cardinality, for example, we are familiar with the decimal number 123.4=1x10²+2x10+3x (10 of the 0 power) +4x (10-1 power); the other binary is the same, such as the binary number 11.01=1x2+1x (2 0 Power) +0+1x ( 2-2 power) = 3.25 in decimal.
A value of type double takes 64bit, or 64 binary numbers, 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 is exactly 2 of the n-th square).

For example, for example, to use 4bit to represent decimal 3.26, from high to low to correspond to 2 1,0,-1,-2 power, according to the top of the analysis, should be in the binary number 11.01 (corresponding to the decimal 3.25) and 11.10 (corresponding to the decimal 3.5) between the selection.
In short, we give the value, in most cases need more than 64bit more digits to accurately represent (even need infinity), and the double type of the value of only 64bit, the back of the number of bits will definitely bring error, can not get "mathematically accurate" results.

What is the output of the following code?

intx=100;

int y=200;

System.out.println ("x+y=" +x+y);

System.out.println (x+y+ "=x+y");

The first sentence outputs the "+" connection literal, and the result is that x and y connect the two data.

The "+" in the statement two output is an operator, and the result is a sum of x and Y.

Java's hands-on brain

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.