Java variable, binary, data type, method, original code, complement, anti-code

Source: Internet
Author: User
Tags float double

1. Variables
1. He She I you someone you want to be rich x-man X = 1
Hello! It

(a variable is a pronoun in a natural language)
2. int age = 15;//00000000 00000000 00000000 00001111
3. Variables in Java
A Java is a strongly typed language,
b variables must be declared and initialized for later use
C variable must have a definite type
D variables cannot be defined repeatedly
4. Scope of variables
A at the point where the declaration begins, until the end of the block
b leave the scope of the variable to end, and the variable will be recycled.

2. binary
1. Only binary data inside the computer!
int i = 15; char c = ' A ';
' A ', 00000000 01000001
00000000 00000000 00000000 00001111
115 = 1*100 + 1*10 + 5*1
= 1x10^2 + 1x10^1 + 5x10^0
Base: 10
Right: Cardinal ^n

Base: 2
Rights: 128 64 32 16 8 4 2 1

2. Binary: 1111 = 1*2^3+1*2^2+ 1*2^1 + 1*2^0
= 1*8 + 1*4 + 1*2 + 1*1
= 15 (10)


8421
1001 = 9

01000001 = 1*64 + 1*1
= 65 (10)

128 64 32 16 8 4 2 1
1 1 0 0 0 0 0 0 = 192 (10)

3. Decimal: 65 = 6*10 + 5*1
= 6*10^1 + 5*10^0

256 128 64 32 16 8 4 2 1
95 (10) = 0 1 0 1 1 1 1 1 (2)

int age = 15;
Age = 00000000 00000000 00000000 00001111
int age = 0xf;

4.16 Binary
0 1 2 3 4 5 6 7 8 9 a B c D E F
10 11 12 13 14 15

41 (16) = 4 * 16 +1 = 65 (10)
4 1
0100 0001 (2) = 65 (10)
* The 16 binary is used as a shorthand for the 2 binary.
4e2d (16) =0100 1110 0010 1101 (2)

5. Eight binary is a shorthand for binary, 3-bit corresponding conversion
  

3. Original code, complement, anti-code
1, the original code: a positive number, according to the absolute value of the conversion into binary numbers, a negative number is converted to the absolute size of the binary, and then the highest bit 1, called the original code.
For example, 00000000 00000000 00000000 00000101 is the original code of 5.
10000000 00000000 00000000 00000101 is the original code of-5.

Note:
For example, byte type, with 2^8 to represent unsigned integers, is 0-255;
If there are symbols, the highest bit represents the symbol, 0 is positive, and 1 is negative, then the normal understanding is 127 to +127. This is the original code,
It is worth mentioning that the weakness of the original code, there are 2 0, that is +0 and 0 (10000000 and 00000000);
There is the addition or subtraction of the same number, compared to the idiot, the first to determine the absolute value of 2 numbers, and then add and subtract operations, the final result of the symbol is also the same as the large symbol;
So, the anti-code produced.

2, anti-code: Positive anti-code and the original code is the same, negative code for the number of the original code in addition to the sign bit out of each of you take the reverse [each bit to reverse (except the sign bit)].
Take the counter-operation means: The original is 1, get 0; 0, 1. (1 change 0; 0 Change 1)
For example: Positive 00000000 00000000 00000000 00000101 Anti-code or 00000000 00000000 00000000 00000101
Negative numbers 10000000 00000000 00000000 00000101 have an inverse code of 11111111 11111111 11111111 11111010.

The anti-code is mutual, so it can also be said: 10000000 00000000 00000000 00000101 and 11111111 11111111 11111111 11111010 are anti-code each other.

Note: There are still +0 and-0, not too long, the anti-code becomes the filter product, that is, the complement appeared later.

3, complement: positive complement and the original code is the same, minus the complement of the number of the original code in addition to the sign bit out of the counter, and then in the last one plus 1.
For example: 10000000 00000000 00000000 00000101 The inverse code is: 11111111 11111111 11111111 11111010.
Then, the complement is:
11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111 11111011

Note: 1, the method of seeking the original code from the complement is the same as the original code, can also be done by a complete inverse, first minus one, then take the reverse.
2, the complement but stipulates that 0 does not have the positive and negative points

So, 5 is expressed in the computer as: 11111111 11111111 11111111 11111011. Convert to 16 binary: 0xFFFFFFFB.

Example:

A. -2*2 = 4

-2 1110
X 2 0010
---------------
0000
1110
0000
+ 0000
----------------
-4 1100

B.-2 + 3 = 1

-2 1110
+ 3 0011
111
-----------------
1 0001

c. -1+-1 =?

-1 1111
-1 1111
+ 1111
-------------------
-2 1110

D. 7+1 =?
7 0111
+ 1 0001
-----------------
-8 1000

E.~3 + 1 =?

3 0011
~ 1100
+1 0001
------------
-3 1101

-3 1101
~-3 0010
+1 0001
-------------
3 0011

Emphasis: The minus sign is only connected to the first digit in the complement operation, and then to the subsequent number, as in the example above -1+-1 (1110 is the complement of 2), to + 1 (1101 is the complement of 3)


                 Summary:
                  Positive numbers have the same inverse code and complement as the original code.
                 Negative numbers are reversed for the original code of the number except for the sign bit.
                 Negative complement for the number of the original code in addition to the sign bit, and then the last one plus 1
                
                 Source: The advantage is that the simple disadvantage of conversion is that two 0 plus subtraction requires a separate operation
                  Anti-code: A bit of a clear drawback is that two 0 plus subtraction also requires a standalone operation
                  Complement: The advantage is that a 0 range large   subtraction can be converted to addition with the disadvantage of understanding difficulties

      
Data type of 4.Java
1. Java data is divided into basic types and reference types. Person someone
2. The basic types are divided into eight categories:
byte short int long float double char Boolean


(a) integer type byte short int long
integers are signed integers
BYTE 8bit-128 ~ +127
Short 16bit-32768 ~ 32767
int 32bit-2g ~ 2g-1 -2^31 ~ 2^31-1
Long 64bit-2^63 ~ 2^63-1

int a = 5;
It is recommended to use int, rarely use short, use int to pay attention to the value range, int value is not big!

Direct volume-literal: the constant value given directly, such as: 1
A. The type of the direct quantity of an integer is the int type by default
B. The literal ending in l,l is a long type
1024
1024L
C. You can assign a value to the Byte,short type using an integer direct amount, but do not exceed the range of byte and short.

(b) Floating point number: Is decimal, float, double
34642323.44 = 3.464232344 * 10^7
Mantissa Index
10001000. (2)
1.0001000 * 2^111 (2)
Mantissa Index
Float 32 bit
0 00000111 10001000000000000000000
Sign bit exponent mantissa

int a = 0x7fffffff;
float F = A;

Double
1 sign Bit
11 Index
52 Mantissa

A. Double is 64 bits, float 32 bit
Double is more accurate than float
B. The default floating-point number literal is: double type
Literal suffix: d/d f/f
Double d = 1d;//1.0
1 1L 1D 1F
C. Use only double to calculate floating-point numbers, float type basic NO!
Floating-point numbers cannot be calculated accurately

int:10001000-float:1.0001000*2^111

0 00000111 10001000) 00000000 0000000

(c) Character type: char, character type is a 16-bit unsigned integer!
A. The value of the character type is the encoding of the corresponding character, which is the Unicode encoding. Java supports internationalization.
The value of the English part is consistent with the ASCII encoding.
Char is a fixed-length encoding, and all characters are 16 bits.
' A '-0x0041
0x4e2d, ' Zhong '
In the code: ' 0 ' ~ ' 9 ', ' a ' ~ ' Z ', ' a ' ~ ' Z ' are all consecutively coded!
' 0 ' = = ' \u0000 ' \u0000 ' ==0
B. Minimum value: 0, max: 65535 = 2^16-1
C. character literals using single quotation marks as bounding symbols: ' Medium '
Literal is also an integer constant!
Special characters are represented by escape characters:
such as: ' \ n ' \ t ' \ \ ' \b ' \ R '
' \ ' \ ' \u4e2d '

char c = ' \ \ ';

(d) Boolean type: Boolean
Literal: True False
float f = (float) 2.6D;
char C = 65;

5. Data type conversions:
1. Automatic type conversion (implicit type conversion)
Auto-complete from small type to large type
such as: int n = ' A ';
Long L = ' A ';
float, long, int, char/short

2. Coercion of type conversions, from large types to small types that require casts
Forced type conversions are risky, resulting in loss of precision or overflow
Long L = 1024l*1024*1024*4;//0x100000000
int i = (int) l;//0
Double pi = 3.1415926535897932384626;
float f = (float) pi;

Methods in 6.Java
1 method is the function: Y=f (x) =3x+6;
2 syntax of the method
(modifier words) (return value type) (Method Name) (parameter list) {
Method body
}
3 method requires a return statement, return and return value type
Compatible data
4 The parameters of the method and the variables inside the method are local variables,
These variables are scoped only inside the method!
5 arguments: Parameters for method calls
Parameters: The parameter definition of a method, which is a temporary variable.

If there is an imperfect place please make more comments, please indicate the source when forwarding!

Java variable, binary, data type, method, original code, complement, anti-code

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.