Javase Learning Summary No. 03 Day _java Basic Grammar 2

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

03.01 additional minor issues in the data type

1: When defining a long or float type variable, add either L or F.

integers are type int by default, and floating-point numbers are double by default.

Byte,short at the time of definition, they are actually receiving a value of type int.

2: Default conversion of data type conversion: byte,short,char→int→long→float→double

Why is a long 8 byte default conversion to a 4-byte float?

A: Their underlying storage structure is different.

The range of data represented by B:float is larger than the range of long

Long:2^63-1

float:3.4*10^38 > 2*10^38 > 2*8^38 = 2*2^3^38 = 2*2^114 > 2^63-1

3:java character Char in a language can store a Chinese character? Why?

OK. Because characters in the Java language occupy two of bytes. The Java language uses Unicode encoding.

03.02 basic usage of arithmetic operators

Operators: Symbols that manipulate constants and variables are called operators

Operators are: arithmetic operators, assignment operators, comparison operators, logical operators, bitwise operators, trinocular operators 6 kinds

Arithmetic operators: + (plus),-(minus), * (multiply),/(except),% (remainder or modulo, two-number division), + (connector), + + (self-increment) 、--(self-decrement)

Cases:

1 int x = 3;2 int y = 4;3 System.out.println (x + y);  System.out.println (x-y);  -15 System.out.println (x * y);  126 System.out.println (x/y);  0,/gets the quotient of the division operation 7 System.out.println (x% y);  3,% gets the remainder of the division operation

Note: Integers can only be divided into integers, and if you want a decimal, simply change any of the data in the operation into a floating-point number

such as: System.out.println (x * 1.0/y); 0.75

03.03 arithmetic operators + + and--usage

Example 1: When used alone

1 int x = 3;2 int y = 4;3 System.out.println ("x=" +x+ ", y=" +y);  x=3,y=44 x++;5--y;6 System.out.println ("x=" +x+ ", y=" +y);  X=4,y=3

When used alone, the effect is the same as before and after the operation data. This is the same a++ or ++a effect.

Example 2: When participating in an operation

1 int x = 3;2 int y = 4;3 int a = x++;4 int b =--y;5 System.out.println ("x=" +x+ ", y=" +y);  x=4,y=36 System.out.println ("a=" +a+ ", b=" +b);  A=3,b=3

Put in front of operand when participating in operation: Increment or Decrement first, then participate in operation

At the back of the operand: take part in the operation first, then self-increment or decrement

Example 3: Special cases

1 int i = 3;2 i = i++;3 System.out.println ("i=" +i);  I=3

i=i++, the first i++ operation, at this time the virtual opportunity to open up a new space to temporarily store the value of I, and then perform i++ operations, the results of the operation 4 to I,i=4, when the i++ operation is completed, and then the temporary storage of the value of I is assigned to i,i=3

03.04 arithmetic operators + + and--exercises

1. Calculate values for a, B, c

1 int a = 10; 2 int b = 10; 3 int c = 10; 4 A = b++; 5 C =--a; 6 B = ++a; 7 A = c--; 8 System.out.println ("a=" +a);  A=9 9 System.out.println ("b=" +b);  b=1010 System.out.println ("c=" +c);  C=8

2. Calculate values for x, y

1 int x = 4;2 int y = (x + +) + (++x) + (x*10);  4+6+603 System.out.println ("x=" +x);  X=64 System.out.println ("y=" +y);  Y=70

03.05 multiple usages of arithmetic operator +

+ Usage: addition, plus sign, string connector

Cases:

1 System.out.println (3 + 4);  addition, SYSTEM.OUT.PRINTLN (+5);  Plus, System.out.println ("Hello" + ' a ' + 1);  String connector, HELLOA1

03.06 basic usage of assignment operators

Basic assignment operator [= (Assignment)]

Extended assignment operator [+ = (left plus),-= (left minus), *= (left multiply),/= (left),%= (left modulo)]

Cases:

1 int y = 10;2 y + +;  Equivalent to Y = y + 20;3 System.out.println ("y=" +y);  Y=30

03.07 Polygon Questions for assignment operators

Interview questions:

Short S = 1,s = s+1;

Short S = 1,s + = 1;

Is there a problem with the two lines of code?

Short S = 1,s = s+1; There is a problem, the result of the s+1 is int type, cannot be assigned to the short type S

Short S = 1,s + = 1; No problem, s + = 1 implies a coercion type conversion equivalent to S = (short) (s+1); Extended assignment operators have this feature

03.08 basic usage of comparison operators and their considerations

Comparison operators: = = (equal),! = (unequal), < (less than), > (greater than), <= (less than equals), >= (greater than or equal)

Attention:

1. The result of the comparison operator operation must be true or false

2. The comparison operator "= =" cannot be written as "="

03.09 basic usage of logical operators

Logical operators:& (with), | (or), ^ (XOR),! (non), && (Dual and), | | (Dual OR)

Logical operators are used to concatenate expressions of two Boolean types

& (Ampersand) The Operation Law of the:& operation has a false result is false, only both sides are true, the result is true

| (or) symbol's Operation law: | One of the two sides of the operation is true, the result is true, only both sides are false, the result is false

^ (XOR) notation: | Both sides of the operation have the same result, the result is false, the results are different on both sides, and the result is true

! (non) Symbolic operation law: Judging the other side of things, example:!true=false!false=true

03.10 the difference between logical operators && and &

The Operation Law of && (double and) symbol is the same as the Operation Law of the & (and) symbol.

|| The Operation Law of (double or) sign: with | (or) notation is the same as the Operation law

The difference between & (single and) and && (double and):

& (Single and), regardless of the result of the left side of the operation is true or false, the result of the expression to the right of the operation

&& (double and), when the result on the left side of the operator is false, the value of the right expression is not evaluated because the end result is false

| (single or) with | | (double or) the difference:

| (single or), regardless of the result of the left side of the operation is true or false, the result of the expression to the right of the operation

|| (double or), when the result on the left side of the operator is true, the value of the right expression is not evaluated because the final result is true

Basic usage of 03.11-bit operators 1

Bitwise operators:& (with operations), | (or operation), ^ (XOR), ~ (Inverse code)

Bitwise operations are performed directly on the binary

Cases:

Features of the 03.12-bit XOR operator

Features: A number of different or the same number two times, the result is still this number

Face question for 03.13-bit operation

Do not use third-party variables to achieve the interchange of two integers

Mode 1

A = a ^ b;

b = a ^ b; b = a ^ b ^ b = A;

A = a ^ b; A = a ^ b ^ a = b;

Mode 2

A = a + B;

b = a A; b = A + B-b = A;

A = a-B; A = a + b-a = b;

Mode 3

B = (A + b)-(a = b);

Basic usage of 03.14-bit operators 2 and face questions

Bitwise operator:<< (left shift), >> (right Shift), >>> (unsigned Right shift)

<< (shift left): symbol bit unchanged, low 0, each left shift is equal to 2

>> (right Shift): low overflow, symbol bit invariant, and sign bit to fill overflow high, each right shift is equivalent to dividing by 2

>>> (unsigned Right shift): low overflow, high fill 0

Cases:

Interview questions: Calculate 2 times 8 results in the most efficient way

2<<3

03.15 basic usage of ternary operators

Format: (conditional expression)? Expression 1: Expression 2;

Explanation: The result of the conditional expression is true, and the result of the operation is the value of expression 1

The result of the conditional expression is false, and the result of the operation is the value of expression 2

Cases:

1 int x = 0,y;2 y = (x > 1)? 100:200;3 System.out.println ("y=" +y);  y=200

03.16 practice of ternary operators

1. Get the maximum value in two numbers

1 int x = 100;2 int y = 200;3 int max = (x > y)? x:y;4 System.out.println ("max=" +max); max=200

2. Get the maximum value in three numbers

1 int x = 100;2 int y = 300;3 int z = 200;4 int temp = (x > Y)? x:y;5 int max = (temp > z)? temp:z;6 System.out.println ("max=" +max);  max=300

03.17 basic format of keyboard entry explained

Keyboard input Data Overview: We are currently writing programs, the data values are fixed, but the actual development, the data value is definitely a change, so, ready to improve the data to keyboard input, improve the flexibility of the program.

Keyboard input data steps:

1. Guide Package: (Position on top of class definition) import Java.util.Scanner;

2. Created object: Scanner sc = new Scanner (system.in);

3. Receive data: int x = Sc.nextint ();

03.18 Keyboard Entry Exercises 1

1. Keyboard input two data, sum

1 Import Java.util.Scanner; 2 class Demo 3 {4 public    static void Main (string[] args) 5    {6       Scanner sc = new Scanner (system.in); 7       System.out.println ("Please enter the 1th number:"); 8       int x = Sc.nextint (); 9       System.out.println ("Please enter 2nd number:");       int y = Sc.nextint ();       System.out.println (x+ "+" +y+ "=" + (x + y));    }13}

Operation Result:

Please enter the 1th number: 23 Please enter the 2nd number: 4523+45=68

2. Keyboard input two data, to find the maximum value

1 Import Java.util.Scanner; 2 class Demo 3 {4 public    static void Main (string[] args) 5    {6       Scanner sc = new Scanner (system.in); 7       Sys Tem.out.println ("Please enter the 1th number:");  8       int x = Sc.nextint (); 9       System.out.println ("Please enter 2nd number:");       int y = Sc.nextint ();       int max = (x > Y) ? x:y;12       System.out.println ("Maximum number is:" +max);    }14}

Operation Result:

Please enter the 1th number: 45 Please enter the 2nd number: 32 The maximum number is: 45

03.19 Keyboard Entry Exercises 2

Keyboard input three data, to find the maximum value

1 Import Java.util.Scanner; 2 class Demo 3 {4 public    static void Main (string[] args) 5    {6       Scanner sc = new Scanner (system.in); 7       Sys Tem.out.println ("Please enter the 1th number:"); 8       int x = Sc.nextint (); 9       System.out.println ("Please enter 2nd number:");       int y = Sc.nextint ();       System.out.println ("Please enter 3rd number:");       int z = sc.nextint ();       int temp = (x > Y)? x:y;14       int max = (temp > z)? temp:z;15       System.out.println ("Maximum number is:" +max);    }17}

Operation Result:

Please enter the 1th number: 34 Please enter the 2nd number: 6 Please enter the 3rd number: 4 The maximum number is: 34

03.20 Sequential structure statements

Process Control Statement Classification: Sequential structure, selection structure, cyclic 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.

Overall: Written in front of the first execution, written in the back after the execution

Flow chart:

03.21 SELECT Structure If statement format 1

The selection structure is also known as the branching structure.

The selection structure has a specific syntax rules, the code to perform specific logical operation to judge, the result of the logical operation has two, so the choice, according to different choices to execute different code.

The Java language provides two alternative structure statements

If statement and switch statement

The IF statement is in the 1th form:

if (relational expression)

{

Statement body;

}

Execution process: First judge the relationship expression to see whether the result is true or false, if true, executes the statement body, if False does not execute the statement body

Flow chart:

Cases:

1 int x = 3;2 if (x > 1) 3 {4    System.out.println ("Yes"), 5}6 System.out.println ("over");

03.22 Select Structure If statement considerations

Precautions:

1. The relationship expression is simple or complex, the result must be a Boolean type

2. If the If control statement is a single statement, the If control range {} can be omitted, and if the {} is omitted, if only the single statement that is closest to him is controlled

If the statement of the If control is not a single statement, the IF {} cannot be omitted. Never omit the suggestion.

3. If it is added after the IF (conditional expression), indicating that there is no judgment structure to execute the statement, if it does not control the following statement, whether the condition of the If is true or FALSE, the following statement will execute

03.23 SELECT Structure If statement format 2

The IF statement is in the 1th form:

if (relational expression)

{

Statement body 1;

}

Else

{

Statement body 2;

}

Execution process: First judge the relationship expression to see whether the result is true or false, if true, executes the statement body 1, if false, executes the statement body 2

Flow chart:

Cases:

1 int a = 3,B; 2 if (a > 1) 3 {4    b =; 5} 6 Else 7 {8    b =; 9}10 System.out.println ("b=" +b);//b=100

03.24 practice of selecting structure if statement format 2

Determine whether a number is odd or even

1 int a = 23;2 if (a% 2 = = 0) 3 {4    System.out.println (A + "is even"), 5}6 else7 {8    System.out.println (A + "is odd"); 9}

Run Result: 23 is odd

03.25 If statement format 2 and ternary mutual conversion problem

The ternary operator is actually the shorthand format for if else, if else is a common format

can be simplified as a ternary operator when there is a specific result after the IF else operation

A ternary operator cannot be written when the statement body controlled by the If Else 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.

03.26 SELECT Structure If statement format 3

The IF statement is in the 3rd form:

if (relational expression 1)

{

Statement body 1;

}

else if (relational expression 2)

{

Statement body 2;

}

......

Else

{

Statement body n+1;

}

Execution process

First judge the relationship expression 1 to see if the result is true or false

If true, executes the statement body 1

If False, continue judging the relationship expression 2 to see if the result is true or false.

If true, executes the statement body 2

If false, continue to judge the relationship expression ... See if the result is true or false

...

If no relationship expression is true, the statement body n+1 is executed.

Flow chart:

Cases:

1 Import Java.util.Scanner; 2 class Demo 3 {4 public    static void Main (string[] args) 5    {6       Scanner sc = new Scanner (system.in); 7       Sys Tem.out.println ("Please enter a score:"); 8       int score = Sc.nextint () 9       if (Score < 0 | | score >)          ("Data Error"); 12       }13       Else if (score >=) (          score+ "corresponding Level a"),       }17       else if ( Score >=)       {          System.out.println (score+ "corresponding level B"),       }21       else if (score >=)          System.out.println (score+ "corresponding Class C"),       }25       else if (score >=)          System.out.println (score+ "corresponding level D"),       }29       else30       {          System.out.println (score+ "corresponding level E"); 32       }33    }34}

03.27 practice of selecting structure if statement format 3

Keyboard input x value, calculate Y and output

The relationship between x and Y satisfies the following:

X>=3 y = 2x + 1;

-1<=x<3 y = 2x;

X<=-1 y = 2x-1;

1 Import Java.util.Scanner; 2 class Demo 3 {4 public    static void Main (string[] args) 5    {6       int y = 0; 7       Scanner sc = new Scanner (Syst em.in); 8       System.out.println ("Please enter a number:"); 9       int x = Sc.nextint (),       if (x >= 3) One       {          y = 2 * x + 1;13          System.out.println ("y=2*" +x+ "+1=" +y),       }15       else if (x >=-1 && x < 3),       {          + y = 2 * x;18          System.out.println ("y=2*" + x+ "=" +y),       }20       else21       {          x y = 2 * x-1;23          System.out.println ("y=2*" +x+ " -1=" +y);    }26}

03.28 practice of selecting structure if statement format 3

Keyboard input month value, output corresponding season

Spring (3,4,5), Summer (6,7,8), Autumn (9,10,11), Winter (12,1,2)

1 Import Java.util.Scanner; 2 class Demo 3 {4 public    static void Main (string[] args) 5    {6       Scanner sc = new Scanner (system.in); 7       Sys Tem.out.println ("Please enter a month:"); 8       int month = Sc.nextint (), 9       if (Month > | | month < 1)          System.out.println ("Incorrect month entered"); 12< C8/>}13       Else if (month >= 3 && month <= 5)       {          System.out.println (month+ "month corresponds to Spring"); 16       }17       Else if (month >= 6 && month <= 8)       {          System.out.println (month+ "month corresponds to Summer"); 20       }21       Else if (month >= 9 && month <=)       {          System.out.println (month+ "month corresponds to Autumn"); 24< c20/>}25       else26       {          System.out.println (month+ "month corresponds to Winter");       }29    }30}

usage Scenarios for the IF statement:

1. For an expression is a Boolean type of judgment

2. For a range of judgments

03.29 nested use of the SELECT structure if statement

Cases:

1 int a = 10; 2 int b = 30; 3 int c = 20; 4 int Max; 5 if (a > B) 6 {7      if (a > C) 8      {9         max = a;10      }11      else12      {          max = c;14      }15}16 els E17 {    if (b > C)    {        max = b;21    }22    else23    {       max = c;25}26    }27 System.out.println ("Max:" +max);

Javase Learning Summary No. 03 Day _java Basic Grammar 2

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.