Java basic grammar--variable operator--program flow control

Java basic syntax



1. Keywords and reserved words

1. Definition and characteristics of keywords

  1. A string with special meaning and purpose
  2. The letters of the keywords are all lowercase
  3. For example
Define the keywords of the data Define the keywords of the process Keywords defining permissions
class if private
interface else protected
int while public

2. Reserved words

  • The current Java version has not been used and may use goto const in the future.

Second, the identifier

1. Identifier concept

  1. Consists of an alphanumeric underscore $
  2. Cannot start with a number
  3. Avoid keywords and reserved words
  4. case sensitive
  5. Cannot contain spaces such as book name ----> book_name

2. Naming convention

  1. Lowercase package name
  2. If the class name has only one word, the first letter is capitalized. For multiple words, the first letter of each word is capitalized
  3. Interface name and similar name
  4. The first letter of the variable name and method name is lowercase. When there are multiple words, only the first letter of the first word is lowercase
  5. Constant names are capitalized such as MAX_VALUE
  6. When naming, try to be aware of the name book_name book_price

Three, variables

1. Variable concept

  1. Used to save data in memory
  2. Declare before use
  3. Only valid within the range of {}
{
    
    
{
    
     int x=0;}

x=1;//语法错误,x在中间的{}中才有效
}

2. Variable classification

Basic data type Reference data type
byte class (String belongs to class
short interface
int Array[]
long
float
double
char
boolean
8 types 3 types
Member variables Local variable
Declare in the class body Declare in the method body
Instance variable Formal parameters
Class variable static modification Local variables defined in the method
Local variables in code blocks
Has a default value Need to display initialization
Types of Occupied bytes Representation range
byet 1 -128~127
short 2 -215 ~ 215-1
int (default type) 4 -231 ~ 231 -1
long 8 -263 ~ 263 -1

3. Conversion of basic data types

  1. Small capacity will automatically be converted to large capacity type
    (byte char short)—int—long—float----double
  2. Byte short char will not be converted into each other when calculating it will be converted to int first
  3. The boolean type cannot be calculated with other data types
  4. When using forced type conversion, you need to add a forced conversion symbol, but the accuracy may be reduced
int a=3;
short b=(int)a;

Four, base

1. Binary classification

  1. Binary: 0b/0B starts with 2 and enters 1
  2. Decimal: 1 2 3 4…
  3. Octal: Start with 0
  4. Hexadecimal: 0-9 and AF(af) beginning with 0x/0X

2. Several encodings

  1. Original code: Convert a number into a binary positive number, the highest bit is 0, and the highest bit of a negative number is 1.
  2. Inverse code: The inverse code of a positive number is the same as the original code. The inverse code of a negative number is inverted on the basis of the original code except for the highest bit.
  3. Complement code: the complement of a positive number is the same as the original code's complement, and the complement of a negative number is based on the complement of +1

3. Base conversion

  1. Decimal -> Binary multiplied by a power of 2
  2. Binary -> Decimal divided by 2 to take the remainder
  3. Octal -> Binary every 1 bit is represented by three binary digits 011=001001
  4. Hexadecimal—>Binary Each bit is represented by four binary digits 11=00010001

Five, operator

Arithmetic Operator Assignment operator comparison operation, operator Logical Operators Bitwise operator Ternary operator
+ - * / % == != & | ! ^ << >> ?
++ - - < > <= >= && || >>>
instaceof & | ^ ~
  1. When single &, no matter whether the left side is true or false, the right side will be calculated
  2. When double &, when the left side is false, the right side is not calculated
  3. When single|, no matter whether the left side is true or false, the right side is operated
  4. When double|, when the left side is true, the right side is not calculated
  5. Shifting to the left is equivalent to multiplying by 2 1>>2=4 to fill the vacant position with 0
  6. Right shift is equivalent to dividing by 2 4>>2=1 to fill in the sign bit of the vacant bit
  7. >>>Unsigned right shift, the highest bit is filled with 0
  int x=1;
       int y=1;
       if(x++==2&++y==2)
       {
    
    
           x=7;
       }
    System.out.println("x="+x+" y="+y);//2 2
int x=1;
       int y=1;
       if(x++==2&&++y==2)
       {
    
    
           x=7;
       }
    System.out.println("x="+x+" y="+y);//2 1
    
    }
int x=1;
       int y=1;
       if(x++==2|++y==2)
       {
    
    
           x=7;
       }
    System.out.println("x="+x+" y="+y);//7 2
  int x=1;
       int y=1;
       if(x++==2||++y==2)
       {
    
    
           x=7;
       }
    System.out.println("x="+x+" y="+y);//7 2
    
    }

Six, program flow control

1. Three structures

  1. Sequence structure: the program is executed line by line from top to bottom, without any judgment or jump in the middle
  2. Branch structure: if...else switch-case is selectively executed
  3. Loop structure: while do...while for executes a certain piece of code repetitively

2. Description of switch statement

  1. The expression in switch (expression) can be of the following types: byte short char int String enum
  2. The value in the case clause must be a constant
  3. The constant values ​​of different cases are not the same
  4. Case should be followed by a break statement, not as good as the program will execute to the end of switch
  5. default is optional, default will be executed when there is no matching case

Guess you like

Origin blog.csdn.net/qq_43478694/article/details/114798464