Java Basic Three

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

Common concepts for 1 operator 1.1 operators
    • Operations: The process of manipulating constants and variables.
    • Operator: a symbol that operates on constants and variables.
    • Operand: The data that participates in the operation is called the operand.
    • Expression: An expression that conforms to the Java syntax of an operator to concatenate constants and variables. The expressions of different operator joins represent different types of expression.
      • For example: int a = 3 + 4;
      • is to do an addition operation.
      • + is the operator, which is the arithmetic operator.
      • 3, 4 is the operation data involved in the operation.
      • 3+4 whole is actually an arithmetic expression.
1.2 Arithmetic operators
    • +, addition
    • -, subtraction
    • *, multiplication
    • /, Division
    • %, take the remainder
    • + +, self-increasing
    • ---, self-reduction

    • Example:
 PackageCom.xuweiwei;/*** operator*/ Public classOperator { Public Static voidMain (string[] args) {//Defining Variables        intx = 3; inty = 4; SYSTEM.OUT.PRINTLN (x+y); SYSTEM.OUT.PRINTLN (x-y); SYSTEM.OUT.PRINTLN (x*y); SYSTEM.OUT.PRINTLN (x/y);//dividing integers can only get integersSystem.out.println (x%y);//Take surplus        /*** If I want to divide integers not to get integers, just change one operand of the operand to a floating-point number*/System.out.println (x*1.0/y); }}

    • Example:
 PackageCom.xuweiwei;/*** + + 、--operator application*/ Public classOperator2 { Public Static voidMain (string[] args) {//define two variables        intx = 3; inty = 4; System.out.println ("x:" +x); System.out.println ("Y:" +y); //Use alonex + +;//equivalent to x=x+1;y++; System.out.println ("x:" +x); System.out.println ("Y:" +y); X=3; Y=4; //participate in the Operation        intA = x + +;//+ + in the back, first assignment, then self-increment        intb = ++y;//+ + in front, first self-increment, then assign valueSystem.out.println ("x:" +x); System.out.println ("Y:" +y); System.out.println ("A:" +a); System.out.println ("B:" +b); }}
1.3 Assignment operators
    • =
    • +=
    • -=
    • *=
    • /=
    • %=

    • Example:
 PackageCom.xuweiwei;/*** Assignment operator*/ Public classOperator3 { Public Static voidMain (string[] args) {intx = 10; System.out.println ("x:" +x); inty = 10; Y+ = 20;//equals y = y +;System.out.println ("Y:" +y); }}
1.4 Relational (comparison) operators
    • = =, equal to
    • ! =, not equal to
    • <, less than
    • , Greater than
    • <=, less than or equal to
    • >=, greater than or equal to
    • instanceof, check if the object is a class, example, "Hello" instanceof String

    • Precautions:
      • The results of the ① comparison operator are either Boolean or True or FALSE.
      • ② comparison operator "= =" do not mistakenly write "=".

    • Example:
 PackageCom.xuweiwei;/*** Relational Operators*/ Public classOperator4 { Public Static voidMain (string[] args) {intA = 3; intb = 4; System.out.println (A==b); System.out.println (A!=b); System.out.println (A>b); System.out.println (A>=b); System.out.println (A<b); System.out.println (A<=b); }}
1.5 Logical operators
    • &, with
    • |, or
    • !, non-
    • &&, short Circuit and
    • | |, short circuit or
    • ^, XOR

    • Example:
 PackageCom.xuweiwei;/*** Logical operator * Features: * Logical operators are typically used to concatenate expressions or values of type Boollean*/ Public classOperator5 { Public Static voidMain (string[] args) {intA = 3; intb = 4; intc = 5; //&: A fake full fakeSystem.out.println ((a > B) & (A > C));//false & false = FalseSystem.out.println ((a > B) & (a < C));//false & true = FalseSystem.out.println ((a < b) & (A > C));//True & false = FalseSystem.out.println ((a < b) & (a < C));//true & true = True//|: A true all-trueSystem.out.println ((a > B) | (A > C));//false & false = FalseSystem.out.println ((a > B) | (A < C));//false & true = TrueSystem.out.println ((a < b) | (A > C));//True & false = TrueSystem.out.println ((a < b) | (A < C));//true & true = True//^: Same as false, different to trueSystem.out.println ((a > B) ^ (a > C));//false ^ false = FalseSystem.out.println ((a > B) ^ (a < C));//false ^ true = trueSystem.out.println ((a < b) ^ (a > C));//true ^ false = TrueSystem.out.println ((a < b) ^ (a < C));//true ^ true = False    }}
1.6-bit operators
    • &, bit with
    • |, bit or
    • ^, Bit XOR or
    • ~, anti-code
    • <<, move left.
    • >>, move right.
    • >>>, unsigned Right shift

    • Example:
 PackageCom.xuweiwei;/*** Bitwise operators:*/ Public classOperator6 { Public Static voidMain (string[] args) {intA = 3; intb = 4; System.out.println (A&b); System.out.println (A|b); System.out.println (A^b); System.out.println (~a); }}
 package   Com.xuweiwei;  /**   * ^ * One data is different to another data or two times, the number itself is unchanged  */ public  class   Operator7 { static  void   main (string[] args) {int  a = 3;         int  b = 4;        System.out.println (a  ^ b ^ b);    System.out.println (a  ^ b ^ a); }}
 Package Com.xuweiwei; /**  */Publicclass  Operator8    {publicstatic  void  Main (string[] args) {        System.out.println (3 << 2);        System.out.println (>> 2);        System.out.println (-24 >>> 2);}    }
1.7 Three mesh operator 1.7.1 format
    • (relational expression)? Expression 1: Expression 2;
    • If the relationship expression is true, the result of the operation is an expression of 1.
    • If the relational expression is false, the result of the operation is an expression of 2.
1.7. Application of 23-mesh operator
    • Example:
 Package Com.xuweiwei; /**  */Publicclass  Operator9    {publicstatic  void  Main (string[] args) {        int a = ten;         int b =;         >= b? a:b);}    }

2 Flow control statements
    • In the process of executing a program, the order of execution of each statement has a direct effect on the result of the program. In other words, the process of the program has a direct impact on the results. So, we have to understand the execution flow of each statement. Moreover, many times we have to control the execution order of the statements to four lines we want to complete the function.
    • Classification of Process Control statements:
      • Sequential structure
      • Select structure
      • Loop structure

3 Sequential structure
    • Is the simplest and most basic process control in the program, no feature of the grammatical structure, according to the Order of the Code, followed by execution, most of the code in the program is executed.
    • In general, written in front of the first execution, written in the back after the execution.

    • Example:
 Package Com.xuweiwei; /**  */Publicclass  orderdemo    {publicstatic  void  Main (string[] args) {        System.out.println ("program started");        System.out.println ("I love Java");        SYSTEM.OUT.PRINTLN ("program ended");}    }

4 Selection structure
    • The selection structure is also known as the branching structure.
    • The selection structure has a specific syntax rules, the code to perform the specific logic run to judge, the result of the logical operation has 2, so the choice, according to different choices to execute different code.
    • The Java language provides two alternative structure statements
      • If statement
      • Switch statement

5 The If statement of the branch structure 5.1 if statement format 15.1.1 Format
if (relational expression) {  statement body;  }
5.1.2 Execution Process
    • First judge the relationship expression to see whether the result is true or false.
    • If true, the statement body is executed.
    • If False, the statement body is not executed.
Application of 5.1.3 If statement format one
    • Example:
 Package Com.xuweiwei;  Public class IfDemo1 {    publicstaticvoid  main (string[] args) {        int x = ten;         if (x >=) {            System.out.println ("x>=10");        }        SYSTEM.OUT.PRINTLN ("program execution ended");}    }

5.2

Java Basic Three

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.