Hands on the brain

Source: Internet
Author: User

I. Enumtest.java

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
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: An 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. 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. Addition.java

Code:


An addition program

Import Javax.swing.JOptionPane; Import Class Joptionpane

public class Addition {
public static void Main (String args[])
{
String Firstnumber,//First string entered by user
Secondnumber; Second string entered by user
int Number1,//first number to add
Number2,//second number to add
Sum Sum of Number1 and number2

Read in first number from user as a string
Firstnumber =
Joptionpane.showinputdialog ("Enter first integer");

Read in second number from user as a string
Secondnumber =
Joptionpane.showinputdialog ("Enter second integer");

Convert numbers from type String to type int
Number1 = Integer.parseint (Firstnumber);
Number2 = Integer.parseint (Secondnumber);

Add the numbers
sum = number1 + number2;

Display the results
Joptionpane.showmessagedialog (
NULL, "The sum is" + Sum, "Results",
Joptionpane.plain_message);

System.exit (0); Terminate the program
}
}

Three. Type conversions in 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

Four. 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.

Five. 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.

Six. 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

Seven. 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.