📄 chap03.htm
字号:
<font color=#0000ff>int</font> j = rand.nextInt() % 100;
prt(<font color=#004488>"i = "</font> + i);
prt(<font color=#004488>"j = "</font> + j);
prt(<font color=#004488>"i > j is "</font> + (i > j));
prt(<font color=#004488>"i < j is "</font> + (i < j));
prt(<font color=#004488>"i >= j is "</font> + (i >= j));
prt(<font color=#004488>"i <= j is "</font> + (i <= j));
prt(<font color=#004488>"i == j is "</font> + (i == j));
prt(<font color=#004488>"i != j is "</font> + (i != j));
<font color=#009900>// Treating an int as a boolean is </font>
<font color=#009900>// not legal Java</font>
<font color=#009900>//! prt("i && j is " + (i && j));</font>
<font color=#009900>//! prt("i || j is " + (i || j));</font>
<font color=#009900>//! prt("!i is " + !i);</font>
prt(<font color=#004488>"(i < 10) && (j < 10) is "</font>
+ ((i < 10) && (j < 10)) );
prt(<font color=#004488>"(i < 10) || (j < 10) is "</font>
+ ((i < 10) || (j < 10)) );
}
<font color=#0000ff>static</font> <font color=#0000ff>void</font> prt(String s) {
System.out.println(s);
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can apply AND, OR, or NOT to
<B>boolean</B> values only. You can’t use a non-<B>boolean</B> as if it
were a <A NAME="Index263"></A><B>boolean</B> in a logical expression as you can
in C and C++. You can see the failed attempts at doing this commented out with a
<B>//!</B> comment marker. The subsequent expressions, however, produce
<B>boolean</B> values using relational comparisons, then use logical operations
on the results.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I40'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I41>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">One output listing looked like
this:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>i = 85
j = 4
i > j is <font color=#0000ff>true</font>
i < j is <font color=#0000ff>false</font>
i >= j is <font color=#0000ff>true</font>
i <= j is <font color=#0000ff>false</font>
i == j is <font color=#0000ff>false</font>
i != j is <font color=#0000ff>true</font>
(i < 10) && (j < 10) is <font color=#0000ff>false</font>
(i < 10) || (j < 10) is <font color=#0000ff>true</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Note that a <B>boolean</B> value is
automatically converted to an appropriate text form if it’s used where a
<B>String</B> is expected.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I41'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I42>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You can replace the definition for
<B>int</B> in the above program with any other primitive data type except
<B>boolean</B>. Be aware, however, that the comparison of floating-point numbers
is very strict. A number that is the tiniest fraction different from another
number is still “not equal.” A number that is the tiniest bit above
zero is still nonzero.<A NAME="Index264"></A>
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I42'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I43>
</FONT><BR></P></DIV>
<A NAME="Heading136"></A><FONT FACE = "Verdana"><H4 ALIGN="LEFT">
Short-circuiting</H4></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">When dealing with
<A NAME="Index265"></A><A NAME="Index266"></A><A NAME="Index267"></A>logical
operators you run into a phenomenon called “short circuiting.” This
means that the expression will be evaluated only <I>until</I> the truth or
falsehood of the entire expression can be unambiguously determined. As a result,
all the parts of a logical expression might not be evaluated. Here’s an
example that demonstrates short-circuiting:</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#009900>//: c03:ShortCircuit.java</font>
<font color=#009900>// Demonstrates short-circuiting behavior.</font>
<font color=#009900>// with logical operators.</font>
<font color=#0000ff>public</font> <font color=#0000ff>class</font> ShortCircuit {
<font color=#0000ff>static</font> <font color=#0000ff>boolean</font> test1(<font color=#0000ff>int</font> val) {
System.out.println(<font color=#004488>"test1("</font> + val + <font color=#004488>")"</font>);
System.out.println(<font color=#004488>"result: "</font> + (val < 1));
<font color=#0000ff>return</font> val < 1;
}
<font color=#0000ff>static</font> <font color=#0000ff>boolean</font> test2(<font color=#0000ff>int</font> val) {
System.out.println(<font color=#004488>"test2("</font> + val + <font color=#004488>")"</font>);
System.out.println(<font color=#004488>"result: "</font> + (val < 2));
<font color=#0000ff>return</font> val < 2;
}
<font color=#0000ff>static</font> <font color=#0000ff>boolean</font> test3(<font color=#0000ff>int</font> val) {
System.out.println(<font color=#004488>"test3("</font> + val + <font color=#004488>")"</font>);
System.out.println(<font color=#004488>"result: "</font> + (val < 3));
<font color=#0000ff>return</font> val < 3;
}
<font color=#0000ff>public</font> <font color=#0000ff>static</font> <font color=#0000ff>void</font> main(String[] args) {
<font color=#0000ff>if</font>(test1(0) && test2(2) && test3(2))
System.out.println(<font color=#004488>"expression is true"</font>);
<font color=#0000ff>else</font>
System.out.println(<font color=#004488>"expression is false"</font>);
}
} <font color=#009900>///:~</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Each test performs a comparison against
the argument and returns true or false. It also prints information to show you
that it’s being called. The tests are used in the expression:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I43'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I44>
</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE><font color=#0000ff>if</font>(test1(0) && test2(2) && test3(2))</PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">You might naturally think that all three
tests would be executed, but the output shows otherwise:
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I44'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I45>
</FONT><BR></P></DIV>
<BLOCKQUOTE><FONT SIZE = "+1"><PRE>test1(0)
result: <font color=#0000ff>true</font>
test2(2)
result: <font color=#0000ff>false</font>
expression is <font color=#0000ff>false</font></PRE></FONT></BLOCKQUOTE>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The first test produced a <B>true</B>
result, so the expression evaluation continues. However, the second test
produced a <B>false</B> result. Since this means that the whole expression must
be <B>false</B>, why continue evaluating the rest of the expression? It could be
expensive. The reason for short-circuiting, in fact, is precisely that; you can
get a potential performance increase if all the parts of a logical expression do
not need to be evaluated.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I45'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I46>
</FONT><A NAME="_Toc375545254"></A><A NAME="_Toc481064548"></A><BR></P></DIV>
<A NAME="Heading137"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Bitwise operators<A NAME="Index268"></A><A NAME="Index269"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The bitwise operators allow you to
manipulate individual bits in an integral primitive data type. Bitwise operators
perform boolean algebra<A NAME="Index270"></A> on the corresponding bits in the
two arguments to produce the result.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I46'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I47>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The bitwise operators come from C’s
low-level orientation; you were often manipulating hardware directly and had to
set the bits in hardware registers. Java was originally designed to be embedded
in TV <A NAME="Index271"></A>set-top boxes, so this low-level orientation still
made sense. However, you probably won’t use the bitwise operators much.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I47'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I48>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The bitwise AND operator
(<B>&</B>)<A NAME="Index272"></A><A NAME="Index273"></A> produces a one in
the output bit if both input bits are one; otherwise it produces a zero. The
bitwise OR operator (<B>|</B>)<A NAME="Index274"></A><A NAME="Index275"></A>
produces a one in the output bit if either input bit is a one and produces a
zero only if both input bits are zero. The bitwise EXCLUSIVE OR, or XOR
(<B>^</B>),<A NAME="Index276"></A><A NAME="Index277"></A><A NAME="Index278"></A>
produces a one in the output bit if one or the other input bit is a one, but not
both. The bitwise NOT<A NAME="Index279"></A> (<B>~</B>, also called the <I>ones
complement </I>operator<A NAME="Index280"></A><A NAME="Index281"></A>) is a
unary operator;<A NAME="Index282"></A><A NAME="Index283"></A> it takes only one
argument. (All other bitwise operators are binary
operators.<A NAME="Index284"></A><A NAME="Index285"></A>) Bitwise NOT produces
the opposite of the input bit—a one if the input bit is zero, a zero if
the input bit is one.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I48'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I49>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The bitwise operators and logical
operators use the same characters, so it is helpful to have a mnemonic device to
help you remember the meanings: since bits are “small,” there is
only one character in the bitwise operators.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I49'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I50>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">Bitwise operators can be combined with
the <B>=</B> sign to unite the operation and assignment:
<B>&=<A NAME="Index286"></A></B>, <B>|=<A NAME="Index287"></A></B> and
<B>^=<A NAME="Index288"></A></B> are all legitimate. (Since <B>~</B> is a unary
operator it cannot be combined with the <B>=</B> sign.)
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I50'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I51>
</FONT><BR></P></DIV>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The <B>boolean</B> type is treated as a
one-bit value so it is somewhat different. You can perform a bitwise AND, OR and
XOR, but you can’t perform a bitwise NOT (presumably to prevent confusion
with the logical NOT). For <B>boolean</B>s the bitwise operators have the same
effect as the logical operators except that they do not short circuit. Also,
bitwise operations on <B>boolean</B>s include an XOR logical operator that is
not included under the list of “logical” operators. You’re
prevented from using <B>boolean</B>s in shift expressions, which is described
next.
</backtalk:display>
[ <a href='http://www.mindview.net/backtalk/CommentServlet?ID=TIJ3_CHAPTER3_I51'
target="_blank">Add Comment</a> ]
<backtalk:display ID=TIJ3_CHAPTER3_I52>
</FONT><A NAME="_Toc375545255"></A><A NAME="_Toc481064549"></A><BR></P></DIV>
<A NAME="Heading138"></A><FONT FACE = "Verdana"><H3 ALIGN="LEFT">
Shift operators<A NAME="Index289"></A><A NAME="Index290"></A></H3></FONT>
<DIV ALIGN="LEFT"><P><FONT FACE="Georgia">The shift operators also manipulate bits.
They can be used solely with primitive, integral types. The left-shift operator
(<B><<</B>)<A NAME="Index291"></A><A NAME="Index292"></A> produces the
operand to the left of the operator shifted to the left by the number of bits
specified after the operator (inserting zeroes at the lower-order bits). The
signed right-shift operator
(<B>>></B>)<A NAME="Index293"></A><A NAME="Index294"></A> produces the
operand to the left of the operator shifted to the right by the number of bits
specified after the operator. The signed right shift <B>>> </B>uses
<I>sign extension<A NAME="Index295"></A><A NAME="Index296"></A></I>: if the
value is positive, zeroes are inserted at the higher-order bits; if the value is
negative, ones are inserted at the higher-order bits. Java has also added the
unsigned right shift <B>>>>, </B>which<B> </B>uses <I>zero
extension<A NAME="Index297"></A><A NAME="Index298"></A></I>: regardless of the
sign, zeroes are inserted at the higher-order bits.<I> </I>This operator does
not exist in C or C++.<I>
</backtalk:display>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -