Understanding of operator and process control related content in Java

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

On the previous operator side, today we review some of the operators ' calculations, priority aspects, and some knowledge of if-else aspects of Process control. Assignment Operators(=, + =,-=, *=,/=,%=, &=, |=, ^=, <<=, >>=, >>>=) Byte/short/char can participate in operations except =, the remaining symbols require that the variable be preceded by a valueNote: The hyphen definition is not supported in Java, but it supports even such assignments
int i = 5;i + = i *= i-= 3;  15i =  5 + (5 * (5 -3)) = Int J = 7;j-= J *= j++;j=7-(7 *7
    ) = 42

  

relational/comparison operators(= = equal!) = unequal > < >= <=)Note: It is not supported to compare in a way logical Operators(& and | or! Non-^ XOR && short circuit with | | Short circuit or) True & ture = True (like playing chess, two people have to come to play this game) |(Let one of the students play a glass of water, this cup of water to fight over a classmate to go on the line) ! (binary in the computer is not true or false, either right or wrong) ^(summed up in physics, compass/magnet, only one North south to meet, the same false, different is true) &&(If the result of the preceding expression is false, the back expression is no longer operational and the result of the entire expression is determined to be false) ||(If the result of the preceding expression is true, the back expression is no longer operational and the result of the entire expression is determined to be true)Note: | | If the && can be shorted out in front of && Bitwise OperatorsNOTE: Bitwise operators operate on the complement of integers, so the result of the operation is also complementary& | ^ << >> >>> ~ &: Convert data to complement form, then treat 0 as False, 1 as true, bitwise AND operation, and finally convert the result to decimal to display even & any number = even positive & any number = positive odd & 1 = 1 even & 1 = 0 |: 7 00000111 convert data to complement form, bitwise OR operation, and finally convert the result to decimal display00001011Odd | Any number = Odd 15 00001111 Negative | Any number = negative ^: 7 00000111 convert data to complement form, bitwise XOR, and finally turn the result into a decimal display000010116^12 = ten, 10^6 = 00001100 i^i=0,j^0 = j, A^b^b=a How to Exchange valuesMode one: XOR or law
int i = 5, j = 9;i = I^j;j = I^j;   j = i^j^j;    ->j=i;i = i^j;   ->i = I^j^i;    ->i = j;
Way two: Add and subtract
int i = 5, j = 9;i = I+j;j = I-j;  J=i+j-j;  j = i; i = i-j;  ->i = I+j-i; ->i = j;
Mode three: Rear-end method
int i = 5, j = 9;int temp = i;i = J;j = temp;
Summary: Three different ways to compareThe efficiency of the XOR method is the highest. Can only be exchanged for integers, the limitations are too large. (like decimals, characters, strings cannot be exchanged) the efficiency of addition and subtraction is lower than the XOR but higher than the rear-end. Numerical types can theoretically be exchanged, but are rarely used to exchange decimals. The efficiency of the rear-end method is the lowest. You can exchange values of any one type. <<3 << 3 00000011 <<: Converts data to complement, then moves to the left by the specified number of digits, discards the high-level move-out, adds 0 to the low-order vacated position, and turns it into a decimal display 24 the00011 the7 << 4 = 7 * 16=112 (equals multiplied by 2^4)within a certain range, the left shift is multiplied by several 2 >>>> 2 00001010 >>: Convert data to complement, then move to the right by the specified number of digits, move out of the data discard, if it is positive, high-order empty 0, if it is negative 1, and finally convert the data to decimal display 00000010Ten>> 3 =1within a certain range, the right shift is divided by 2 of several sidesPositive shift moves the smaller, the minimum is 0 negative numbers move more right, up to 1 >>>: Unsigned right shift. After the data is converted to the complement, move to the right by the specified number of digits, the data moved out, the high-level empty, whether plus or minus 0. ~300000011~: After the data is converted to complement, it will be 0<->1, and finally the data will be converted to decimal display 11111100 ~ = 11 ~i =-i-1-4 10000100 Ternary operatorsFormat: logical value? Expression 1: Expression 2 Execution Order: The logical value is executed first, if the logical value is true, the expression 1 is executed, and the expression 2 is executed insteadNote: The ternary expression itself is an expression that requires a result after execution of the expression---the result must be able to define the corresponding type of variable to catcha > B?  "ABC": TRUE; There is no way to define the result of a uniform type to store, which is wrong
Ternary expression nesting int max = i > J? (i > K i:k): (J > K j:k);

Exercise: output fractions corresponding to the rank  >=90-a  >=80-b  >=70-c  >=60-d  <60-e        char level = score >= 90? ' A ':(score >= 80? ' B ': (Score >= 70?) ' C ': (Score >=60? ' D ': ' E '));        System.out.println (level);
Extension: Getting data from the console
Import Java.util.Scanner; Scanner s = new Scanner (system.in); int i = S.netint ();      Gets the integer double d = s.nextdouble ();      Gets the fractional string str = S.next ();
>=Greater than or equal >>=Right shift equals 2 >=, True10 >>= 2->i = i >> 2; 2 Precedence of Operators~! Arithmetic (+ +--*/% +-) << >> >>> relationship Logic & | ^ Ternary assignment unary two operation (The precedence of several operators of a unary is the same) Process ControlSequential structure: Refers to the code from top to bottom from left to right to compile and run sequentially Branching structure: Judging Structureif (logical value) {code block} execution order: The logical value is executed first, and if the logical value is true, then the code block is executed. Note: If the code block in the if is only 1 sentences, then you can omit {} Do not write if (logical value) {Code1;} else{Code2;}Practice:1. Enter three digits to get the minimum value in three digits
Import Java.util.scanner;public class Ifelseexer {public static void main (string[] args) {Scanner s = new Scanner (system.in int i = S.nextint (); int j = S.nextint (); int k = S.nextint ();/*if (I < J) {if (I < k) {System.out.println (i);} else {Sy Stem.out.println (k);}} else {if (J < k) {System.out.println (j);} else {System.out.println (k);}} */int min = i;if (min > j) min = j;if (min > k) min = k; System.out.println (min);}}

  

2. Enter a number to indicate weight, if the weight is <=20, the charge is $0.35 per kilogram, if more than 20 kilograms of the range is less than 100 kilograms, then the excess is charged at $0.5 per kilogram, and if more than 100 kilograms, the excess is charged at $0.8 per kilogram.
Import Java.util.scanner;public class IfElseExer2 {public static void main (string[] args) {Scanner s = new Scanner (system.i N);d ouble weight = s.nextdouble ();d ouble price = 0;if (Weight < 0) {System.out.println ("illegal weight!!! ");} else {if (weight <=) {Price = weight * 0.35;} else {if (weight <=] {Price = $ * 0.35 + (weight-20) * 0.5;} els e {Price = $ * 0.35 + * 0.5 + (weight-100) * 0.8;}}} SYSTEM.OUT.PRINTLN (price);}}

Or

Import Java.util.scanner;public class Ifelseifdemo {public static void main (string[] args) {Scanner s = new Scanner (System. IN);d ouble weight = s.nextdouble ();d ouble price = 0;if (Weight < 0) {System.out.println ("illegal weight!!! ");}  else if (weight <=) {Price = weight * 0.35,} else if (weight <=) {Price = 7 + (weight-20) * 0.5;} else {price = + (weight-100) * 0.8;} SYSTEM.OUT.PRINTLN (price);}}

  

if (logical value 1) {Code1;} else if (logical value 2) {Code2;} Exercise: Enter a number to represent the month, and then output the season for the month. 3-5-Spring 6-8-Summer 9-11-Autumn 12, 1, 2-winter
Import Java.util.scanner;public class Ifelseifexer {public static void main (string[] args) {Scanner s = new Scanner (System.  IN), int month = S.nextint (), if (Month < 1 | | month >) {System.out.println ("Illegal month!!!");} else if (Month > 2 && Month < 6) {System.out.println ("Spring");} else if (Month > 5 && Month < 9) {System.out.printl N ("Summmer");} else if (Month > 8 && Month <) {System.out.println ("Autumn");} else {System.out.println ("Winter");}}}
Select Structure Loop structure:

Understanding of operator and process control related content in Java

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.