Today's content:
1. The arithmetic operators
2. assignment operator
3. Relational Operators
4. Logical Operators
The bitwise operators
6. The ternary operator
An operator
Operation: The process variables and constants arithmetic operation called
Operator: constant and variable symbols called operators operating
Operands: data involved in computing called operands
1. The arithmetic operators
1.1. Subtraction, multiplication and division operations
1.1.1 addition and subtraction, multiplication and division operations
+: Positive sign; adder; connection string
-: negative; subtraction
*: Multiplication
/: Division, taken supplier
%: Remainder
Precautions:
Integer and Integer arithmetic phase obtained is an integer, when the next operation from the left, subtraction, multiplication and division after the first count, the first operator brackets parentheses
Case:
. 1 class MathOperatorDemo { 2 public static void main (String [] args) { . 3 int A = 13 is ; . 4 int B = 2 ; . 5 System.out.println (A + B); // 15 . 6 System.out.println ( ab &); // . 11 . 7 System.out.println (a * B); // 26 is . 8 System.out.println (a / B); // . 6, int two phase operation, the result obtained is int . 9 the System .out.println (A% B); // . 1 10 } . 11 }
To a / b is equal to 6.5, the line 8 to one of the two lines of code as follows:
System.out.println (1.0 * a / b) //7.5
System.out.println ((Double) a / b) //7.5, if a / b in parentheses, the result is 7.0
1.2 Save and Save operation jerk
(1) + : before jerk , in front of the variable; Houjia Jia , placed behind the variable (manipulated variable only)
When used alone, both the same, the operation is made in a 1 plus
Involved in computing time and the difference between:
Gaga before: first add 1 after the operation (including assignment, print statements, parentheses)
Hou Jiajia: first operation, after adding 1
Case:
class MathOperatorDemo2 { public static void main (String [] args) { int A = 10 ; int B = 10 ; System.out.println (A ++ ); // 10 Houjia Jia, after the first operation (print) was added 1 , after printing 11 = A System.out.println ( ++ B); // 11 before jerk, before adding 1, after calculation } }
If, before the printing, with a ++; b ++; if both results are printed 11
(2) Exercise
/ * 1: Basic small problems int a = 10; int B = 10; int = 10 C; a = B ++; C = - a; B ++ = a; a = C--; Please a calculated respectively, b, c values * * / class MathOperatorTest { public static void main (String [] args) { int a = 10 ; int B = 10 ; int C = 10 ; a = B ++; // a = 10, B =. 11 Houjia Jia, to assign a = 10, b is incremented by one after the assignment of the 11, behind empathy C = --a; // C =. 9, a =. 9 B a = ++; // B = 10, 10 = A A = C--; // A =. 9, C =. 8 System.out.println(a); //9 System.out.println(b); //10 System.out.println(c); //8 } }
Title slightly more complex
int a = 4; int b = (a++)+(++a)+(a*10);// 70
Note: The brackets also as a kind of operation
(A ++) Houjia Jia, the first operation, the first count parentheses, brackets 4 small value, after adding 1, a 5 becomes
(++ a) before jerk, before adding 1, a 6 becomes the value becomes the parentheses 6
4+6+60 = 70
2. assignment operator
Symbol: =, + =, - =, * =, / =,% =
= Is the basic assignment operator, others are extended assignment operator
NOTE: + =, - =, * =, / =,% = like symbol comprising a cast
The following code (The interview questions)
short s =. 1; s = s +. 1; // error, the right budget End value is int, but is left s type short, int is greater than the short range, it will be given s + =. 1; // equivalent to s = (short) (s + 1 ), so that no error
Case
class GiveValueOperatorDemo{ public static void main(String[] args){ int a = 13; int b = 2; System.out.println(a+=b); //15 a = a+b=15 System.out.println(a-=b); //13 a = a-b=13 System.out.println(a*=b); //26 a=26 System.out.println(a/=b); //13 System.out.println(a%=b); //1 System.out.println(a=b); //2 a=2 System.out.println(a==b); //true } }
3. Relational Operators
The relational operators (also called comparison operators), characterized by the returned results are Boolean values, either true, or is false
==: basic data type connection, compare values for equality; reference data connection type, the address value comparison
! =: Not equal
> ;<;>=;<=;
instanceof: determining whether the object belongs to the class of "hello" instanceof String =====> true relational operators can only be used for this type of data reference
case
class RelationOperatorDemo{ public static void main(String[]args){ int a = 15; int b = 2; System.out.println(a=b);//2 System.out.println(a==b);//true System.out.println(a!=b);//false System.out.println(a>b);//false System.out.println(a<b);//false System.out.println(a>=b);//true System.out.println(a<=b);//true System.out.println("jh " instanceof String);//true } }
4. Logical Operators
4.1 Basic rules
Logical operators typically used to connect Boolean value
(1) &: AND (and) two results are true is true false & true = false
(2) |: OR (or) as long as one is true, then the result is true false | true = true
(3) ^: XOR (exclusive or) different to true, for the same false true ^ true = false
! (4) Non-: (inverted) true = false!
Priority : & precede ^ precede |
6> x> 3 is not so write in java to write x> 3 & x <6
Case
class LogicOperatorDemo{ public static void main(String[] args){ System.out.println((4>3)&(2!=1)); // true: true&true System.out.println((4>3)^(2==1));// true true^false System.out.println((4<3)|(2!=1));//true false|true System.out.println((4>=3)^(2!=1)&(2!=1));//false true^true&true System.out.println(!(2!=1));//false } }
4.2 short-circuit operation
(1)
& : Both are true, the result is true, no matter what the result of the previous operation is carried out behind the expressions operation
&& : When connecting two Boolean expressions, if && previous expression evaluates to false, it is already possible to confirm the results of the whole expression is false, short-circuit operator, behind the operation is not performed
& & & And final calculation results are consistent out
(2)
|: As long as one is true, the result is true, regardless of the outcome of the preceding expression, have carried out operations behind
||:. A short-circuit or if the preceding expression evaluates to true, then the overall result of the expression is true, not in the later operation of the
Case
class LogicOperatorDemo2 { public static void main (String [] args) { int X =. 3 ; int Y =. 4 ; System.out.println ((X ++>. 3) & (Y ++>. 4)); // to false, & also performed later in the code, printed from the back of the display can know the value of y System.out.println (X); // . 4 System.out.println (y); // . 5 } }
If the code is changed as follows (Note the results of running), the
System.out.println ((X ++>. 3) && (Y ++>. 4)); // to false, seen from the results, the code is not running behind && System.out.println (X); // . 4 System.out.println (Y); // . 4
Similarly the symbol " | ", too
4.3 digital connection
& , | , ^ Addition Boolean connection, but also can be used to connect an integer, a short circuit can not be connected operator when connecting digital numbers, calculation is performed for a binary digital connecting turn into binary complement code. bit by bit operation, the 0 as false, as the one to true
"~" bitwise also after an operator, is converted into the digital binary complement code (including the sign bit) is inverted
Exercises:
a = 15, b=2 求a&b,a|b,a^b,~2
class LogicOperatorDemo3{ public static void main(String[] args){ int a = 15; int b = 2; System.out.println(a&b); //2 System.out.println(a|b); //15 System.out.println(a^b); //13 System.out.println(~b); // -3 } }
Explained below
5 Bitwise Operators
Made for shifting the binary complement operation
<<: the most significant bit is discarded, fill vacancies 0
>>: highest fill vacancies
>>>: unsigned right shift, the highest bit 0s
Law:
the left square several times multiplied by 2 is equivalent to the
right is equivalent to several times divided by the square of 2
Case
class BitOperatorDemo{ public static void main(String[] args){ System.out.println(15>>2);// 3 System.out.println(15<<2);// 60 } }
6. The ternary operator
Also called ternary operator
format:
(relational expression) Expression 1:? 2 expression;
if the condition is the result of true, operation is the expression 1;
result if the condition is false, the operation is the expression 2;
the final result of an expression 1 and expression 2 is an explicit value (can not write the output statement)
Exercise
/ * 1. Define two variables of type float, the minimum return 2. Define a variable of type int, if the number is an even number, it returns true, otherwise to false 3. defines two long type variable, if two the number of equal, returns "equal", otherwise unequal 4. define three double variable, and the maximum value of these three numbers * / class ThreeEyesTest { public static void main (String [] args) { // . 1 a float F1 = 12.3F ; a float F2 = 56 is ; a float min = F1 <F2? F2;: F1 System.out.println (min); // 12.3 System.out.println (F1 <F2? F1: F2 ); // 2 int A = 100 ; Boolean? BO =% 2 == 0 A to true : to false ; Boolean BO1% 2 == 0 = A ; System.out.println (BO); // to true // . 3 Long B1 = 100 ; Long B2 = 200L ; String Result ? = b1 == b2 "equal": "not equal" ; System.out.println (Result); // not equal // . 4 Double D1 = 12.3 ; Double D2 = 45 ; Double D3 = 89.3 ; Double TEMP = D1 ?> d1 D2: D2; // find the first two numbers of a maximum double? max = TEMP> TEMP D3: D3; // then compared the two numbers before the maximum value and the third number, System.out.println (max); System.out.println (D1 > D2 (D1? ?> D3 d1: D3) :( d2> D3? d2: D3)); System.out.println (d1 > d2 & d1> D3 d1:?? d2> d2 D3: D3); // if d1 and d2 is greater than d1 > d3, d1 is a maximum, not the maximum or d1, d2 and d3 in the maximum in
Nested practice
Demand:
using nested conditional operator to complete this question:
academic> = 90 points represented by students A,
represented by B between points 80-89,
represented by the following points C 70-79,
60-69 the following points represented by D,
60 with the following E.
class StudentScoreSystem{ public static void main(String[] args){ int score = 69; String result = score>=90?"A":score>=80?"B":score>=70?"C":score>=60?"D":"E"; System.out.println(result); } }
II. Keyboard Input
1. Package guide: import full class name (package name + class name) to be placed above the phrase class of
如: import java.util.Scanner
2. Create a Scanner object
Scanner object name (variable name) = new Scanner (System.in);
3. Use the object gets keyboard input information:
name of the object .nextInt (); get int
object name .nextDouble () Double
....
no nextChar this method
object name .nextLine (); Gets a string of
Note: If a program that uses nextInt (nextDouble.nextLong ....), also using nextLine
If nextLine placed nextInt (nextDouble.nextLong ....) below, it will lead nextLine not yet received content is over
the solution:
1. nextLine into nextInt (nextDouble.nextLong ....) The above
2 may be used alternatively next nextLine
Import java.util.Scanner; class ScannerDemo { public static void main (String [] args) { // 2. Create Object Scanner SC = new new Scanner (the System.in); // . 3 using the object information acquired keyboard input System. out.println ( "Please enter your age" ); int Age = sc.nextInt (); // If the input is not of type int, java.util.InputMismatchException System.out.println ( "Please enter your name" ) ; String name = sc.next (); System.out.println ( "hello," + name + ", the original is your age:" + age + ", the young ah hello" ); } }
Three eclipse of use