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};
Output:false false True SMALL MEDIUM LARGE
Summary: The enumeration type is equivalent to a definition namedenumclass, such as in this program,enum SIZE {samll,medium,large};the meaning is to define a nameSIZEenumeration type, which can be used in theSmall,medium,largetake a value from three elements. and use it when you're using it .SIZE s=size. SMALLThis form is equivalent to defining aSIZETypes of variabless, becausesis aSIZEtype of, so thisscan only haveSmall,medium,largeone of the three values. Assigning a value to an enumeration type variable can also be called directlyvalueOf ()The function converts from a string. However, when using enum typesenumthe ObjectSIZEbefore you need to haveenum Size{small,medium,large}such a word toSIZESets the set of values.
compiling this program with JAVAC will generate 2 class files, indicating the enum Size{small,medium, LARGE}; the equivalent defines a class .
You can use the "= =" and the equals () methods to directly compare the values of the enumeration variables, in other words, for variables of enum type, "= =" and The result of the equals () method execution is equivalent
2, Addition.java
Source:
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
}
}
Run results
As you can see from the results, the method that calls the Integer class in the program converts the string type variable to the int class, so if I enter a decimal in the input box 12.5 and 3.0 will get an error.
Summary: This example mainly learns the use of dialog boxes. Through the Joptionpane class of two method, namely showinputdialog and The simple invocation of Showmessagedialog demonstrates the basic use of dialog boxes.
And because I'm not familiar with Joptionpane, I don't know what functions and interfaces there are, and what functions need to be given which parameters, what the function is, what the return value is, and so on, so we need to check the JDK 1.8 API online. . In this process I have generally learned how to view the API documentation, which shows a more detailed description of the use of each function and interface in the document, and some example. Because it is in English, I also specially downloaded a Youdao dictionary desktop version for screen translation.
1, Testdouble.java
Source:
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));
}
}
Result Analysis: The method of storing data in double type determines that it will not get accurate results when it is being operated. Workaround:
When performing an operation of a double type of data, it is coerced into another data type each time a cloth is computed, or when data representing two double types is equal. The absolute value of the difference between two numbers is less than a particular small number ", say 10e-6 .
1, Testbigdecimal.java
Source:
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));
}
}
Run results
Therefore, the construction of BigDecimal objects can solve the problem of imprecise. However, you should use a string instead of a double value when using it, and it is still possible to raise the computational accuracy issue.
1. String connection operation
Source:
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");
}
}
Analysis: In System.out.println () , if the string string is followed by a + and variable, the variable is converted to string type, the plus sign connects, 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, then the addition and subtraction of the variables are calculated from left to right, and then with the subsequent string to combine into a new 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.
Java Basic Syntax *