ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
## Java编程那些事儿24—赋值运算符 郑州游戏学院 陈跃峰 出自:[http://blog.csdn.net/mailbomb](http://blog.csdn.net/mailbomb) **4.4 赋值运算符** 赋值运算符是指为变量或常量指定数值的符号。最基本的赋值运算符是“=”。 由于Java语言是强类型的语言,所以赋值时要求类型必须匹配,如果类型不匹配时需要能自动转换为对应的类型,否则将报语法错误。示例代码: ~~~                    byte b = 12;      //类型匹配,直接赋值                    int n = 10;       //类型匹配,直接赋值                    double d = 100; //类型不匹配,系统首先自动将100转换成100.0,然后赋值                    char c = -100;   //类型不匹配,无法自动转换,语法错误 ~~~ 需要强调的是,只能为变量和常量赋值,不能为运算式赋值,例如: ~~~                    int a = 10;                    int b = 2;                    a + b = 100; //不能为运算式a + b赋值,语法错误 ~~~ 常量只能赋值一次,否则也将出现语法错误,例如: ~~~                    final int N = 10;                    N = 20; //常量只能赋值一次,语法错误 ~~~ 在基本的赋值运算符基础上,可以组合算术运算符,以及后续将学习的位运算符,从而组成复合赋值运算符。赋值运算符和算术运算符组成的复合赋值运算符如下表4-4所示。 表4-4 复合赋值运算符 <table style="BORDER-RIGHT: medium none; BORDER-TOP: medium none; MARGIN-LEFT: -3.95pt; BORDER-LEFT: medium none; BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse" cellspacing="0" cellpadding="0" border="1"><tbody><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 33.75pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="45"> <div align="center">符号</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 55.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="74"> <div align="center">名称</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: black 1pt solid; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 215.2pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="287"> <div align="center">功能说明</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 33.75pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="45"> <div align="center">+=</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 55.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="74"> <div align="center">加等</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 215.2pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="287"> <div align="center">把变量加上右侧的值然后再赋值给自身</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 33.75pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="45"> <div align="center">-=</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 55.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="74"> <div align="center">减等</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 215.2pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="287"> <div align="center">把变量减去右侧的值然后再赋值给自身</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 33.75pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="45"> <div align="center">*=</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 55.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="74"> <div align="center">乘等</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 215.2pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="287"> <div align="center">把变量乘以右侧的值然后再赋值给自身</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 33.75pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="45"> <div align="center">/=</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 55.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="74"> <div align="center">除等</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 215.2pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="287"> <div align="center">把变量除以右侧的值然后再赋值给自身</div> </td> </tr><tr><td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: black 1pt solid; WIDTH: 33.75pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="45"> <div align="center">%=</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 55.25pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="74"> <div align="center">取余等</div> </td> <td style="BORDER-RIGHT: black 1pt solid; PADDING-RIGHT: 5.4pt; BORDER-TOP: medium none; PADDING-LEFT: 5.4pt; PADDING-BOTTOM: 0cm; BORDER-LEFT: medium none; WIDTH: 215.2pt; PADDING-TOP: 0cm; BORDER-BOTTOM: black 1pt solid" valign="top" width="287"> <div align="center">把变量和右侧的值取余然后再赋值给自身</div> </td> </tr></tbody></table> 实际使用示例: int n = 2; n += 3; 说明:计算以前n的值是2,也就是把n + 3的值,也就是5再赋值给n,经过运算以后n的值为5,因为该代码只执行一次,所以不会导致循环。 依次类推,其它的复合赋值运算符也是这样: ~~~                    int n = 10;                    n -= 2; //则n的值是8                    n *= 3; //因为n的初值是8,则n运算后的结果是24                    n /= 5; //因为n的初值是24,则n运算后的值是4 ~~~ 注意:复合赋值运算不会改变结果的类型,所以在有些时候运算在逻辑上会出现错误,但是符合计算中数值的表述。例如: ~~~                    byte b = 127;                    b += 1;                    System.out.println(b); ~~~ 根据前面的介绍,byte类型的取值区间是-128~127,由于复合赋值运算符不改变结果的类型,则导致结果是-128,而不是128。原因如下: 1. byte类型值127的机器数是01111111,0表示正数,后续的数值表示127 2. 该数值加1后,得到的数值是10000000,二进制加法 3. 而10000000在byte类型中恰好是-128的机器数表示形式 其它类型的符合运算符也可能存在类似的情况,使用时需要注意。