Java Arithmetic Operator, Assignment Operator, Comparison Operator, Logical Operator, Bit Operator, Trinomial Operator

Posted by ark014 on Wed, 19 Jan 2022 18:59:21 +0100

operator

operation

Operations on constants and variables are called operations

operator

Symbols that operate on constants and variables are called operators

Operand

The data that participates in the operation is called an operand

Arithmetic Operators

+several functions:

Additive Positive Number String Connector

One thing to note when dividing is that if you divide an integer, you get only an integer. If you want to get a decimal, you can *1.0

/and% difference

/Get the operator,%Get the remainder of the division operation

Application of++ and--

Same effect when used alone

Participate in operations with different effects before and after operands

Examples of operations

/*
int a = 10;
int b = 10;
int c = 10;
a = b++;
c= --a;
b= ++a;
a =c--;
Please calculate a,b,c values separately
2:More complex topics
int x= 4 ;
int y =(x++)+(10+x)+(x*10);
*/
class OperatorTest {
public static void main (String [] args) {
//Three variables a,b,c
int a = 10;
int b = 10;
int c = 10;
a = b++;           //a = 10,b = 11,c = 10;
c = --a ;          // a= 9,b=11 ,c = 9
b=++a ;            //a=10,b = 10,c=9;
a = c--;           //a = 9 , b=10,c = 8
System.out.println ( "a : "+a+" ,b : "+b+",c : "+c);
System.out.println ( "------------")
int x=4 ;
// x. =4,5
//        4  +  6  +   60
int y =(x+ten)+(+ten X)+(x*10) ;
System.out.println ("y : "+y);

Assignment Operator

=, +=, -=, *=, /=,%=== is the basic assignment operator, the other is the extended assignment operator

1:=Assignment Number

2:+=Assignment

Assign left and right results to left.

Note: The left side cannot be a constant

3: Features of the +=operator derived from the interview questions:

There is an implicit default conversion function.

Equivalent to:

short s = 1;

s+=1 is equivalent to

s=(s data type) (s+1);

Relational Operators

Logical Operators

Differences between'&'and'&'

Single-time, the left side is true or false, and the right side is operated on.

If the left side is true, the right side participates in the operation, and if the left side is false, the right side does not participate in the operation.

The difference between'|'and'|' is the same. When double or, the left side is true and the right side does not participate in the operation

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

&: false with false

|: true if true

^: same false, different true

Help understand: couples must be men and women or men

!: Non-false is true, non-true is false

Differences between &&, || and & and |

The results are the same, but with short-circuit effect, it is generally recommended to use &&, ||

int x = 3;int y =4;
System.out.print1n((x++>4)&(y++>5));
System.out.print1n(x);
System.out.println(y);
System.out.print1n((x++>4)&&(y++>5));
System.out.print1n(x);
System.out.print1n(y);

Bitwise Operators

Bit Operator Details

1) 3-bit and &4 (bit and &)

Calculated separately: 3 and 4 corresponding to the original code (binary)

2) 3 or 4 (bit or |)

3) 3-bit XOR 4 (XOR^)

4) Decode 3 (Decode~)

Trinomial Operator

format

(Relational expression)? Expression 1: Expression 2;

If the condition is true, the result of the operation is expression 1.

If the condition is false, the result of the operation is expression 2.

Example

Gets the large number of two numbers.

 int x=3,y=4,z;

Z = (x>y)? X:y;// The Z variable stores the large number of two numbers

Code Case

Compare Maximum

class OperatorDemo{
	public static void main(String[] args){
		//Define two variables
		int a = 10 ;
		int b = 15 ;
		
		//(expression)? 1) Results of true execution: 2) Results of false execution;
		//Define variable max
		int max = (a>b)? a:b;
		System.out.println("The maximum value of both data is:"+max) ;}}

Two methods compare the maximum of three numbers

//Define three variables x,y,z
		int x = 150 ;
		int y = 200 ;
		int z = 80 ;
		
		//Mode 1:
		//1) Define the intermediate variable temp: Record the maximum values of x and y: Use the ternary operator
		//2) Compare values using temp and z, and use ternary operators
		int temp = (x > y )? x: y ;
		int result = (temp > z)? temp : z ;
		System.out.println("The maximum value in the three data is:"+result) ;
		
		System.out.println("---------------------------------") ;
		//Mode 2: Step by step: Use ternary nesting (best to wrap it up with (), otherwise the code is not well written)
		//Define the final variable: max2 
		int max2 = (x>y)?((x>z)?x:z):((y>z)?y:z) ;
		System.out.println("The maximum value in the three data is:"+max2) ;

Compare whether two numbers are equal
 

//Define two variables to compare whether two data are equal
		int m = 50 ;
		int n = 40 ;
		//Compare equality: true/false: results are received using boolean type
		//boolean flag = (m==n)? true:false ;
		
		//Optimization: ==: itself is a comparison operator: returns true/false
		boolean flag = m==n;
		System.out.println(flag) ;

Keyboard entry data

We are currently writing programs with fixed data values, but in the actual development, data values are certainly changing, so I am ready to improve the data to keyboard entry, to increase the flexibility of the program

Used in the requirement to implement a data entry immediately

Steps to achieve:

1. Import package (place on top of class definition)

import java.util.Scanner

2. create object

 Scanner sc = new Scanner(System.in);

3. receive data

int x = sc.nextInt();

/*
	Previous programs defined variables as dead and assigned a value directly. So improve, use the keyboard to enter data, so that the program is more flexible!
	
	jdk Give us classes:
		java.util.Scanner :A simple text scanner
	Use steps
		1)Guide: In the Java language: as long as it is not java. Classes under the Lang package need to be imported!
		Location: above class  
				import java.util.Scanner;
				
		2)Fixed format: Create keyboard entry object (text scanner object)
				Scanner Object name = new Scanner(System.in);
				
		3)Start typing data: use int type for example
		 Scanner Functions of Classes
				public int nextInt():Enter the next int type
				
		  Friendly Tip "Please enter a data:"
			int Variable name = object name. nextInt();
			
		4)Using variables	
		Requirements:
			Known two variables a, B (keyboard input data) are initialized separately to find the maximum value of the two data!
*/
//1) Guide
import java.util.Scanner ;
class ScannerDemo{
	
	public static void main(String[] args){
		//2) Create keyboard entry object (text scanner object)
		Scanner scanner = new Scanner(System.in) ;
		
		//3) Prompt and enter data
		System.out.println("Please enter a data:") ;
		int x = scanner.nextInt() ;
		
		//4) Use this data
		System.out.println("The data you entered is:"+x) ;
		
		System.out.println("-------------------------------------") ;
		
		//Create keyboard entry object
		Scanner sc = new Scanner(System.in) ;
		
		//Prompt and enter data
		System.out.println("Please enter the first data:") ;
		int a = sc.nextInt() ;
		
		System.out.println("Please enter the second data:") ;
		int b = sc.nextInt() ;
		
		//Comparison of Ternary Operators
		int max = (a>b)? a: b ;
		System.out.println("Maximum is:"+max) ;
	}
}