Java learning 04

Review 03:

1, "&" and "&" The similarities and differences between
the same points: & && calculation result is the same, when the left symbol is true, both of which can perform an operation on the right.
Differences: When the symbol on the left is false, & continue operation right of the sign. && no longer perform the operation right of the sign.
2. The program output.

class OperatorTest {
	public static void main(String[] args) {
	boolean x = true;
	boolean y = false;
	short z = 40;
	i评((z++ == 40) 8& (y = true)) {
		Z++;
	}
	if ((x = false) II (++z = 43)) {
		Z++;
	}
	System. out.println("z = "+ z);
	}
}

Results: 44
3. Define three int type and assign, using the ternary operator or if-else acquiring achieve greater number of the three numbers.

int num1 = 10,num2 = 15, num3=-20;
int max;
if(num1>=num2 && num1>=num3){
	max=num1;
}else if(num2>=num1 && num2>=num3){
	max=num2;
}else{
	max =num3;
}
System.out.println(max);
 (((n1>n2)? n1 : n2)>n3)? ((n1>n2)? n1 : n2) : n3;

4. Write a program, declare two double variables and assignment. Determining a first number is greater than 10.0, and the second number is less than 20.0, and the number of prints two. Otherwise, the print product of two numbers

double d1=12.3;
double d2=32.1;
if (d1>10.0 && d2<20){
	
}else{

}

The exchange implementation code values ​​of two variables

int a=10;
int b=20;
int c;
c=a;
a=b;
b=c;

Arithmetic Operators + positive - negative + Addition - Subtraction * multiplication / division% modulus (remainder) + (before) + (after) - (pre) - (after) +

//除号:/
int num1 = 12;
int num2 = 5;
int result1 = num1/num2;
System.out.println(result1);

//(前)++ :先自增1,后运算
//(后)++ :先运算,后自增1
int a1 = 10;
int b1 = ++a1;
System.out.println("a1= "+ a1 + ", b1 = " + b1);
int a2 = 10;
int b2 = a2++;
System.out.println("a2= "+ a2 + ", b2 = " + b2);

//(前)-- :先自减1,后运算	
//(后)-- :先运算,再自减1
int a4 = 10;
int b4 = a4--;
System.out.println("a4= "+ a4 + ", b4 = " + b4);
int a5 = 10;		
int b5 =--a5;
System.out.println("a5= "+ a5 + ", b5 = " + b5);

Special instructions;
1 .// (front) +: a first increment, after calculation
// (rear) +: first calculation, since increasing 1
2 .// (pre) -: First Decrement 1, after calculation
// (rear) -: first calculation, and then from minus 1
3. connector: "+" (only used between the string can be used with other data types)

Assignment operator + = = - = = * / =% =

int i1 = 10;
int j1 = 10;
int i2,j2;
//连续赋值
i2 = j2 = 10;
int i3 = 10,j3 = 20;

int num1 =  10;
num1+= 2;//num1 = num1 + 2;
System.out.println(num1);

int num2=12;
num2 %= 5;//num2= num2%5
System.out.println(num2);

short s1 = 10;
//s1 = s1+2;//编译失败
s1+=2;//不会改变变量本身的数据类型;
System.out.println(s1);

Special Instructions:
1. The result of the operation does not change the data type of the variable itself
// development, if desired operating variable to achieve +2, there are several ways (provided: NUM = int 10;)?
// a manner: num NUM + 2 =
// Second way: num + = 2; (recommended)
// development, if you want to achieve variable operation +1, there are several ways (premise: NUM int = 10;)?
// one way: +. 1 NUM = NUM
// Second way: = NUM +. 1;
// three ways: num ++; (recommended)

Comparison operator ==! => <<=> = Instanceof

int i = 10;
int j = 20;
System.out.println(i==j);//false
System.out.println(i = j);//20

boolean b1 = true;
boolean b2 = false;
System.out.println(b1==b2);//false
System.out.println(b2=b1);//true

Special Instructions:
1. The result is a Boolean comparison operator
2. <>> = <=: can be used only between a numeric data type
3 = ==:.! May be used between only numeric data type, further between the reference variable may be used in other types
of C ont acct1 = new new account (1000);
of C ont acct2 = new new account (1000);
Boolean B1 = (== acct1 acct2); // if the comparison is the same account account two
boolean b2 = (acct1 = acct2!);

Logical operators: & && || |! ^

//区分& 与&& 
		//相同点:&与&&的运算结果相同,当符号左边为true,二者都会执行右边的运算。
		//不同点:当符号左边为false时,&继续执行符号右边的运算。&&不再执行符号右边的运算。
		//开发中,推荐使用&&
		boolean b1=true;
		b1 = false;
		int num1=10;
		if(b1 & (num1++>0)){
			System.out.println("北京");
		}else{
			System.out.println("南京");
		}
		System.out.println("num1 = " + num1);

		boolean b2 = true;
		b2 = false;
		int num2 =10;
		if(b2 && (num2++>0)){
			System.out.println("北京");
		}else{
			System.out.println("南京");
		}
		System.out.println("num2 = " + num2);

		//区分 | 与 ||
		//相同点1: | 与|| 的运算结 果相同
		//相同点2:当符号左边是false时,二者都会执行符号右边的运算
		//不同点3:当符号左边是true时,|继续执行符号右边的运算,而||不再执行符号右边的运算
		//开发中推荐使用||
		boolean b3 = false;
		b3 = true;
		int num3 = 10;
		if(b3 | (num3++>0)){
			System.out.println("北京");
		}else{
			System.out.println("南京");
		}
		System.out.println("num3 = " + num3);
		
		boolean b4 = false;
		b4 =true;
		int num4 = 10;
		if(b4 || (num4++>0)){
			System.out.println("北京");
		}else{
			System.out.println("南京");
		}
		System.out.println("num4 = " + num4);

		boolean x=true;
		boolean y=false;
		short z=42;
		//if(y == true)
		if((z++==42)&&(y=true))
			System.out.println("z="+z);
			z++;
			System.out.println("z="+z);
		System.out.println("z="+z);
		if((x=false) ||(++z==45)) 
			z++;
		System.out.println("z="+z);

! ab & A & B A && B ALB ALLB AA ^ B
to true to true to true to true to true to true to false to false
to true to false to false to false to true to true to false to true
to false to true to false to false to true to true to true to true
to false to false to false to false to false to false to true to false
Special Instructions:
1. Logical Operators operating It is boolean type variable, and the result is a boolean

Bitwise operators: << >> >>> & | ^ ~

int i = 21;
	System.out.println("i << 2 : " + (i << 2));//21*4
	System.out.println("i << 3 : " + (i << 3));//21*8
	System.out.println("i << 27 : " + (i << 27));
	System.out.println("i << 26 : " + (i << 26));
	System.out.println("i >> 2 : " + (i >> 2));
	 i = -21;
	System.out.println("i >> 2 : " + (i >> 2));
	System.out.println("i >>> 2 : " + (i >>>2));

	int m = 12;
	int n = 5;
	System.out.println("m & n : " + (m & n));
	System.out.println("m | n : " + (m | n));
	System.out.println("m ^ n : " + (m ^ n));

[Face questions] Can you write the most efficient implementation of 2 * 8?
Answer: 2 3 or 8 << 1 <<
1 bits are integer operations on data operator
2. <<: certain the range of each one to the left, equivalent to 2 *
>>: within a certain range, one each to the right, corresponding to / 2
typical subject:
1. exchange two variables
2. implement 60 binary to hexadecimal conversion

//方式一:自动实现
String str1 = Integer.toBinaryString(60);//二进制
String str2 = Integer.toHexString(60);//十六进制
//方式二: 手动实现
int i1 = 60;
int i2 = i1&50;
String j = (i2>9)?(char)(i2-10+'A')+"":i2+"";
int temp = i1>>>4
i2 = temp&15;
String k = (i2>9)?(char)(i2-10+'A')+"":i2+"";
System.out.println(k+""""+j);

Ternary operator :( conditional expressions)? Expression 1: Expression 2
// Get the value of the larger of two integers
1 Description:
Results ① condition of a boolean expression
② The expression of true or false, decides to perform the expression 1, expression 2, or
if the expression the formula is true, the expression 1 is executed
if the expression is false, the expression 2 is performed
2 ③ expression 1 and the expression is consistent
④ can be nested ternary operator
2
who can use the ternary operator place, can be rewritten as if-else
not vice versa
3. If the procedures can use the ternary operator, and may be used if-else structure, the ternary operator preference. The reason: simple, high efficiency,

Sequence Structure: top down program execution
branch structure: if-else if -else switch- case
cycle structure: for while do-while

Released five original articles · won praise 0 · Views 33

Guess you like

Origin blog.csdn.net/weixin_44722917/article/details/105208015