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

Analysis of application examples of various operators in Java

王林
王林forward
2023-05-04 21:43:051650browse

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)则进行运算。
	}
}
-

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。
	}
}
*

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
	}
}
/

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
	}
}
%

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
	}
}

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
	}
}

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
	}
}

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);
	}
}

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: >, 95ec6993dc754240360e28e0de8de30a=, e06f7d622684246b64e9cf2b688746ae> (right shift operation), 42cf1999c55746a685a9bd6c675a2316>> (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);
	}
}

>> 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
	}
}

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!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete