Demo 1:
1 public class EnumTest { 2 public static void main(String[] args) { 3 Size s=Size.SMALL; 4 Size t=Size.LARGE; 5 //s and t Reference to the same object? 6 System.out.println(s==t); 7 //Is it the original data type? 8 System.out.println(s.getClass().isPrimitive());//getClass()Run class when returning object 9 //class .isPrimitive()Determine the specified Class Object represents a basic type(This method returns true,If and only if this class represents a base type.) 10 //Convert from String 11 Size u=Size.valueOf("SMALL"); 12 System.out.println(s==u); //true 13 //List all its values 14 for(Size value:Size.values()){ 15 System.out.println(value); 16 } 17 } 18 } 19 enum Size{SMALL,MEDIUM,LARGE};
Results and Analysis:
Size enumeration type defines s, t object refers to different elements in Size, so it is not the same object, so the first output is false.
The second determines whether the class type of object s is a generic type and gets the false output, so the class s belongs to is not a generic type.
The object u is then defined to refer to SMALL and, therefore, to the same element as s.
Then the elements in the Size are printed in a loop to get the output.
Conclusion:
Enumeration type is a special class.
Enumeration type is reference type.
Enumeration types can be used in switch statements.
The objects instantiating different elements in an enumeration type are not the same object, the enumeration is not of the original data type, each of its specific values refers to a specific object, and the same value refers to the same object.
Enumerations make it easy to define constants.
The enumeration type converted from the string can be compared to the original enumeration type and can have the same address.You can use the'=='and'equal()' methods to directly compare the values of an enumeration constant.
Demo 2:
Adds two numbers, enters data with two text boxes, and outputs with one text box.
1 // An addition program 2 import javax.swing.JOptionPane; // import class JOptionPane 3 public class Addition { 4 public static void main( String args[] ) 5 { 6 String firstNumber, // first string entered by user 7 secondNumber; // second string entered by user 8 int number1, // first number to add 9 number2, // second number to add 10 sum; // sum of number1 and number2 11 12 // read in first number from user as a string 13 firstNumber = 14 JOptionPane.showInputDialog( "Enter first integer" ); 15 16 // read in second number from user as a string 17 secondNumber = 18 JOptionPane.showInputDialog( "Enter second integer" ); 19 20 // convert numbers from type String to type int 21 number1 = Integer.parseInt( firstNumber ); 22 number2 = Integer.parseInt( secondNumber ); 23 24 // add the numbers 25 sum = number1 + number2; 26 27 // display the results 28 JOptionPane.showMessageDialog( 29 null, "The sum is " + sum, "Results", 30 JOptionPane.PLAIN_MESSAGE ); 31 32 System.exit( 0 ); // terminate the program 33 } 34 }
Run result:
Demo 3:
Verify the output of the following code.
1 import java.util.*; 2 3 public class InputTest 4 { 5 public static void main(String[] args) 6 { 7 Scanner in = new Scanner(System.in); 8 9 // get first input 10 System.out.print("What is your name? "); 11 String name = in.nextLine(); 12 13 // get second input 14 System.out.print("How old are you? "); 15 int age = in.nextInt(); 16 17 int i,j; 18 String value="100"; 19 i=Integer.parseInt(value);//Integer.parseInt()return a int value 20 j=200; 21 String s=String.valueOf(j);//String.valueOf() return a String object 22 23 // display output on console 24 System.out.println("Hello, " + name + ". Next year, you'll be " + (age + 1)); 25 System.out.println("i="+i+",s="+s); 26 27 28 } 29 }
Run result:
Demo 4:
Verification accuracy is not accurate.
1 public class TestDouble { 2 3 public static void main(String args[]) { 4 System.out.println("0.05 + 0.01 = " + (0.05 + 0.01)); 5 System.out.println("1.0 - 0.42 = " + (1.0 - 0.42)); 6 System.out.println("4.015 * 100 = " + (4.015 * 100)); 7 System.out.println("123.3 / 100 = " + (123.3 / 100)); 8 } 9 }
Run result:
Conclusion: The result is not accurate when using the double type numerical value to calculate.
Ultimately, this question involves the conversion of binary to decimal.N-ary can be interpreted as the power of a number x a cardinality.Numeric values of the Double type take up 64 bits, or 64 binary numbers, and there will always be an error with the actual data at the lowest bit (unless the actual data is exactly the n-th power of 2), except for the bits with the highest bit representing the positive and negative symbols.
So there are almost always errors.
Demo 5:
Processing method for imprecision.
1 import java.math.BigDecimal; 2 3 public class TestBigDecimal 4 { 5 public static void main(String[] args) 6 { 7 BigDecimal f1 = new BigDecimal("0.05"); 8 BigDecimal f2 = BigDecimal.valueOf(0.01); 9 BigDecimal f3 = new BigDecimal(0.05); 10 System.out.println("Use below String As BigDecimal Calculation results for constructor parameters:"); 11 System.out.println("0.05 + 0.01 = " + f1.add(f2)); 12 System.out.println("0.05 - 0.01 = " + f1.subtract(f2)); 13 System.out.println("0.05 * 0.01 = " + f1.multiply(f2)); 14 System.out.println("0.05 / 0.01 = " + f1.divide(f2)); 15 System.out.println("Use below double As BigDecimal Calculation results for constructor parameters:"); 16 System.out.println("0.05 + 0.01 = " + f3.add(f2)); 17 System.out.println("0.05 - 0.01 = " + f3.subtract(f2)); 18 System.out.println("0.05 * 0.01 = " + f3.multiply(f2)); 19 System.out.println("0.05 / 0.01 = " + f3.divide(f2)); 20 } 21 }
Run result:
Conclusion:
Strings should be used instead of double s when building BigDecimal objects, otherwise calculation accuracy problems may still arise.
Demo 6:
String Connection
1 public class Test { 2 public static void main(String[] args) { 3 int X=100; 4 int Y=200; 5 System.out.println("X+Y="+X+Y); 6 System.out.println(X+Y+"=X+Y"); 7 } 8 }
Run result:
Analysis Reason:
1. In the first output statement, the first encounter is a string, so X, Y after it is also converted to string output.At this point "+" is understood as a connector.
2. In the second statement, the first X encountered and the second Y encountered are of type int, and the third is a string, so the connection outputs the result of the operation and the string.At this point "+" is understood as an operator.
Demo 7:
The number of bits and range of values for each data type:
1 byte: 8 bits, range -128-127
(2) short (short integer): 16bits, range of values -32768-32768
(3) int (integer): 32 bits, the range of values is -2147483648-2147483648
(4) long (long integer): 64bits, the value range is -9233372036854477808-9233372036854477808
float (floating point): 32 bits, range of values -3.40292347E+38-3.40292347E+38
Double (double): 64bits, numeric range -1.7976931486231570E+308-1.7976931486231570E+308
char (character type): 16bits, numeric range'\u0000-uffff'
boolean: 1bits, range of values true/false
Conclusion:
Basic data type conversion in java is divided into automatic type conversion and forced type conversion.Automatic data type conversion is the assignment of low priority data to high priority variables.Mandatory type conversions are assignments from higher-priority data to lower-priority variables.Casting typesets often results in loss of precision, especially when larger integer types are converted to decimal types, which can easily occur due to different storage methods.