1. Read the example carefully : Enumtest.java, run it, analyze the running results?
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 first false indicates that s and T do not reference the same object. The second false indicates that s is not the original data type.
Size.valueof can be u get SMALL, then s is the same as U. Then Assign all values of Size.values to value, and then list all values.
2. What conclusions can you get? Have you mastered the basic usage of enum types?
1>javaThe basic data types are8species:Byte (bytes),Short (Short-integer),Int (integer Type),Long (Long Integer type),Float (single-precision floating-point number type),
Double ( dual-precision floating-point number type ) , Char ( character type ) , Boolean ( boolean type ). The enumeration type is not the original data type.
2> Each of its specific values refers to a specific object. The same value refers to the same object.
3> can use the "= =" method to directly compare the value of an enumeration variable.
3. 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,
Look at the output, compare it to the results of the manual calculation , and see if the number in Java is represented by any of these codes.
The original code: binary fixed-point notation, that is, the highest bit is the sign bit, "0" is positive, "1" is negative, the remaining bits represent the size of the value.
Anti-code: The inverse code of positive number is the same as its original code, and the inverse code of negative number is the inverse of its original code, except the sign bit.
Complement: The complement of a positive number is the same as its original code, and the complement of a negative number is added 1in its inverse code.
4. Take a look at the example code above, what is the output ?
The output is 2if the "int value=2 " is removed; "This line of code outputs the result as 1 , this is because: Java Variable Compliance
"Masking principle of variable with the same name". If you want to output value=1, you can use "System.out.println (test.value); "
Where the first value is a member variable and is also a global variable. The second is a local variable. In Java , local variables can override global variables,
when a variable is used locally JVM A variable definition that is close to the current location is preferred.
5. Please run the following code
What kind of output do you see?
This is because a numeric value of type double is used for calculations, and the result is imprecise.
Why is the numeric value of a double type not "mathematically accurate" result?
This involves binary and decimal conversion issues.NThe binary can be understood as: a power of numeric x cardinality, such as the decimal number we are familiar with123.4=1xTen∧ thex10+3x (Tenof the0Power)+4x (Tenof the-1The other binary is also the same solution, such as the binary number11.01=1x2+1x (2of the0Power)+0+1x (2of the-2Power)=of decimal3.25.
A value of type Double takes 64bit, or up to four binary units, to remove the highest bit representing the positive and negative sign, and at the lowest bit there will be an error with the actual data (unless the actual data is 2 of the n - th square).
For example, for example, use 4bit to represent the Span style= "Font-family:times New Roman;" >3.26 2 1,0 , -1 , -2 11.01 (the corresponding 3.25 " and 11.10 (the corresponding 3.5 ) Choose between.
In simple terms, the values we give, in most cases, require more digits than 64bit to be accurately represented (even infinite), and The double type has only the number of digits in the back of the 64bit is bound to bring an error, and the result of "mathematically accurate" cannot be obtained .
6. 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");
Why is there such an output result?
the plus sign in Java is sometimes a connector, sometimes an operator:
1> is a connector when both left and right connections are non-numeric.
2> is a connector when one side is a numeric type and the other is a non-numeric type.
3> When both sides are numeric types, it is the operator, that is, the sum of the sum.
The first is a connector and the second is an operator.
7. Write a program, the user input two number, to find out its subtraction, and the message box to display the calculation results.
Import Javax.swing.JOptionPane; Import Class Joptionpane
public class Test {
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
Cha,//Cha of Number1 and number2
Ji,//Ji of Number1 and number2
Shang Shang of Number1 and Number2
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);
sum = number1 + number2;
Cha = number1-number2;
Ji = number1 * NUMBER2;
Shang = Number1/number2;
Display the results
Joptionpane.showmessagedialog (
NULL, "The sum is" +sum + "\ n" +
"The CHA is" +cha + "\ n" +
"The JI is" + Ji + "\ n" +
"The Shang is" +shang, "Results",
Joptionpane.plain_message);
System.exit (0); Terminate the program
}
}
02-java Grammar Basics (hands-on and after-school assignments)