Learn JAVA together and learn JAVA operators

Posted by scvinodkumar on Tue, 21 Dec 2021 22:33:57 +0100

1 operator

1.1 general

Operator is used to concatenate operands of an expression and perform operations on operands.
For example, the expression num1+num2 has operands num1 and num2, and the operator is "+".
In the java language, operators can be divided into five types:
Arithmetic operator, assignment operator, relational operator, logical operator, bit operator.
According to different operands, operators are divided into monocular operators, binocular operators and ternary operators.
A unary operator has only one operand, a binocular operator has two operands, and a ternary operator has three operands.
Bit operator involves binary bit operation, which is not widely used in java programs.


1.2 operator quick look-up table

1.3 exercise: Test self increasing and self decreasing

Create package: CN tedu. basic
Create class: testoperator java

package cn.tedu.basic;
/**This class is used to test the remainder operator*/
public class TestOperator {
	//0. Create the entry function main of the program
	public static void main(String[] args) {
		//1. Test division and remainder
		System.out.println(25 / 10);//2
		System.out.println(25 % 10);//5
		
		//2. Exercise 1: get ten and one digits of a two digit 59
		int x = 59;
		System.out.println(x/10);//Print ten digits, 5
		System.out.println(x%10);//Print bits, 9
		
		//3. Exercise 2: get a three digit 159 hundred digit, ten digit and one digit
		int y = 159;
		System.out.println(y / 100);//Print hundreds
		System.out.println(y / 10 % 10);//Print ten digits
		System.out.println(y % 10);//Print bits
		
		//4. Test self increasing and self decreasing operators
		/** Prefix type: the symbol comes first: + a --a. change the value of the variable itself before using it, such as printing
		 * Suffix type: after the symbol: a++ a --, use it first, and then change the value of the variable itself
		 * ++:This is equivalent to giving the value of the variable itself + 1
		 * --: It is equivalent to giving the value of - 1 to the variable itself*/
		System.out.println("I am a ruthless dividing line");
		
		/**Ordinary four operations do not change the value of the variable itself
		 *    The self increasing and self decreasing operator will change the value of the variable itself*/
		int z = 1;
		System.out.println(z+4);//5
		System.out.println(z);//1
		
		int a = 1;
		System.out.println(++a);//2
		System.out.println(a);//2
		
		int b = 1;
		System.out.println(b++);//1
		System.out.println(b);//2
		
		int c = 1;
		System.out.println(--c);//0, before the symbol, subtract first and then print
		System.out.println(c);//0, the above has been reduced by itself
		
		int d = 1;
		System.out.println(d--);//1. After the symbol, print first and then subtract automatically
		System.out.println(d);//0. After printing, it is reduced to 0
		
		//The previous code will affect the later code, and the value of c is 0
		System.out.println(c);//0
		System.out.println(--c-c-c--);//1,-1-(-1)-(-1)=1
		System.out.println(c);//-2. It has experienced two self subtractions, so 0-2 = - 2
		
	}
}

1.4 exercise: testing logical operators

Create package: CN tedu. basic
Create class: testoperator2 java

package cn.tedu.basic;
/**This class is used to test logical operators*/
public class TestOperator2 {
	public static void main(String[] args) {
		/**And: Quanzhen is true*/
		System.out.println("Test sheet and:");
		System.out.println(true & true);
		System.out.println(true & false);
		System.out.println(false & true);
		System.out.println(false & false);
		System.out.println("Test double and:");
		System.out.println(true && true);
		System.out.println(true && false);
		System.out.println(false && true);
		System.out.println(false && false);
		/**Or: full leave*/
		System.out.println("Test sheet or:");
		System.out.println(true | true);
		System.out.println(true | false);
		System.out.println(false | true);
		System.out.println(false | false);
		System.out.println("Test dual or:");
		System.out.println(true || true);
		System.out.println(true || false);
		System.out.println(false || true);
		System.out.println(false || false);
	}
}

1.5 exercise: find the maximum value of two numbers

Create package: CN tedu. basic
Create class: testmaxnum java

package cn.tedu.basic;

import java.util.Scanner;

/**Requirement: receive two integers input by the user, and compare and output the maximum value of the two numbers*/
public class TestMaxNum {
	public static void main(String[] args) {
		//1. Prompt the user for input
		System.out.println("Please enter the first integer you want to compare:");
		//2. Receive the integer entered by the user and give the value to variable a to save
		int a = new Scanner(System.in).nextInt();
		System.out.println("Please enter the second integer you want to compare:");
		int b = new Scanner(System.in).nextInt();
		
		//3. Compare the received two numbers and use the ternary operator
		/**1 ? 2 : 3
		 * 1 Is an expression. If the result of 1 is true, the result takes position 2. If the result of 1 is false, the result takes position 3
		 * */
		//4. Define the variable max to save the maximum value of the comparison
		int max = a > b ? a : b;
		
		//5. Print maximum value
		System.out.println("The maximum of the two numbers is:"+max);	
		/**Thinking question: how to judge the maximum value of three numbers? int max = a>b? (a>c?a:c):(b>c?b:c);*/
	}
}

1.6 exercise: find normal and leap years

Create package: CN tedu. basic
Create class: testyear java

demand:Receive the year entered by the user,Judge whether it is a normal year or a leap year
package cn.tedu.basic;

import java.util.Scanner;

/**
 * Demand: receive the year entered by the user and judge whether it is a normal year or a leap year
 * If the year is a leap year, you need to meet one of the following two conditions:
 * Condition 1: can be divided by 4 and cannot be divided by 100
 * Condition 2: can be divided by 400
 * */
public class Test3_Year {
	public static void main(String[] args) {
		//1. Prompt and receive the year entered by the user
		System.out.println("Please enter the year you want to judge:");
		int year = new Scanner(System.in).nextInt();
		
		//2. Define a variable to save the result
		String result = "Ordinary year";//The default value is a normal year, assuming that every year is a normal year
		
		//3. Judge whether the year entered by the user meets the conditions of leap year
		/**Solution 1*/
		/**Condition 1: can be divided by 4 and cannot be divided by 100*/
//		If (year% 4 = = 0) {/ / can be divided by 4
//			If (year% 100! = 0) {/ / cannot be divided by 100
//				Result = "leap year"// If the condition 1 of leap year is met, the modification result is leap year
//			}
//		}
//		/**Condition 2: can be divided by 400*/
//		If (year% 400 = = 0) {/ / can be divided by 400
//			Result = "leap year"// If the condition 2 of leap year is met, the modification result is leap year
//		}
		/**Solution 2*/
		/**Judgment structure if (judgment condition) {code executed after the judgment condition is met} */
		//If (condition 1 | condition 2) {is leap year}
		//If ((minor condition 1 & & minor condition 2) | condition 2) {is a leap year}
		//If ((divisible by 4 & & not divisible by 100) | divisible by 400) {is a leap year}
		if((year % 4 == 0 && year %100 != 0) || year % 400 == 0){
			result = "leap year";//If it meets the conditions of leap year, the modification result is leap year
		}
		System.out.println(year+"Year is"+result);
	}
	
}

2. Expansion supplement:

2.1 summary 1: self increasing and self decreasing operators of arithmetic operators

A is the operand, + + is the self increasing operator, and - is the self decreasing operator. The self increasing and self decreasing operators can be placed in front of or behind the variable, such as a + +, + + A, a --, -- A, etc.
Auto increment (+ +): increase the value of the variable by 1
It is divided into prefix formula (such as + + a) and suffix formula (such as a + +). Prefix formula is to add 1 before use; suffix formula is to use first and then add 1.
Self subtraction (-): subtract the value of the variable by 1
It is divided into prefix formula (such as -- a) and suffix formula (such as a --). Prefix formula is used after subtracting 1; suffix formula is used before subtracting 1.


2.2 summary 2: logical operators

Logical operators connect two relational expressions or boolean variables to solve the combination judgment problem of multiple relational expressions
Note that the operation result returned by the logical operator is Boolean
Usually, we use 0 for false and 1 for true
Relationship with: express and
&Single and: 1 & 2. If the result wants to be true, both 1 and 2 must be true
&&Double and (short circuit and): 1 & & 2. When 1 is false, 2 will be short circuited to improve the efficiency of the program
Or: a relationship that represents or
|Single or: 1 | 2. If the result wants to be true, only one of 1 and 2 is required to be true
||Double or (short circuit or): 1 | 2. When 1 is true, 2 will be short circuited to improve program efficiency
 

2.3 summary 3: priority control

When an expression contains multiple operators, the priority of the operator needs to be considered. Operators with high priority participate in the operation first, and operators with low priority participate in the operation later. In the actual development, there is no need to memorize the priority of operators, and do not deliberately use the priority of operators. For places where the priority is not clear, use parentheses to assist in priority management.

Topics: Java Back-end