📄 j-scjp-6-1.html
字号:
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜单" border="0" src="../i/main.gif" name="topmain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index6.html"><img alt="章节菜单" border="0" src="../i/section.gif" name="topsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-scjp-11-3.html"><img alt="给出此教程的反馈意见" border="0" src="../i/feedback.gif" name="topfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><img border="0" src="../i/xprevious.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-scjp-6-2.html"><img alt="下页" border="0" src="../i/next.gif" name="topnext"></a></TD>
</TR>
</TABLE>
<table bgcolor="ffffff" cellspacing="0" cellpadding="2" border="0" height="400" width="100%">
<tr valign="bottom">
<a name="navskip"></a><td height="25" colspan="4"><img alt="6.操作符和赋值 " src="imagemaster/titlebar6.jpg" border="0" height="25" width="562"></td>
</tr>
<tr>
<td bgcolor="ffffff" width="15"> </td><td bgcolor="ffffff" width="12"> </td><td valign="top" align="left" bgcolor="ffffff" width="*">
<p>
<br>
<table cellspacing="0" cellpadding="0" border="0">
<tr>
<td width="90%"><font size="4" face="Verdana, Arial, Helvetica"><b>使用操作符 </b></font></td><td width="200" align="right"><font size="1" face="Verdana, Arial, Helvetica"><nobr> 第 1 页(共5 页)</nobr></font></td>
</tr>
</table>
<br>
<br>
</p>
<font size="2" face="Verdana, Arial, Helvetica">
<p>
<b>赋值操作符 </b>
<br>
可以用常量或者表达式的结果来给基本变量赋值。使用了整数 (<code>int</code>、 <code>short</code> 和 <code>byte</code>) 的表达式的结果至少为类型 <code>int</code>。不允许窄化转换,因为它们会导致精度损失。例如: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
byte b = 5;
byte c = 4;
byte d = b + c; // does not compile because int cannot fit in a byte
float f = (float)35.67; // implicitly double, so casting to float
float f = 35.67F;
</code>
</pre>
<p>只能给布尔变量赋布尔类型的值。 </p>
<p>如果我们将现有对象引用赋值给一个引用变量,那么两个引用变量就指向同一个对象。将子对象赋值给父对象是合法的,但是返之则不允许。如果一个对象实现了一个接口,那么您也可以将这个对象引用赋值给这个接口引用变量。例如: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
class Base{}
public class Child extends Base
{
public static void main(String argv[])
{
Child child = new Child();
Base base = new Base();
base = child; // Compiles fine
child = base; // Will not compile
}
}
</code>
</pre>
<p>
<b>
<code>instanceof</code> 操作符 </b>
<br>
<code>instanceof</code> 操作符用于检查一个对象引用是否属于某种类型。这个类型可以是实例或者超类。例如: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
class A{}
class B extends A
{
public static void main(String args[])
{
B b = new B();
if(b instanceof A)
System.out.println("b is of type A"); // "b is of type A" is printed
}
}
</code>
</pre>
<p>注意,如果一个对象的任何超类实现了特定的接口,那么这个对象就是这种接口类型。 </p>
<p>
<b>加号 <code>(+)</code> 操作符 </b>
<br>
加号 <code>(+)</code> 操作符通常用于将两个数相加。如果操作数是 <code>String</code> 对象,那么加号 <code>(+)</code> 操作符就被覆盖以提供连接。例如: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
String s1 = "hello ";
String s2 = "World ";
System.out.println(s1 + s2); // prints "hello world"
</code>
</pre>
<p>如果一个操作数是 <code>String</code> ,而另一个不是,那么 Java 代码就会试图将另一个操作数转换为 <code>String</code> 表示。如果操作数是一个对象引用,那么就调用其 <code>toString()</code> 方法进行转换: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
int i = 10;
String s1 = " rivers";
String s2 = i + s1;
System.out.printlns(s2); // prints "10 rivers"
</code>
</pre>
<p>
<b>递增和递减操作符 </b>
<br>
两个快捷的算术操作符是将操作数加 1 的 <code>++</code> 和将操作数减 <code>--</code>。 </p>
<ul>
<li>
<code>++</code> 递增操作符 (值加 1) </li>
<li>
<code>--</code> 递减操作符 (值减 1) </li>
</ul>
<p>
这些操作符可以出现在操作数之前(前缀),也可以出现在操作数之后(后缀)。在前缀的情况下,在递增/递减操作之后对操作数的值进行计算。对于后缀的情况,在递增/递减操作之前对操作数的值进行计算,如下所示:</p>
<p>前缀(操作符放在操作数之前): </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
int i = 10;
int j = ++i; // j and i are 11
System.out.println(--i); // prints 10
</code>
</pre>
<p>后缀(操作符放在操作数之后): </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
int i = 10;
int j = i++; // j is 10, i becomes 11
System.out.println(i--); // prints 11
</code>
</pre>
<p>
<b>移位操作符 </b>
<br>
移位操作符向右或者向左移动数字的位,从而产生新的数字: </p>
<ul>
<li>
<code>>></code> 右移(用原来符号位的内容填充左面的位) </li>
<li>
<code><<</code> 左移(用零填充右边的位) </li>
<li>
<code>>>></code> 无符号右移(用零填充左边的位) </li>
</ul>
<p>例如: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
8 >> 1;
Before shifting: 0000 0000 0000 0000 0000 0000 0000 1000
After shifting: 0000 0000 0000 0000 0000 0000 0000 0100
</code>
</pre>
<p>注意,移动后的位模式相当于十进制的 4。 </p>
<p>
<b>位操作符 </b>
<br>
Java 提供了对其操作数进行位操作的四种操作符: </p>
<ul>
<li>如果两个操作数的位都是 1, <code>&</code> 操作符设置该位为 1。 </li>
<li>如果只有一个操作数的位是 1,则 <code>^</code> 操作符设置该位为 1。 </li>
<li>如果至少一个操作数的位为 1,则 <code>|</code> 操作符设置该位为 1。 </li>
<li>
<code>~</code> 操作符将操作数的每一位的值取反。 </li>
</ul>
<p>例如: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
class Test
{
public static void main(String args[])
{
int x = 10 & 9; // 1010 and 1001
System.out.println(x);
} // prints 8, which is the decimal equivalent of 1000
}
</code>
</pre>
<p>位操作符 <code>&</code> 和 <code>|</code> 也可以用于布尔表达式。 </p>
<p>
<b>逻辑操作符 </b>
<br>
四种逻辑操作符是 <code>&</code>、 <code>&&</code>、 <code>|</code> 和 <code>||</code>。</p>
<p>如果两个操作数都为 true,那么 <code>&</code> 和 <code>&&</code> 操作符返回 <code>true</code>。 </p>
<p>如果至少有一个操作数为 true,那么 <code>|</code> 和 <code>||</code> 操作符返回 <code>true</code>。 </p>
<p>
<code>&</code> 和 <code>|</code> 操作符总是判断两个操作数。 <code>&&</code> 和 <code>||</code> 称为 <i>short circuit</i> 操作符,因为如果左边的操作数就可以确定布尔表达式的结果,那么它们就不对右边的操作数进行判断了。例如: </p>
<pre>
<code style="font-family: Courier New, Courier, monospace; font-size: 12">
if ((++x > 2) || (++y > 2)) { x++; }
// y is incremented only if (++x > 2) is false
</code>
</pre>
<p>注意 <code>||</code> 和 <code>&&</code> 只能用于逻辑表达式。 </p>
<br>
</font></td>
</tr>
</table>
<TABLE border="0" cellpadding="0" cellspacing="0" width="100%">
<TR>
<TD background="../i/sw-gold.gif"><a border="0" href="index.html" onMouseOver="iOver('topmain'); iOver('bottommain'); self.status=mainblurb; return true;" onMouseOut="iOut('topmain'); iOut('bottommain'); self.status=''; return true;"><img alt="主菜单" border="0" src="../i/main.gif" name="bottommain"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topsection'); iOver('bottomsection'); self.status=sectionblurb; return true;" onMouseOut="iOut('topsection'); iOut('bottomsection'); self.status=''; return true;" href="index6.html"><img alt="章节菜单" border="0" src="../i/section.gif" name="bottomsection"></a></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topfeedback'); iOver('bottomfeedback'); self.status=feedbackblurb; return true;" onMouseOut="iOut('topfeedback'); iOut('bottomfeedback'); self.status=''; return true;" href="j-scjp-11-3.html"><img alt="给出此教程的反馈意见" border="0" src="../i/feedback.gif" name="bottomfeedback"></a></TD><TD width="100%" background="../i/sw-gold.gif"><img src="../i/c.gif"></TD><TD background="../i/sw-gold.gif"><img border="0" src="../i/xprevious.gif"></TD><TD background="../i/sw-gold.gif"><a border="0" onMouseOver="iOver('topnext'); iOver('bottomnext'); self.status=nextblurb; return true;" onMouseOut="iOut('topnext'); iOut('bottomnext'); self.status=''; return true;" href="j-scjp-6-2.html"><img alt="下页" border="0" src="../i/next.gif" name="bottomnext"></a></TD>
</TR>
<TR>
<TD width="150" height="1" bgcolor="#000000" colspan="6"><IMG alt="" height="1" width="150" src="../i/c.gif"></TD>
</TR>
</TABLE>
<TABLE width="100%" cellpadding="0" cellspacing="0" border="0">
<TR>
<TD width="100%">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td><img alt="" height="1" width="1" src="../i/c.gif"></td>
</tr>
<tr valign="top">
<td class="bbg" height="21"> <a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/index.shtml">关于 IBM</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/privacy/index.shtml">隐私条约</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/legal/index.shtml">法律条款</a><span class="divider"> | </span><a class="mainlink" href="/developerWorks/cgi-bin/click.cgi?url=http://www-900.ibm.com/cn/ibm/contact/index.shtml">联系 IBM</a></td>
</tr>
</table>
</TD>
</TR>
</TABLE>
<script src="//www.ibm.com/common/stats/stats.js" language="JavaScript1.2" type="text/javascript"></script>
<noscript>
<img border="0" alt="" height="1" width="1" src="//stats.www.ibm.com/rc/images/uc.GIF?R=noscript"></noscript>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -