Home > Java > javaTutorial > Analysis of application examples of various operators in Java

Analysis of application examples of various operators in Java

王林
Release: 2023-05-04 21:43:05
forward
1671 people have browsed it

1. Arithmetic operators

1. Introduction

In Java, the arithmetic operators , -, *, /, and % represent addition, subtraction, multiplication, division, and modulo respectively.

2. Application

There are three types in java: 1. Normal operation. 2. Use as positive and negative. 3. As a connector (when data of any data type is connected to a string, that number is the connector)

Example:

package com;
public class liu {
	public static void main(String[] args) {
		//实例一:作为正常运算
		int c = 1;
		int d = 2;
		int e = c + d;
		System.out.println(e)  //最后结果为3;
		//实例二:作为连接符
		//已知有两个变量a和b,值分别为1和2,在控制台上输出a + b = 3, 1 + 2 = 3
		int a = 1;
		int b = 2;
		System.out.println(a + " + " + b + " = " + (a + b));  
		//控制台显示结果为:a + b = 3。
		//注意此时的"+","="作为字符串,而(a + b)则进行运算。
	}
}
Copy after login
-

in java There are two types: 1. Normal operation. 2. Use as positive and negative.

Example:

package com;
public class liu {
	public static void main(String[] args) {
		int a = 1;
		int b = 3;
		int c = b-a;
		System.out.println(c);  //运算结果:2。
	}
}
Copy after login
*

Example:

package com;
public class liu {
	public static void main(String[] args) {
		int i = 4;
		int j = 2;
		int a = i*j;
		System.out.println(a);//8
	}
}
Copy after login
/

When the two numbers involved in/operating are both integers , the result is an integer, otherwise it is a floating point number.

Example:

package com;
public class liu {
	public static void main(String[] args) {
        //实例1:作为整数运算
		int i = 4;
		int j = 2;
		int a = i / j;
		System.out.println(a);//2
     	//实例2:作为浮点运算
		int i = 5;
		double j = 2;
		double a = i / j;
		System.out.println(a);//2.5
	}
}
Copy after login
%

The remainder operation of an integer (sometimes called modulo) is represented by %.

package com;
public class liu {
	public static void main(String[] args) {
		int i = 2;
		int j = 3;
		System.out.println(i % j);  //0
		int a = 4;
		int b = 2;
		System.out.println(a % b); //2
	}
}
Copy after login

2. Auto-increment and auto-decrement operators

Used alone: ​​No matter before or after, the result is the same. See Example 1 for details.

Participate in the operation: First: first add 1 to itself, and then participate in the operation, see Example 2 for details; Behind: first participate in the operation, and then add 1 to itself, see Example 3 for details.

Example:

package com;
public class liu {
	public static void main(String[] args) {
		//实例1:单独运算。
		int i = 1;
		i++; // i = i + 1
		++i;
		System.out.println(i);//结果都等于2。
		//实例2:++在前。
		int i = 1;
		int j = ++i;
		System.out.println(j);//2
		System.out.println(i);//2	
		//实例3:++在后。
		int i = 1;
		int j = i++;
		System.out.println(j);//1
		System.out.println(i);//2
	}
}
Copy after login

Used alone: ​​No matter whether – is in front or behind, the result is the same. See Example 1 for details.

Participate in the operation: – In the front: first decrement itself by 1, and then participate in the operation, see Example 2 for details; – In the back: first participate in the operation, and then decrement itself by 1, see Example 3 for details.

package com;
public class liu {
	public static void main(String[] args) {
		//实例1:单独运算。
		int i = 2;
		i--; // i = i - 1
		--i;
		System.out.println(i);//结果都等于1。
		//实例2:--在前。
		int i = 1;
		int j = --i;
		System.out.println(j);//1
		System.out.println(i);//1	
		//实例3:--在后。
		int i = 1;
		int j = i--;
		System.out.println(j);//2
		System.out.println(i);//1
	}
}
Copy after login

3. Assignment operator

You can use binary operators in assignment, and the form is very simple: x = 4 is equivalent to x = x 4.

Common assignment operators are: =, =, -=, *=, /=, %=.

The following takes = as an example:

package com;
public class liu {
	public static void main(String[] args) {
		int i = 1;
		i += 2; // i = i + 2
		System.out.println(i); //输出结果3 
		byte b = 1;
		b += 2; // b = (byte)(b + 2)
		System.out.println(b);
	}
}
Copy after login

4. Relational operators

The result of the relational operator must be boolean type data, which means that it is either true , or false

Common relational operators: >, <, >=, <=, !=, ==.

Example:

package com;
public class liu {
	public static void main(String[] args) {
		int i = 3;
		int j = 2;
		System.out.println(i > j);//true
		System.out.println(i < j);//false
		System.out.println(i >= j);//true
		System.out.println(i <= j);//false
		System.out.println(i != j);//true
		System.out.println(i == j);//false  
		// ==是比较两边的大小,=是赋值。
	}
}
Copy after login

5. Logical operators

The data types of the data on the left and right sides are all boolean type, and the results are all boolean type.

&(Single AND)

As long as one of the two sides is false, the result will be false

Example:

package com;
public class Demo12 {
	public static void main(String[] args) {
		System.out.println(true & true); 
		System.out.println(true & false); //false
		System.out.println(false & false);
		System.out.println(false & true); //false
	}
}
Copy after login

| (Single Or)

As long as one of the two sides is true, the result is true.

Example:

package com;
public class Demo12 {
	public static void main(String[] args) {
		System.out.println(true | true);
		System.out.println(true | false);
		System.out.println(false | false); //false
		System.out.println(false | true);
	}
}
Copy after login

^(XOR)

If both sides are the same, it will be false, and if both sides are different, it will be true.

Example:

package com;
public class Demo12 {
	public static void main(String[] args) {
		System.out.println(true ^ true); //false
		System.out.println(true ^ false);
		System.out.println(false ^ false); //false
		System.out.println(false ^ true);
		
	}
}
Copy after login

! (Non)

is directly the opposite.

Example:

package com;
public class Demo12 {
	public static void main(String[] args) {
		System.out.println(!true); //false
		System.out.println(!false); //true
		System.out.println(!!true); //true
		System.out.println(!!!true); //false
	}
}
Copy after login

&&(Double AND)

As long as one of the two sides is false, the result will be false.

Example:

package com;
public class Demo12 {
	public static void main(String[] args) {
		System.out.println(true && true);
		System.out.println(true && false); //false
		System.out.println(false && false); //false
		System.out.println(false && true);  //false
	}
}
Copy after login

What is the difference between & and &&?

  • &&: If the left side is false, the right side will no longer be executed, and the result will be false.

  • &: If the left side is false, the right side will still be executed, and the result will be false.

|| (Double OR)

As long as one of the two sides is true, the result is true.

Example:

package com;
public class Demo12 {
	public static void main(String[] args) {
		System.out.println(true || true); //true
		System.out.println(true || false); //true
		System.out.println(false || false); //false
		System.out.println(false || true); //true
	}
}
Copy after login

What is the difference between | and ||?

  • ||: If the left side is true, the right side will no longer be executed, and the result will be true.

  • |: If the left side is true, the right side will still be executed, and the result will be true.

6. Bitwise operators

The data on both sides are numbers, and the result is also a number.

Common bitwise operators: & (AND bit operation), | (OR bit operation), ^ (XOR bit operation), ~ (bitwise negation), >> (right shift operation), << (left shift operation), >>> (unsigned right shift).

Example:

package com;
public class Demo01 {
	public static void main(String[] args) {
	    System.out.println(3 & 2); //2
		System.out.println(3 | 2); //3
		System.out.println(3 ^ 2); //1
		System.out.println(~3);//-4
		System.out.println(3 >> 2);//0
		System.out.println(3 << 2);//12
		System.out.println(3 >>> 2);//0
		System.out.println(-3 >> 2);//-1
		System.out.println(-3 >>> 2);
	}
}
Copy after login

>> What is the difference between >>>?

>>: If the data is a negative number, the leftmost sign bit is 1. After the right shift, 1 must be added to the left.

If the data is a positive number, the leftmost sign bit is 0. After the right shift, 0 must be added to the left.

>>>: Regardless of whether the data is positive or negative, after the right shift, 0 is added to the left.

7. Ternary operator

Format: Conditional expression? Expression 1 : Expression 2; Equivalent to x > y ? x : y

Conditional expression The result of the expression must be of type boolean

Execution process:

If the conditional expression is true, expression 1 will be executed, and expression 2 will not be executed

If the condition If the expression is false, expression 2 will be executed and expression 1 will not be executed

Example:

package com;
public class Demo02 {
	public static void main(String[] args) {
		//获取两个数的较大值
		int i = 2;
		int j = 3;
		//第一种:
		int max = i > j ? i : j;
		System.out.println(max);  //3
		//第二种:
		System.out.println(i > j ? i : j);
		//表达式1和表达式2既然会得到一个结果,如果传递给一个变量去接收,该变量的数据类型应该和表达式1和表达式2的结果的数据类型匹配。
		//浮点型:
		double d = 3 > 2 ? 1 : 2;
		System.out.println(d); //输出:1.0
		//字符型:
		char c1 = 3 > 2 ? 97:98;
		System.out.println(c1); //输出:a
	}
}
Copy after login

The above is the detailed content of Analysis of application examples of various operators in Java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template