---
title: "Idiot's Note Two: java中的复合运算符及其等价形式"
author: "danielhjd"
date: "2011-06-29T17:46:04Z"
tags: []
categories: ["编程语言"]
slug: "idiot-s-note-two-java"
draft: false
lastmod: "2011-06-29T17:46:04Z"
id: 1109628
url: "https://danielhjd.iteye.com/blog/1109628"
---

**复合赋值运算符：**

运算符 用法 等价形式 += op1+=op2 op1=op1+op2 -= op1-=op2 op1=op1-op2 /= op1/=op2 op1=op1/op2 %= op1%=op2 op1=op1%op2 \*= op1\*=op2 op1=op1\*op2

 在有些地方会出现：

op1=+op2 此处的运算符=+实际上是将op2进行一元运算。

 

下面的例子为：

```
public class BasicExample {

	public static void main(String[] args) {
		int total=0;
		for(int i=1;i<=3;i++){
			total=+i;
			System.out.println("Sum="+total);
		}
		char c='c';
		total=+c;
		System.out.println("Sum="+total);
	}

}
```

 

outprintln:

```
Sum=1
Sum=2
Sum=3
Sum=99
```

 

扩展：其他运算符；

1： ？： 运算符 op1?op2:op3 改运算符在op1为true的时候返回op2,否者返回op3;

2： 【】 运算符 float \[] arrayOfFloats =new float\[10]; or float arrayOfFloats\[]  =new float\[10];

3：  .      运算符 用来访问对象实例或访问类的成员变量或成员方法；

4： （）   运算符 用于放参数

5： （type） 运算符 将某类型的值或对象转换为type型

6： new

7：instanceof  op1 instanceof op2 op1为对象名 op2为类名