problems in Java learning
an enumeration type:
Here is a section of the source program code:
Package Demo;
Public class Enum {
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};
Operation Result:
Results Analysis:Sand theTprinted is not the same object,sReferencessize.small,tReferencesSize.large,so the first result of the output isfalse, sincesis not a raw data type, so the second run result is alsofalse,uConvert to and from string conversionssthe same type of data, so the third one istrue, and finally the outputsizeall the data inside.
Conclusion: Enum types are reference types!
An enumeration 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, for variables of enum type, "= =" and The result of the equals () method execution is equivalent.
two output-time type issue:
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?
Operation Result:
Analysis: The first one in X and Y are not added because the "x+y=" is the same type as the plus join of the String type, which will put the following x and Y are automatically converted to String types, so no addition is performed, and the second one begins with x and Y are int type, can do addition operation, the stock output result is as above.
Scope of the three variables:
each variable has a "valid" area (called a "scope"), out of which the variable will no longer be valid.
take a look at the sample code on the left, what is the output ?
each variable has a "valid" area (called a "scope"), out of which the variable is no longer valid, and the variable with the same name has the principle of automatic masking within the specified range. That is, local variables can have the same name as global variables, and when referenced within a function, local variables of the same name are used instead of global variables.
the output is:2. It is obvious that value=1 is a global variable, and value=2 is a global variable .
Four: Anti-code, complement, the understanding of the original code:
Before you learn the original code , the inverse code and the complement , you need to understand the concept of machine number and truth value first.
1. Number of machines
a binary representation of a number in a computer , called the number of machines. The number of machines is signed, in the computer with a number of the highest bit of the symbol , positive 0, negative number is 1.
For example, in decimal number +3 , the computer word length is 8 bits, converted to binary is 00000011. If it is -3 , it is 10000011 .
So, the 00000011 and 10000011 Here are the number of machines.
2. Truth Value
10000011 1 represents negative, its true value is -3 instead of the formal value 131 ( 10000011 converted to decimal equals 131 Span style= "font-family: the song Body;" >). So, for the sake of differentiation, the true value of the number of machines with the sign bit is called the truth of the machine number.
Example: The truth value of 0000 0001 = +000 0001 = +1,the True value of the 0001 0001 = –1
for a number , the computer is stored with a certain encoding . Original Code , Anti-code , The complement is the way the machine stores a specific number. .
1. Original Code
The original code is the absolute value of the symbolic bit plus the truth , that is, the first digit represents the symbol , and the remaining bits represent the values . For example, if 8 bits Binary :
[+1] Original = 0000 0001
[-1] Original = 0001
the first bit is the sign bit . because the first bit is the sign bit , so 8 the value range of bit binary number is :
[1111 1111, 0111 1111]
That
[-127, 127]
The original code is the most easily understood and calculated representation of the human brain .
2. Anti-code
The inverse code is represented by: Positive inverse code is its own, negative number of the inverse code is in its original code base , the sign bit is unchanged, the rest of each bit is reversed .
[+1] = [00000001] original = [00000001] anti-
[-1] = [10000001] original = [11111110] Reverse
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 .
3. complement
the complement of the expression method is : A positive complement is its own, negative complement is in its original code on the basis of the symbol bit unchanged , the rest of you take the counter , last +1. ( that is, on the basis of the inverse code + 1)
[+1] = [00000001] original = [00000001] anti = [00000001] complement
[-1] = [10000001] original = [11111110] counter = [11111111] complement
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 .
Five: conversion rules for data types in Java:
Conversion of the base data type refers to the automatic completion of the system according to the transformation rules, without requiring the programmer to explicitly declare conversions between different data types. The transformation executes in the compiler, not until the run time.
integer : Byte(8), short(+),Int (+), long (+)
Float type : float (+), double (64)
Specific rules:
(1) Boolean and other basic data types can not be converted to each other;
( 2 byte short , int , long , float and double
( 3 short can be converted to int , long , float and double
(4)char can be converted to int,long,float and double;
(5)int can be converted to long,float , and Double ;
(6)long can be converted to float and double;
(7)float can be converted to double;
Icon:
Note: ( except that the float type is converted to a double type of no precision loss, other types are converted to double types with a precision loss )
That is, only narrow ranges are converted to a wide range, and vice versa.
six question: Why is the value of a double type not "mathematically accurate" as a result of the operation?
answer: This involves a binary and decimal conversion problem. NThe binary can be understood as: a power of numeric x cardinality, such as the decimal number we are familiar with123.4=1xTen²+2x10+3x(Tenof the0Second Power) +4x(Tenof the-1Second Power)the same is true of other binaries, such as binary numbers11.01=1x2+1x(2of the0Second Power) +0+1x(2of the-2Second Power)=of decimal3.25.
A value of type double takes 64bit, or up to four 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 happens to be 2 of the n - th square).
4bit to represent decimals 3.26 2 1,0,-1,-2 Power, according to the top analysis, Should be in the binary number 11.01 ( corresponds to the decimal Span style= "Font-family:consolas;" >3.25) and 11.10 ( corresponds to the decimal 3.5) choose between.
in simple terms, the values that we give, in most cases, require more digits than 64bit to be accurately represented (even for infinite numbers), and the double type has only 64bit, the number of digits in the back is bound to bring an error, unable to get "mathematically accurate" results.
Java Basic question Answer