* Hands-on brain:
Question 1: Read the example carefully: Enumtest.java, run it, analyze the running results?
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? No
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};
Run results
False
False
True
SMALL
MEDIUM
LARGE
Question two: What conclusions can you get? Have you mastered the basic usage of enum types?
Constants of enumerated types are stored in the form of strings. The source code s and T are not raw data types. GetClass (): Gets the class object to which the current object belongs; Isprimitive: Used to determine whether the specified class class is a base type, so S.getclass (). Isprimitive () is used to determine if s is the base type. Size u=size.valueof ("SMALL"), which is used to convert the data type. Size.value () iterates through all the elements in a size. Use the "= =" and the Equals () methods to directly compare the values of the enumeration variables.
* After-school exercises:
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 numbers, negative numbers of various bit operations, observe the output, and the results of the manual calculation, to see the number in Java is the above-mentioned code representation.
The original code: the original code is the absolute value of the symbol bit plus the truth, that is, the first digit symbol, the remaining bits represent values.
Inverse code: Positive number: The inverse code of positive number is the same as the original code. Negative number: The inverse of the negative, the sign bit is "1", the numeric part is reversed by a bit. It can be seen that if an inverse code represents a negative number, the human brain cannot visually see its value. It is usually converted to the original code and then calculated.
Complement: A positive complement is its own. The complement of a negative number is on the basis of its original code, the sign bit is unchanged, the rest of you take the counter, the last +1. (That is, on the basis of the anti-code +1). For negative numbers, the complement representation is also the human brain can not intuitively see its value. It is also often necessary to convert the original code to calculate its value.
* After-school exercises:
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: Package com;
Class Test {
private static int value=1;
public static void Main (string[] args) {
int value=2;
System.out.println (value);
}
}
Output results: 2
Each variable has a "valid" area, becomes a valid field, and out of this area, the variable will no longer be valid.
* Hands-on brain:
What conclusions can you draw in view of the number of bits per data type in Java, and the range of values that are represented?
The basic data types are:
int length data types are: Byte (8bits), short (16bits), int (32bits), Long (64bits);
Float length data types are: Single precision (32bits), double precision (64bits double);
The values of Boolen type variables are: Ture, false;
Char data types are: Unicode characters;
The 16-bit corresponding class types are: Integer, Float, Boolean, Character, Double, short, Byte, Long
Conclusion: from low precision to high precision conversion, basic type to class type conversion, class type to string conversion, basic type to string conversion
* Hands on the brain
Why is the numeric value of a double type not "mathematically accurate" result?
float and double types are primarily designed for scientific calculations and engineering calculations, and they perform binary floating-point operations, which are divided into three parts: The sign bit, the exponent, and the tail part of the computer. When data is stored as binary in the computer, a series of wireless loops appears, and errors occur during data conversion. Decimal cannot accurately represent 1/3, and the same binary cannot accurately represent 1/10.
* Attention to thinking:
You should use a string instead of a double value when building a BigDecimal object, or it is still possible to raise the problem of computational precision. (why is this?) )
Double+,-cannot accurately represent BigDecimal (String) 16-bit effective numbers when using bigdecimal, it makes sense to apply the *,/constructor to create objects. An object created by BigDecimal cannot use traditional arithmetic operators to perform mathematical operations on its objects directly, but must call the corresponding method. The argument in the method must be an BigDecimal object.
* Hands on the brain
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");
Output Result: x+y=100200 300=x+y
Why is there such an output result?
Statement one output in the "+" connection literal, the result is that x and Y will connect the two data.
The "+" in the two outputs of the statement is an operator that sums X and Y.
* After-school assignments 1
1, write a program, the user input two number, to find out its subtraction, and the message box display calculation results.
Source:
Import Java.util.Scanner;
Import javax.swing.joptionpane;//Import Class Joptionpane
public class Operation {
public static void Main (String args[]) {
Scanner input=new Scanner (system.in);
System.out.println ("Please enter two integers:");
Double A, B;
A=input.nextdouble (); Enter the first number
B=input.nextdouble (); Enter second number
Double sum,difference,product,quotient;
Sum=a+b;
Difference=a-b;
Product=a*b;
quotient=a/b;
Output dialog box displays the results
Joptionpane.showmessagedialog (
NULL, "The sum is" + sum + "\ n" +
"The difference is" + difference + "\ n" +
"The product is" +product+ "\ n" +
"The quotient is" +quotient, "Results",
Joptionpane.plain_message);
System.exit (0);
}
}
Operation Result:
Java language Basics Related issues