2. Java Basic Grammar Notes

Source: Internet
Author: User
Tags arithmetic operators bitwise operators float double logical operators

I. Keywords & identifiers

1, Keywords: By the Java language has been given special meaning, used as a special purpose string (word);

2. Reserved words: The existing Java version is not yet used, but later versions may be used as keywords. Avoid using these reserved words when you name them;

3. Identifiers: All the places where you can name them are called identifiers;

  4. Naming rules for identifiers (must be followed):

1, by 26 letters of the letter case, 0-9,_ or $ composition;

2, the number can not start;

3, can not use keywords and reserved words, but can contain keywords and reserved words;

4, the Java strict case-sensitive, the length of unlimited;

5. Identifiers cannot contain spaces;

5. Name naming specification in Java (recommended):

1, Package Name: Multi-word composition when all letters are lowercase: for example: xxxyyyzzz;

2, class name, Interface name: Multi-word composition, the first letter of all words capitalized: for example: xxxyyyzzz;

3, variable name, method name: Multi-word composition, the first letter lowercase, the second word starts each word capitalized: for example: xxxyyyzzz;

4, constant name: All letters are capitalized, multiple words each word using underline connection: xxx_yyy_zzz;

Second, variable:

  1. Variables in Java are categorized by data type:

Basic data type vs reference data type

    

  2. Basic data type:

    

    

Integer: byte short int (default type) long

Float type: float double (default type)

Character type: char (")

Boolean type: Boolean (can only value true or FALSE, cannot take null)

Add: Depending on the location that exists in the class, it can be divided into:

Member variables vs local Variables

  3. Binary

    

1. Type: binary decimal octal hexadecimal

2, binary: The bottom of the computer is the use of binary storage and operation;

3. Conversion between binary and decimal:

1, binary in the underlying storage (positive, negative) are in the form of a complement (the original code, complement, anti-code)

The original code: directly converts a numeric value into a binary number;

Anti-code: for the original code to take the reverse;

Complement: For anti-code +1;

Positive (original code, inverse code, the same complement);

The first bit is the sign bit: 1 for negative numbers and 0 for positive numbers;

      

4, the conversion between the four kinds of binary;

  4, the operation of the variable:

1, automatic type conversion: Small capacity of the data type automatically converted to a large capacity data type;

Note: The operation is done between a byte short char and the result is int. Data type auto-conversions are sorted from small to large:

    

2, coercion type conversion: is the inverse process of automatic data type conversion, the use of "()" to achieve strong turn, but easy to lead to loss of precision or overflow problems.

Third, Operator:

An operator is a special symbol that represents the operation, assignment, and comparison of data.

  1. Arithmetic operators:

+ - + - * / % ++ -- +

    

Note: 1,/left and right sides of the type need to be consistent;

2. The final symbol and the modulus are the same;

3, before + +, first +1, after the operation + +; First operation, after + 1;

4, +: When string strings can only be concatenated with other data types, and the result is of type string;

  2. Assignment operators:

= += -= *= /= %=

  3. Comparison operators (relational operators)

    

    

    Attention:

1. Both ends of the comparison operator are of type Boolean, meaning either True or false;

2, the comparison operator "= =" and "=" action is different, when using the need to be careful.

  4, logical operators (both ends of the symbol are of type Boolean):

    

    Attention:

1, & and && and | | The difference:

&: Either True or false on the left, the right side will be calculated;

&&: If the left is false, then the right side does not operate;

| with | | The difference is the same as above; we recommend using && | | ;

2, (^) and OR (|) are different: when the left and right are true, the result is false.

  5, bitwise operators (data at both ends are numeric):

     

     

 6, ternary operator

     

      Attention:

1, expression 1 and expression 2 must be the same type;

2, use the ternary operator where you must use IF. else instead, it is not necessarily true;

Four, Process Control:

  1. Sequential structure: The program is executed from the top down order;

  2. Branch structure:

    If.. Else

First format:

if (conditional expression) {   code block executed;        }

The second type of format:

if (conditional expression) {   code block executed;  } Else {   the code block executed;  }

The third type of format:

if (conditional expression) {  code block executed;} Else if (conditional expression)  {if)else{  execute code block;    }

    Note: 1. Once a conditional expression is met, it goes into its execution statement block execution and does not execute the conditional statement once it finishes executing.

2, if more than one conditional expression between the "mutually exclusive" relationship, multiple statements can be reversed between the order, once the inclusion of the relationship, the scope of the condition expression is required to write to the top of the large range;

   Swich...case        

swich (variable name) { Case0: System.out.println ("Helloworld!");  Break;  Case1: System.out.println ("Helloworld!");  Break;  Case2: System.out.println ("Helloworld!");  Break; .........      default: System.out.println ("Helloworld!");  Break; }

      Attention:

1. The return value of an expression in a swich (expression) must be one of the following types:

Byte,short,char,int, enumeration, String

2. The value in the case clause must be a constant, and the values in all case clauses should be different;

3, the default clause is optional, when there is no matching case, the execution of default;

4. The break statement is used to make the program jump out of the Swich statement block after executing a case branch, and if no break program executes to Swich end;

if Else vs. Swich case:

if and Swich statements would like to, if the specific value of the judgment is not many, and composite byte, short, int, char four types. It is recommended to use the Swich statement, because the efficiency is slightly higher;

Other cases: The range is judged, the result is a Boolean type, the use of if,if is more extensive.

2. Java Basic Grammar Notes

Related Article

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.