The third day of basic Java learning--operators and Process Control statements

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

Document Version Development Tools Test Platform Project Name Date author Notes
V1.0 2016.02.22 Lutianfei None
Operator
    • Arithmetic operators
    • Assignment operators
    • Comparison operators
    • logical operators
    • Bitwise operators
    • Trinocular operator
Arithmetic Operators


* Note: integers can only be divided into integers, and if you want to get decimals, simply change any of the data into floating-point numbers.
* EG:SYSTEM.OUT.PRINTLN (x*1.0/y);

    • ++,– :

      • Put in front of the operand, increment or decrement first, and then participate in the operation.
      • after the operand, participate in the operation, and then self-increment or decrement.
    • Exercises 1:

      int a=10,b=10,c=10
      a=b++; a=10,b=11,c=10
      C=–a; A=9,b=11,c=9
      B=++a; A=10,b=10,c=9
      a=c–; A=9,b=10,c=8

    • Exercises 2:

      int x =4
      int y = (x + +) + (++x) + (X*10)
      step1:4+ (x = 5)
      Step2:4+6 (x = 6)
      Step3:4+6 + (6*10)

usage of ' + '
    • Addition operator
      • System.out.println (' a ' + 1); The result is: 98
    • Positive sign
    • String connector
      • System.out.println ("Hello" + ' a ' + 1); Results: HELLOA1
      • System.out.println (' a ' + 1 + "Hello"); Result: 98hello, the second plus is a string connector.
Assignment Operators
    • Symbol:

      • = , +=, -=, *=, /=, %=
      • = is the basic assignment operator, and the other is the extended assignment operator
    • Interview questions

      • Short S=1; s = s+1; Short S=1; S+=1;
      • The above two code has no problem, if there is, there is a problem
        • The first one has a problem , the factor short variable participates in the operation of the first conversion to int, there may be a loss of precision .
        • The second is not a problem because the extended assignment operator implies a coercion type conversion , s+=1; not equivalent to S = s+1; but is equivalent to S = (data type of s) (s+1);


Relational Operators


* Note 1: The result of the comparison operator is Boolean , which is either true or false.


logical Operators

    • Logical operators are used to concatenate Boolean expressions that cannot be written in Java 3<x<6 and should be written x>3 & x<6 .
    • The difference between "&" and "&&":

      • Single &, bitwise with! Whether the left is true or false, the right side is calculated;
      • Double & Time, logic with! If the left is true, the right side participates in the operation, if the left is false, then the right side does not participate in the operation.

      | and "| |" The difference is the same, dual or time, the left is true, the right does not participate in the operation.

    • XOR (^) with or (|) The difference is that when both left and right are true, the result is false.

Bitwise operators

    • Note 2: ' ^ ' Features: one data to another data bit XOR or two times, the data is unchanged.

    • Interview 1: Please implement the exchange of two integer variables yourself

Class operatortest{ Public Static void Main(string[] args) {intA =Ten;intb = -; System.out.println ("A:"+a+", B:"+B);//Mode 1: Use third-party variables (most commonly used in development)        intc = A;        A = b; b = C;//Mode 2: Use a different or implementation (interview loading)        //conclusion: Left A,b,a; right: A^bA = a ^ b; b = a ^ b;//a^b^b = aA = a ^ b;//a^a^b = b        //Mode 3: Adding variables to the procedureA = A+b;//A=b = a A;//b=A = a-B;//A=        //Mode 4: One sentence to get it done (interview loading)b = (a+b)-(A=B); }}


Three mesh operator
    • Format

      • (关系表达式)?表达式1:表达式2
      • If the condition is true, the result of the operation is an expression of 1;
      • If the condition is false, the result of the operation is an expression of 2;
    • Exercise: Get the maximum value in three integers

class OperatorTest{    publicstaticvoidmain(String[] args){        int10;        int30;        int20;        int temp = (a>b)?a:b;        //System.out.println(temp);        int max =(temp > c) ? temp : c;        System.out.println("max is "+max);        //方法2:一步搞定        int max1 =(a>b) ? ((a>c) ? a : c) : ((b>c) ? b : c);    }}


Keyboard input Data
    • How to implement keyboard input data? (now remember to use it first)
      • Guide package (position on top of class definition)
        • Import Java.util.Scanner;
      • Creating objects
        • Scanner sc = new Scanner (system.in);
      • Receive data
        • int x = Sc.nextint ();
import java.util.Scanner;class ScannerDemo{    publicstaticvoidmain(String[] args){        new Scanner(System.in);        System.out.println("请输入一个数据:");        int x = sc.nextInt();            System.out.println("你输入的数据是:"+x);    }}


Process Control Statements
    • Process Control Statement Classification
      • Sequential structure
      • Select structure
      • Loop structure
Sequential Structure
    • Sequential structure Overview
      • Is the simplest and most basic process control in the program, there is no specific grammatical structure, according to the Order of the code, executed sequentially, most of the code in the program is executed in this way.
Select Structure
    • The Java language provides two alternative structure statements
      • If statement
      • Switch statement
If statement first form
if(关系表达式) {    语句体}
    • Execution process

      • First judge the relationship expression to see if the result is true or false
      • If true, the statement body is executed
      • If False, the statement body is not executed
    • Precautions

      • A relational expression is either simple or complex, the result must be a Boolean type
      • If statement control statement body if it is a statement, curly braces can be omitted, if it is more than one statement, it cannot be omitted. Never omit the suggestion.
      • Generally speaking: there is no semicolon on the left curly brace, there is no left brace for the semicolon
If statement second form
if(关系表达式) {       语句体1;  }else {       语句体2;  }
    • Execution process

      • First judge the relationship expression to see if the result is true or false
      • If true, executes the statement body 1
      • If False, executes the statement body 2
    • Conversion between the second format of if and the ternary operator

      • The operation of the ternary operator can be improved by using the IF statement, which is not true
      • When will it not be established?
        • This is not true when the statement body controlled by the IF statement is an output statement. Because the ternary operator is an operator, you must require that a result be returned. The output statement cannot be returned as a result.
The third format of the IF statement
if(关系表达式1) {       语句体1;  }else  if (关系表达式2) {       语句体2;  }    …  else {       语句体n+1;  }
    • Usage scenarios for the IF statement:
      • A: For an expression is a Boolean type of judgment
      • B: For a range of judgments

The third day of basic Java learning--operators and Process Control statements

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.