📄 ch1.htm
字号:
For example, you should not call a variable <TT>pi+3.</TT><H3><A NAME="Comments">Comments</A></H3><P>Java has three comment styles. Two are similar to those used inC and C++. A double slash (<TT>//</TT>)means that everything to the end of the line should be ignored:<BLOCKQUOTE><TT>// Ignore this</TT></BLOCKQUOTE><P>Everything between the characters <TT>/*</TT>and <TT>*/</TT> is also ignored. Thiscan be spread over several lines. If the first part of the commentstarts with <TT>/**</TT>, then a specialdocumenting feature is indicated. How this works is discussedin <A HREF="ch14.htm" >Chapter 14</A>.<H3><A NAME="Operators">Operators</A></H3><P>Table 1.2 lists a quick summary of operators in Java, which arefairly simple to use. The following code declares two integers,assigns values to them, and adds them to a third variable:<BLOCKQUOTE><TT>int x,y;<BR>x = 3;<BR>y = 4;<BR>int z = x + y;</TT></BLOCKQUOTE><P>The final value is <TT>7</TT>.<P>The following code applies the bitwise <TT>AND</TT>operator to the end of the previous example to get the value <TT>4</TT>.<BLOCKQUOTE><TT>int q = z & 4;</TT></BLOCKQUOTE><P>You'll see examples of other categories of operators in the upcomingsections.<BR><P><CENTER><B>Table 1.2. Classification of operators.</B></CENTER><P><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%><TR VALIGN=TOP><TD WIDTH=300><I>Classification</I></TD><TD WIDTH=203><I>Operators</I></TD></TR><TR VALIGN=TOP><TD WIDTH=300>Arithmetic</TD><TD WIDTH=203>+ - * / %</TD></TR><TR VALIGN=TOP><TD WIDTH=300>Relational Operators</TD><TD WIDTH=203>< > >= <= == != && || !</TD></TR><TR VALIGN=TOP><TD WIDTH=300>Bitwise Operators</TD><TD WIDTH=203>& | ^ << >> >>> ~ &= |= ^=</TD></TR><TR VALIGN=TOP><TD WIDTH=300>Assignments</TD><TD WIDTH=203>= += -= /= %=</TD></TR><TR VALIGN=TOP><TD WIDTH=300>Bitwise Assignments</TD><TD WIDTH=203>&= |= <<= >>= >>>= ^=</TD></TR><TR VALIGN=TOP><TD WIDTH=300>Ternary Operator (<TT>if...else</TT> shorthand)</TD><TD WIDTH=203>?:</TD></TR><TR VALIGN=TOP><TD WIDTH=300>Increment</TD><TD WIDTH=203>++</TD></TR><TR VALIGN=TOP><TD WIDTH=300>Decrement</TD><TD WIDTH=203>--</TD></TR></TABLE></CENTER><P><P>Expressions that have multiple operators are resolved accordingto where they are in a hierarchy of precedences, shown in Table1.3. Operators higher up in the precedence table are evaluatedfirst. If multiple operators on the same line have the same precedence,then they are resolved by left-right order. If you are confusedor cannot remember precedence, then a nice rule is "Whenin doubt, use parentheses."<BR><P><CENTER><B>Table 1.3. Operator precedence.</B></CENTER><P><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=40%><TR VALIGN=TOP><TD WIDTH=176>. [] ()</TD></TR><TR VALIGN=TOP><TD WIDTH=176>++ - ! ~</TD></TR><TR VALIGN=TOP><TD WIDTH=176>* / %</TD></TR><TR VALIGN=TOP><TD WIDTH=176>+ -</TD></TR><TR VALIGN=TOP><TD WIDTH=176><< >> >>></TD></TR><TR VALIGN=TOP><TD WIDTH=176>< > <= >=</TD></TR><TR VALIGN=TOP><TD WIDTH=176>== !=</TD></TR><TR VALIGN=TOP><TD WIDTH=176>&<BR>^</TD></TR><TR VALIGN=TOP><TD WIDTH=176>|</TD></TR><TR VALIGN=TOP><TD WIDTH=176>&&</TD></TR><TR VALIGN=TOP><TD WIDTH=176>||</TD></TR><TR VALIGN=TOP><TD WIDTH=176>?:</TD></TR><TR VALIGN=TOP><TD WIDTH=176>= and other assignments</TD></TR><TR VALIGN=TOP><TD WIDTH=176>Bitwise Assignments</TD></TR></TABLE></CENTER><H3><A NAME="KeywordsandConditionals">Keywords and Conditionals</A></H3><P>Table 1.4 lists Java keywords; they are reserved for Java statements,so you can't use them for things like variable names. They areidentifiers for such things as data types, conditionals, controlflow constructs, class definitions, and object implementations.Most of these keywords will be described in this and the nextchapter. Those that are reserved but currently not implementedare italicized.<BR><P><CENTER><B>Table 1.4. Java keywords</B>.</CENTER><P><CENTER><TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=60%><TR VALIGN=TOP><TD WIDTH=80>Abstract</TD><TD WIDTH=106>boolean</TD></TR><TR VALIGN=TOP><TD WIDTH=80>break</TD><TD WIDTH=106>byte</TD></TR><TR VALIGN=TOP><TD WIDTH=80><I>byvalue</I></TD><TD WIDTH=106>case</TD></TR><TR VALIGN=TOP><TD WIDTH=80>catch</TD><TD WIDTH=106>char</TD></TR><TR VALIGN=TOP><TD WIDTH=80>class</TD><TD WIDTH=106><I>const</I></TD></TR><TR VALIGN=TOP><TD WIDTH=80>continue</TD><TD WIDTH=106>default</TD></TR><TR VALIGN=TOP><TD WIDTH=80>do</TD><TD WIDTH=106>double</TD></TR><TR VALIGN=TOP><TD WIDTH=80>else</TD><TD WIDTH=106>extends</TD></TR><TR VALIGN=TOP><TD WIDTH=80>false</TD><TD WIDTH=106>final</TD></TR><TR VALIGN=TOP><TD WIDTH=80>finally</TD><TD WIDTH=106>float</TD></TR><TR VALIGN=TOP><TD WIDTH=80>for</TD><TD WIDTH=106><I>goto</I></TD></TR><TR VALIGN=TOP><TD WIDTH=80>if</TD><TD WIDTH=106>implements</TD></TR><TR VALIGN=TOP><TD WIDTH=80>import</TD><TD WIDTH=106>instanceof</TD></TR><TR VALIGN=TOP><TD WIDTH=80>int</TD><TD WIDTH=106>interface</TD></TR><TR VALIGN=TOP><TD WIDTH=80>long</TD><TD WIDTH=106>native</TD></TR><TR VALIGN=TOP><TD WIDTH=80>new</TD><TD WIDTH=106>null</TD></TR><TR VALIGN=TOP><TD WIDTH=80>package</TD><TD WIDTH=106>private</TD></TR><TR VALIGN=TOP><TD WIDTH=80>protected</TD><TD WIDTH=106>public</TD></TR><TR VALIGN=TOP><TD WIDTH=80>return</TD><TD WIDTH=106>short</TD></TR><TR VALIGN=TOP><TD WIDTH=80>static</TD><TD WIDTH=106>super</TD></TR><TR VALIGN=TOP><TD WIDTH=80>switch</TD><TD WIDTH=106>synchronized</TD></TR><TR VALIGN=TOP><TD WIDTH=80>this</TD><TD WIDTH=106>threadsafe</TD></TR><TR VALIGN=TOP><TD WIDTH=80>throw</TD><TD WIDTH=106>transient</TD></TR><TR VALIGN=TOP><TD WIDTH=80>true</TD><TD WIDTH=106>try</TD></TR><TR VALIGN=TOP><TD WIDTH=80>void</TD><TD WIDTH=106>while</TD></TR></TABLE></CENTER><P><P>An <TT>if...else</TT> conditionalis used to execute code based on whether a <TT>boolean</TT>test is <TT>true</TT>. Single statementscan follow, or multiple statements can be declared in braces:<BLOCKQUOTE><TT>if (test == true)<BR> // ... A single statement to execute if test if true<BR>else {<BR> // ... multiple statements to execute if test if false<BR>}</TT></BLOCKQUOTE><P>The test must return a <TT>boolean</TT>value. This means an expression such as<BLOCKQUOTE><TT>if (1)</TT></BLOCKQUOTE><P>is not valid because it returns a <TT>numeric</TT>,but not a <TT>boolean</TT>. It's acceptableto nest <TT>if...else</TT> statementsor call them in a nested series, such as<B> </B><TT>if...else...if...else...if...else</TT>.<P>The latter can also be simulated with the <TT>switch</TT>conditional. Not being restricted to just boolean comparisons,the <TT>switch</TT> conditional usesa comparison with a general primitive to conduct its test. Thefollowing code uses an integer to perform its test:<BLOCKQUOTE><TT>switch ( Count ) {<BR> case 1:<BR> //... test 1<BR> break;<BR> case 4: {<BR> //... do some operation...<BR> return0;<BR> }<BR> case 24: break;<BR> default:<BR> return-1;<BR> }</TT></BLOCKQUOTE><P>The <TT>case</TT> statements can appearin braces.<H3><A NAME="Loops">Loops</A></H3><P>Java has three looping operations. The <TT>for</TT>construct loop is structured as follows:<BLOCKQUOTE><TT>for (init; test; post-test)</TT></BLOCKQUOTE><P>The first part of the expression is any initialization at thestart of the loop. The test is a simple or complex expression.The last part is an expression such as an increment or decrementof a variable. The <TT>for</TT> loopis followed by a single statement or a block of code. For example,<BLOCKQUOTE><TT> for (k = 0; k < 10; ++k) {<BR> // ... do something<BR>}</TT></BLOCKQUOTE><P>loops ten times.<P>A <TT>while</TT> loop is just thetest portion of the <TT>for</TT> loop:<BLOCKQUOTE><TT>while (test)</TT></BLOCKQUOTE><P>The <TT>do...while</TT> constructperforms the test and the end of the loop:<BLOCKQUOTE><TT>do {<BR> // ... do something<BR>} while (test);</TT></BLOCKQUOTE><P>The <TT>break</TT> construct is usedto exit from a loop, and the <TT>continue</TT>statement skips to the next iteration of the loop. Labeled loopscan also be used to control where to go in complex loops. If alabel follows a <TT>break</TT> statement,then the code breaks out of the nearest loop that has the matchinglabel. The following example effectively quits both loops whenthe variable <TT>j</TT> is greaterthan 100:<BLOCKQUOTE><TT>int i,j;<BR> quit: for (i = j = 0; i < 100; ++i) {<BR> for(int k = 0; k < 10; ++k) {<BR> //... Do something<BR> if(j > 100)<BR> breakquit;<BR> }<BR> }</TT></BLOCKQUOTE><P>If the <TT>break</TT> statement inthis example was replaced by the following,<BLOCKQUOTE><TT>if (j > 100)<BR> continue quit;</TT></BLOCKQUOTE><P>then the code would jump to the next iteration of the outer <TT>for</TT>loop.<H3><A NAME="Arrays">Arrays</A></H3><P>Arrays are first-class objects in Java; they are not just a pointerto memory as in C. Consequently, Java arrays are a lot safer.You cannot indiscriminately assign an element to just any indexin an array. Java makes sure the index is valid-this preventsthe difficult-to-track memory access violations that occur soeasily in C. If you try to access a bad array index in Java, anexception will be thrown and no action will be taken.<P>Because Java arrays are objects, their semantics are a littledifferent from their counterparts in C or C++. The thing thatconfuses most new Java programmers is how to allocate a new array.First, you cannot declare an array with a prefixed size; it mustbe declared as an uninitialized variable:<BLOCKQUOTE><
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -