The third day of Basic Java learning-operators and flow control statements

Source: Internet
Author: User
Tags integer division

The third day of Basic Java learning-operators and flow control statements

Document version Development Tools Test Platform Project name Date Author Remarks
V1.0       2016.02.22 Lutianfei None
Operator Arithmetic Operator value assignment operator comparison operator logical operator bit operator three-object Operator Arithmetic Operators


* Note: Only integers can be obtained for integer division. If you want to get a decimal number, you only need to change any data to a floating point number.
* Eg: System. out. println (x * 1.0/y ); <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Examples + examples/fK/TxzdHJvbmc + examples/examples + examples/fK/TxzdHJvbmc + examples/LV39fUvPWhow0KPHA + examples + DQoJPHA + examples + samples + DQo8aDUgaWQ9 usage "> '+' UsageAddition Operator
System. out. println ('A' + 1); the result is: 98. Connector
System. out. println ("Hello" + 'A' + 1); Result: helloa1 System. out. println ('A' + 1 + "Hello"); Result: 98 hello. The second plus sign is the string connector. Value assignment operator

Symbol:

=, + =,-=, * =,/=, % = Is a basic value assignment operator, and others are extended value assignment operators.

Interview Questions

Short s = 1; s = s + 1; short s = 1; s + = 1; Are there any problems with the above two codes?
First ProblematicThe short variable is first converted Int, There may be Precision loss. Second NoProblem, because The extended value assignment operator implies a forced type conversion., S + = 1; not equivalent to s = s + 1; but equivalent to s = (Data Type of s) (s + 1 );

 

Relational operators


* Note 1: The comparison operator'sResultAllBoolean Type, That is, either true or false.

 

Logical operators

Logical operators are used to connect boolean expressions. They cannot be written in Java. 3 , Should be written x>3 & x<6.

The difference between "&" and:

Order & time, by bit and! Operations are performed on the left, whether true or false, and on the right. Double & hour, logical and! If the left side is true, the right side is involved in the operation. If the left side is false, the right side is not involved in the operation.

The difference between "|" and "|" is the same. In double or mode, the left side is true, and the right side is not involved in calculation.

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

Bitwise operators

Note 2: The character of '^': one data bit is different from the other data bit or twice, and the data remains unchanged.

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

Class OperatorTest {public static void main (String [] args) {int a = 10; int B = 20; System. out. println ("a:" + a + ", B:" + B); // Method 1: Use a third-party variable (most commonly used in development) int c =; a = B; B = c; // Method 2: Use a bit of difference or implement (interview forced item) // conclusion: a, B, a on the left; right: a ^ B a = a ^ B; B = a ^ B; // a ^ B = a ^ B; // a ^ B = B // method 3: add variables a = a + B; // a = 30 B = a-B; // B = 10 a = a-B; // a = 20 // Method 4: one sentence (mandatory for interview) B = (a + B) -(a = B );}}

 

Three-object Operator

Format

(Relational expression )? Expression 1: expression 2If the condition is true, expression 1 is returned. If the condition is false, expression 2 is returned;

Exercise: obtain the maximum value among the three Integers

Class OperatorTest {public static void main (String [] args) {int a = 10; int B = 30; int c = 20; int temp = (a> B )? A: B; // System. out. println (temp); int max = (temp> c )? Temp: c; System. out. println ("max is" + max); // Method 2: Get int max1 = (a> B) in one step )? (A> c )? A: c): (B> c )? B: c );}}

 

Keyboard Input DataHow to input data through the keyboard? (Remember to use it now)
Import package (place on the class definition)
Import java. util. Producer; create an object
Received SC = new pipeline (System. in); receives data
Int x = SC. nextInt ();
Import java. util. extends; class ScannerDemo {public static void main (String [] args) {Using SC = new using (System. in); System. out. println ("enter a data:"); int x = SC. nextInt (); System. out. println ("the data you entered is:" + x );}}

 

Process control statementProcess control statement Classification
Sequential Structure Select structure Loop Structure Sequential StructureSequence Structure Overview
It is the simplest and most basic process control in a program. It does not have a specific syntax structure and is executed sequentially according to the code sequence. Most of the Code in the program is executed in this way. Select StructureThe Java language provides two types of select structure statements.
If statement switch statement First if statement format
If (relational expression) {statement body}

Execution Process

First, judge the relational expression to see whether the result is true or false. If the result is true, the statement body is executed. If the result is false, the statement body is not executed.

Notes

No matter whether it is simple or complex, the result must be a boolean if statement. if it is a statement, braces can be omitted. if it is multiple statements, they cannot be ignored. We recommend that you do not omit this feature. In general, there are no semicolons with left braces and no left braces with semicolons. Second format of if statement
If (relational expression) {statement body 1;} else {statement body 2 ;}

Execution Process

First, judge the relational expression to see whether the result is true or false. If it is true, execute the statement body 1 if it is false. then execute the statement body 2.

Conversion between the second format of if and the ternary Operator

Operations of the ternary operators can be improved using the if statement. When is it not true if it is not true?
The statement body controlled by the if statement is an output statement. Because the ternary operator is an operator, a result must be returned. However, the output statement cannot be used as a returned result. Third format of if statement
If (relational expression 1) {statement body 1;} else if (relational expression 2) {statement body 2 ;}... Else {statement body n + 1 ;}
Use Cases of if Statements:
A: A boolean expression. B: A range.

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.