01.Java Programming Basics (i)--constants, variables, operators

Source: Internet
Author: User

I. Basic components of Java1. Keywords-words that are given special meanings

???????????? Learn the process of Java to remember.

2. Identifier---refers to the name (class name, object name, variable name, function name, object name, etc.)

? ? ? ????????? 1.26. Uppercase and lowercase letters, 0~9 numbers, _ and $ (no spaces allowed)

? ? ? ? ???????? 2. The number cannot begin, the keyword cannot be used to name

? ? ? ????????? 3. Class name-First letter capital (xxxyyyzzz), Function name-(xxxyyyzzz); Variable name Object name (XXXYYYZZZ)

(1) and (2) are mandatory rules, (3) are unspoken rules

3. Notes

???????? 1. Single-line comment--//comment content

???????? 2. Multi-line Comment--/* Comment */

???????? 3. Document Comments--/** Comments */

4. Constants and variables 5. Operator 6. Statement 7. function 8. Array

?

Two. Constant (constant is a definite value, does not require a name)

Constant types: integer constants, Decimal constants, Boolean constants (values only true,false), character constants, string constants,

NULL constant (value only null--meaning null, used as the value of a reference variable)

To define the format of a constant:

???? Final constant Type constant name = value;

Example: Final int yuanzhoulv=3.1415926

Note: The value of a constant is fixed, as long as the value defined can no longer be changed, meaning that the value of a constant identifier cannot be modified.

Three. Binary

? ? ? ? ? ? -------Binary is just a representation of the number .

1. Eight binary

Every 8 in 1, such as 0234 (beginning with 0, indicating that this is an octal number, which represents the number 156, algorithm: from the lowest number of 0 1 2,? 4*8^0+3*8^1+2*8^2=156)

2.16 Binary

Every 16 in 1,10~15 is represented by a~f, such as 0x9c (beginning 0x means this is a hexadecimal number, which represents the number 156, algorithm: from the lowest number of 0 1 2,? c*16^0+9*16^1=156)

3. Decimal????

Every ten in one, such as 156 (indicating the number 156, algorithm: from the lowest number of 0 1 2,? 6*10^0+5*10^1+1*10^2=156)

4. Binary

Every binary one, such as 0000-0000-1001-1100, (for the number 156, algorithm: from the lowest number of 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15, because of the 0*2^n=0, so the position of 0 omitted,? 2^2+2^3+2^4+2^7=15 6)????

8-bit value range " -2^7--2^7-1"; 16-bit value range " -2^15--2^15-1";

32-bit value range " -2^31--2^31-1"; 64-bit value range " -2^63--2^63-1"

Summarize:

1. In a computer, all data is transformed into binary before binary, and everything is based on the binary

2. The basic algorithm of the computer is the binary system, the life of the decimal, decimal to eight and 16 first turn into binary and then calculate better

3. Binary negative number: (It must start with 1)

156 How do I use a 16-bit binary representation? (Positive take counter plus 1)

First 156 means: 0000-0000-1001-1100, reverse (1 change 0,0 to 1),

Get: 1111-1111-0110-0011, plus 1, get 1111-1111-0110-0100------that's-156

1111-1111-0110-0100 also indicate which negative number of decimal? (minus 1 to reverse)
Minus 1:1111-1111-0110-0011:0000-0000-1001-1100, corresponding to positive 156, so it should be-156

---------This section should remember the name of the identifier rules, the binary positive negative to take the inverse plus 1, minus 1 to take the inverse

Four. Data type

????

Note: The algorithm for values other than char is 0~2*2^ (n-1) "does not include the maximum", the other value of the algorithm is -2^ (n-1) ~ 2^ (n-1) "does not include the maximum value"

Five Variable

????????????? 1.? A variable with the same name cannot be defined within the same interval (the {} of the function is an interval)

??? 2. Define variables:? variable-type variable name = initialization value; example: int?x=9;

???? When do you define variables?

?????? When data is uncertain and needs to be stored , define a variable to complete the storage action

????? 3. Type conversion:

Automatic type promotion?? Example: byte?b=3;b=b+2; (compile fails because 2 defaults to int, left B is automatically promoted to int, 5 is int, cannot be assigned to the left byte b)?? you can only cast???? if the left and right types are different automatic type promotion occurs between integer, float, and character types, the same type encounters are automatically promoted to the default type, between the different types is an integer promoted to a floating point number, the character type is promoted to an integer, the character type is promoted to floating point number. For example: ' t ' +5+ ' \ n ' + The value of 8.0 is 139.0; Because the character Fouis is promoted, it is good to use "" (string) to label characters

Coercion type conversion?? Example: byte?b=3;b= (Byte) (b+2); "Byte must Have ()"

? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? double?d=314321412.14142414513;

?????????????????????????????? int?i= (int) d;//cast, only the integer portion of D is taken

Add: Float retains 7 valid digits, double retains 16 digits of valid digits? Float f=1341.56464f (must add f)?

?

Vi. operators
  1. Arithmetic operator? +? -? *?? /? % (modulo, which takes the remainder)? + + (increment)?--(decrement)? ? (For example: int a=4;a++; When it runs, it uses the value 4 of a in memory at this time, then a+1 operation and assigns the value to a "first use after calculation";???? int a=4;++a; When it runs, it a+1 the first operation and assigns a value to a, then uses the value of a in memory at this time to "Count First")?? ? ?? % modulo takes the remainder?? Example: 5%3=2???? 1%5=1??? -3%3=0?? -3%4=-3?? 3%-4=3? -3%-4=-3???? (All follow the law of mathematics)?? Memory, the number of negative participation in the model, plus or minus to see the left ?? +? String connector
  2. The escape character (still char character) transforms the meaning of the following letter or character by \ (right oblique)?? \ n----line break?? \ r-----Enter?? \b----backspace key?? \ t------tab, equivalent to Tab key?? " \\hello\\ "Show \hello\?? "\" hello\ "" shows "Hello"
  3. Assignment operator 6??? =??? += ??? -=?? *=? ??? /= ? ? %=? The following five algorithms are equivalent to ++a, are first counted after use, for example: X+=5, that is x=x+5, it can also be left and right automatic conversion, for example: Byte b=5;b+=9; can be directly compiled through
  4. Comparison operator Six----"Comparison expression is a Boolean variable"

    == ? != ? <??>? <=? >=??

    The purpose of the comparison is to obtain a true or false result

  5. Logical operator 6---used to concatenate two Boolean variables, and finally get true or false result "the expression is also a Boolean variable" &? &&? | ? || ? ?? ^??!?? ??

? ? ? ? ?? Suppose A and B are all Boolean variables?????????????

& (And)

A&b

There is a false then the result is false "a false is false, all true is true"

&& (and)

a&&b

The first is false and is directly judged false

| (OR)

a| B

There is a true result is true" one true true, all false is false "

| | (or)

a| | B

The first is true then directly evaluates to True

! ( Non)

! A

a if True, then! A is false; A If False, then! A is true

^ (XOR)

A^b

Same false, Xor true

?

6. Bitwise Operators 7----<<??????? >>?????? >>>??????? &???? |???????? ^??????? ~ "Unique algorithm for binary"

??? Since all data is based on binary, any data can be bitwise

<<

9<<3

Get 72

9 binary left shift three bit

Equivalent to decimal 9* (2^3)

Plus or minus, empty 0 fill

>>

9>>3

Get 1

9 Binary right shift three bit

Equivalent to decimal 9/(2^3)

Remainder ignored, positive vacancy 0 complement

-9>>3

Get-2

-9 binary right shift three bit

Equivalent to decimal -9/(2^3)

Plus-1, negative vacancy 1 complement

?

>>>. Unsigned right shift, empty space is 0 complement, so negative result is different from >>

? ????? <<?>> ????????>>>

? ? 0 is false,1 to True

?&?? 0110&0101=0100 can also be written 6&5=4

? ? ?| ? ?? 0110|0101=0111 can also be written as 6|5=7

0110^0101=0011 can also be written as 6^5=3.

??? ~ (anti-code, that is, take the reverse)??? ~0000-0000-0000-0110=1111-1111-1111-1001 is ~6=-7,

???????????. ~-7=6; "~ Positive =--1"??? ~ Negative =-Negative-1 "

?

^ Features: M^n^n=m?? M^n^m=n (can be used to set keys, m,n can be not only numbers, but also characters, strings, symbols, etc.)

?

The classic application of bitwise operators:

???? 1.? Convert 365 to Octal

? ? ? ? ? ?? 365&7,365>> (3*1) &7,?365>> (3*2) &7,?365>> (3*3) &7,365>> (3*4) &7

???? 2. The value of the interchange variable M,n

? ? ? ? ? ? ? ? ?

???????? The first method is the most easy to think of; the second method saves space but has large numerical value, which is prone to the problem of type conversion; The third one is both space-saving and no conversion.

Memory: An expression containing a comparison operator is a Boolean variable, and the logical operator joins a Boolean variable, and the result is a Boolean variable;

01.Java Programming Basics (i)--constants, variables, operators

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.