- 2021-03-31 21:52
*views 6*- Self increasing and self decreasing oper
- Short circuit and
- javase
- operator
- Java
- ternary operato

<>1 operator

<>1.1 summary

operator For connecting expression Of Operands , And perform the operation on the operands .

for example , expression num1+num2, Its operands are num1 and num2, The operator is ”+”.

stay java In language , Operators can be divided into 5 Types ：

Arithmetic operator , Assignment Operators , Relational operators , Logical operators , Bitwise Operators .

Depending on the number of operands , Operators are divided into monocular operators , Binocular and trinary operators .

The unary operator has only one operand , The binocular operator has two operands , The ternary operator has three operands .

Bit operators involve binary bit operations , stay java Not many of them are used in the procedure .

<>1.2 Operator quick reference table

<>1.3 practice ： Test self increase and self decrease

create package : cn.tedu.basic

Create class : TestOperator.java

package cn.tedu.basic; /** This class is used to test the remainder and auto increment auto decrement operators */ public class TestOperator{

//0. Create entry function /**main Alt+/ Complete code ,Ctrl+s preservation Ctrl+F11 implement */ public static void main(

String[] args) { /** Four operations + - * / */ /**syso Alt+/ Complete code */ //2. Test division and remainder % System.

out.println(25/10);//int/int->int, Shang is 2 System.out.println(25 % 10);// model , Mod

//3. Test the add subtract operator /** Prefix form , The symbol comes first , Transform first , Reuse ++a --a * Suffix form , Symbol after , Use first , Change again a++ a-- * ++: plus 1

--: reduce 1 * */ int a = 1; System.out.println("*************"); System.out.println(a

++);// The print result is 1, Change after printing , Self accretion 2 System.out.println(a); int b = 1; System.out.println(

++b);// The print result is 2, Because symbols come first , First increase int c = 1; System.out.println(c--);

// The print result is 1, Symbol after , Print first , Further self subtraction System.out.println(c);// After subtraction, it becomes 0 int d = 1; System.out.

println(--d);// The print result is 0, Symbol before , Subtract before printing // Because of the influence of the previous code ,c The initial value of is 0 System.out.println(--c-c-

c--);//-1-(-1)-(-1)=-1+1+1=1 System.out.println(c);//-2 } }

<>1.4 practice ： Finding the maximum of two numbers

create package : cn.tedu.basic

Create class : TestMaxNum.java

package cn.tedu.basic; import java.util.Scanner;

/** demand : Receive two integers from the user , And compare the output of the maximum of the two numbers */ public class TestMaxNum { public static void

main(String[] args) { //1. Prompt user for input System.out.println(" Please enter the first integer you want to compare :");

//2. Receive the integer input by the user , And give this value to the variable a To save int a = new Scanner(System.in).nextInt(); System.out

.println(" Please enter the second integer you want to compare :"); int b = new Scanner(System.in).nextInt();

//3. Compare two numbers received , Using ternary operators /**1 ? 2 : 3 * 1 It's an expression , if 1 The results are as follows true, The results are as follows 2 position , if 1 The results are as follows false, The results are as follows 3 position *

*/ //4. Defining variables max To save the maximum value of the comparison int max = a > b ? a : b; //5. Print maximum System.out.println(

" What is the maximum of the two numbers :"+max); /** Thinking questions : How to judge 3 Maximum number ? int max = a>b?(a>c?a:c):(b>c?b:c);*/ } }

<>1.5 practice ： Seeking the average year and leap year

create package : cn.tedu.basic

Create class : TestYear.java

demand : Year of receiving user input , Judge whether it is a normal year or a leap year package cn.tedu.basic; import java.util.Scanner; /** *

demand : Year of receiving user input , Judge whether it is a normal year or a leap year * If the year is a leap year , You need to meet one of the following two conditions : * condition 1: Can be 4 to be divisible by , And it can't be 100 to be divisible by *

condition 2: Can be 400 to be divisible by * */ public class Test3_Year { public static void main(String[] args

) { //1. Prompt and receive the year entered by the user System.out.println(" Please enter the year you want to judge :"); int year = new Scanner(

System.in).nextInt(); //2. Define a variable to hold the result String result = " Ordinary year ";// The default value is normal year , Suppose every year is an ordinary year

//3. Judge whether the year entered by the user meets the condition of leap year /** Solution 1*/ /** condition 1: Can be 4 to be divisible by , And it can't be 100 to be divisible by */ // if(year % 4 ==

0) {// Can be 4 to be divisible by // if(year %100 != 0) {// Can't be 100 to be divisible by // result =

" leap year ";// Meet the conditions of leap year 1, The result is a leap year // } // } // /** condition 2: Can be 400 to be divisible by */ // if(year % 400 == 0)

{// Can be 400 to be divisible by // result = " leap year ";// Meet the conditions of leap year 2, The result is a leap year // } /** Solution 2*/ /** Judgment structure

if( Judging conditions ){ The code to be executed after the judgment condition is met } */ //if( condition 1 || condition 2){ It's a leap year } //if(( Minor conditions 1 && Minor conditions 2) || condition 2){

It's a leap year } //if(( Can be 4 to be divisible by && Can't be 100 to be divisible by ) || Can be 400 to be divisible by ){ It's a leap year } if((year % 4 == 0 && year %100

!= 0) || year % 400 == 0){ result = " leap year ";// Meet the conditions of leap year , The result is a leap year } System.out.println(

year+" Year is "+result); } }

<>2 Expansion and supplement :

<>2.1 summary 1: Self increasing and self decreasing operators of arithmetic operators

a Is the operand ,++ Is the autoincrement operator ,– Is a self subtraction operator , Self increasing and self decreasing operators can be placed in front of variables , It can also be placed after variables , for example ：a++,++a,a–,--a etc .

Self increasing （++）： Add the value of the variable to the 1

Fractional prefix （ as ++a） And suffixes （ as a++）. The prefix is added first 1 Reuse ; Suffixes are used first and then added 1.

Self subtraction （–）： Subtracting the value of a variable 1

Fractional prefix （ as –a） And suffixes （ as a–）. The prefix is minus first 1 Reuse ; The suffix is used first and then subtracted 1.

<>2.2 summary 2: Logical operators

Logical operators connect two relational expressions or boolean variables , It is used to solve the combination judgment problem of multiple relational expressions

Note that the operation result returned by the logical operator is Boolean

usually , We use 0 express false, use 1 express true

And ： The relationship between expression and expression

& Shan Yu ： 1 & 2 , It turns out to be true, requirement 1 and 2 It has to be true

&& Double and ( Short circuit and )：1 && 2 , When 1 yes false Time ,2 It will be short circuited , Improve the efficiency of the program

or ： To express or relate to

| Single or ： 1 | 2, It turns out to be true, requirement 1 and 2 As long as there is one for you true Can

|| Double or ( Short circuit or )：1 || 2, When 1 yes true Time ,2 It will be short circuited , Improve program efficiency

<>2.3 summary 3: Priority control

When an expression contains more than one operator , You need to consider the priority of the operator , Operators with higher priority participate in the operation first , Operators with lower priority participate in the operation . In the actual development , There is no need to memorize the priority of operators , And don't deliberately use the precedence of operators , For unclear priority, use bracket to assist priority management .

Technology

Daily Recommendation

views 5

views 3

©2019-2020 Toolsou All rights reserved,

Solve in servlet The Chinese output in is a question mark C String function and character function in language MySQL management 35 A small coup optimization Java performance —— Concise article Seven sorting algorithms （java code ） use Ansible Batch deployment SSH Password free login to remote host according to excel generate create Build table SQL sentence Spring Source code series ( sixteen )Spring merge BeanDefinition Principle of Virtual machine installation Linux course What are the common exception classes ?