Hands on the brain

Source: Internet
Author: User

1.enumtest.java

Source:

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 result of the output is: false false true SMALL MEDIUM LARGE

Conclusion: The enumeration type is a reference type and is not part of the original data type, and each of its specific values refers to a specific object, and the same value references the same object. For variables of enum type, the result of "= =" and the Equals () method is equivalent.

2.java variable mask with the same name

Example: static int time=3;
public static void Main (string[] args) {
int time =4;//Local variables can be the same as member variable names
SYSTEM.OUT.PRINTLN ("Time Output value:" +time);
}
You can add a prefix to a member variable name by the way.
Like the subject. If the class name is a.
SYSTEM.OUT.PRINTLN ("Time Output value:" +time);//output local variable value
SYSTEM.OUT.PRINTLN ("Member Variable time output value:" +a.time);//The value of the output member variable

Type conversions in 3.java

1.Int 32-bit value range of -2147483648~2147483647
2.Short 16-bit value range of -32768~32767
3.long 64-bit value range of -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
7.boolean Test Compilation environment
8.byte 8-bit value range of -128~127

4.testdouble.java

Code:

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

The result of the output is:

0.05 + 0.01 = 0.060000000000000005
1.0-0.42 = 0.5800000000000001
4.015 * 100 = 401.49999999999994
123.3/100 = 1.2329999999999999

Conclusion:

The result of a calculation using a numeric value of type Double is imprecise.

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

This involves the conversion of binary and decimal.
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.

6.testbigdecimal.java

Code:

Import Java.math.BigDecimal;

public class Testbigdecimal

{

public static void Main (string[] args)

{

BigDecimal f1 = new BigDecimal ("0.05");

BigDecimal F2 = bigdecimal.valueof (0.01);

BigDecimal F3 = new BigDecimal (0.05);

System.out.println ("Using string as the calculation result of the BigDecimal constructor parameter:");

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

System.out.println ("0.05-0.01 =" + f1.subtract (F2));

SYSTEM.OUT.PRINTLN ("0.05 * 0.01 =" + f1.multiply (F2));

System.out.println ("0.05/0.01 =" + f1.divide (F2));

SYSTEM.OUT.PRINTLN ("Use double as the calculation result of the BigDecimal constructor parameter:");

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

System.out.println ("0.05-0.01 =" + f3.subtract (F2));

SYSTEM.OUT.PRINTLN ("0.05 * 0.01 =" + f3.multiply (F2));

System.out.println ("0.05/0.01 =" + f3.divide (F2));

}

}

The following uses string as the result of the calculation of the BigDecimal constructor parameter:
0.05 + 0.01 = 0.06
0.05-0.01 = 0.04
0.05 * 0.01 = 0.0005
0.05/0.01 = 5
The following uses a double as the result of the BigDecimal constructor argument:
0.05 + 0.01 = 0.06000000000000000277555756156289135105907917022705078125
0.05-0.01 = 0.04000000000000000277555756156289135105907917022705078125
0.05 * 0.01 = 0.0005000000000000000277555756156289135105907917022705078125
0.05/0.01 = 5.000000000000000277555756156289135105907917022705078125

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:

x+y=100200
300=x+y

Conclusion: In System.out.println (), if the string is followed by a + and variable, the variable is converted to a string type, the plus sign is concatenated, and the two strings are concatenated into a new string output, and if the addition and subtraction of a variable is preceded by a string, Then the addition and subtraction of the variable is computed from left to right, and then a new string is combined with the subsequent string. That is, the plus sign is connected only if the two string type or one of them is a string type, otherwise it is still an operator.

Hands on the 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.