📄 http:^^www.cs.wisc.edu^~cs537-1^java-tutorial.html
字号:
break; a[j+1] = a[j]; } // now a[0..j] are all <= x and a[j+2..i] are > x a[j+1] = x; } } /** Test program to test sort */ public static void main(String argv[]) { if (argv.length != 1) { System.out.println("usage: sort array-size"); System.exit(1); } int size = Integer.parseInt(argv[0]); int test[] = new int[size]; Random r = new Random(); System.out.println("<" + 4 * Math.atan(1) + ">"); for (int i = 0; i < size; i++) test[i] = (int)(r.nextFloat() * 100); System.out.println("before"); for (int i = 0; i < size; i++) System.out.println(test[i]); sort(test, size); System.out.println("after"); for (int i = 0; i < size; i++) System.out.println(test[i]); System.exit(0); }}</font></pre><p>A copy of this program is available in<!WA15><!WA15><!WA15><!WA15><!WA15><!WA15><a href="http://www.cs.wisc.edu/~cs537-1/examples/SortTest.java"><samp>~cs537-1/public/examples/SortTest.java</samp>.</a>To try it out, create a new directory and copy the example to a filenamed <samp><font color="0f0fff">SortTest.java</font></samp> in that directory. (The file <em>must</em>be called <samp><font color="0f0fff">SortTest.java</font></samp>!)<pre><font color="0f0fff"> mkdir test1 cd test1 cp ~cs537-1/public/examples/SortTest.java SortTest.java javac SortTest.java java SortTest 10</font></pre>(You can also grab a copy of the source with your net browser.Visit<!WA16><!WA16><!WA16><!WA16><!WA16><!WA16><a href="http://www.cs.wisc.edu/~cs537-1/examples/SortTest.java"><samp>~cs537-1/public/examples/SortTest.java</samp></a>and use the <em>Save As...</em> option from the <em>File</em> menu.)<p>The <samp><font color="0f0fff">javac</font></samp> command invokes the Java compiler on the sourcefile <samp><font color="0f0fff">SortTest.java</font></samp>.If all goes well, it will create a file named <samp><font color="0f0fff">SortTest.class</font></samp>,which contains code for the Java virtual machine.The <samp><font color="0f0fff">java</font></samp> command invokes the Java interpreter to run the codefor class <samp><font color="0f0fff">SortTest</font></samp>. Note that the first parameter is<samp><font color="0f0fff">SortTest</font></samp>, not <samp><font color="0f0fff">SortTest.class</font></samp>or <samp><font color="0f0fff">SortTest.java</font></samp> because it is the name of a <em>class</em>,not a file.<p>There are several things to note about this program.First, Java has no ``top-level'' or ``global'' variables or functions.A Java program is <em>always</em> a set of <samp><font color="0f0fff">class</font></samp> definitions.Thus, we had to make <samp><font color="0f0fff">sort</font></samp> and <samp><font color="0f0fff">main</font></samp> member functions(called ``methods'' in Java) of a class, which we called<samp><font color="0f0fff">SortTest</font></samp>.<p>Second, the <samp><font color="0f0fff">main</font></samp> function is handled somewhat differently inJava from C++.In C++, the first function to be executed is always a function called<samp><font color="0f0fff">main</font></samp>, which has two arguments and return an integer value.The return value is the ``exit status'' of the program; by convention,a status of zero means ``normal termination'' and anything else meanssomething went wrong.The first argument is the number of words on the command-line that invokedthe program, and the second argument is a an array of character strings(denoted <samp><font color="0f0fff">char *argv[]</font></samp> in C++) containing those words.If we invoke the program by typing<pre><font color="0f0fff"> sort 10</font></pre>we will find that<samp><font color="0f0fff">argc==2</font></samp>,<samp><font color="0f0fff">argv[0]=="sort"</font></samp>, and<samp><font color="0f0fff">argv[1]=="10"</font></samp>.<p>In Java, the first thing executed is the method called <samp><font color="0f0fff">main</font></samp>of the indicated class (in this case <samp><font color="0f0fff">SortTest</font></samp>).The <samp><font color="0f0fff">main</font></samp> method does not return any value (it is of type<samp><font color="0f0fff">void</font></samp>).For now, ignore the words ``<samp><font color="0f0fff">public static</font></samp>'' preceding<samp><font color="0f0fff">void</font></samp>. We will return to these later.The <samp><font color="0f0fff">main</font></samp> method takes only one parameter, an array of strings(denoted <samp><font color="0f0fff">String argv[]</font></samp> in Java).This array will have one element for each word on the command line<em>following</em> the name of the class being executed.Thus in our example call,<pre><font color="0f0fff"> java SortTest 10</font></pre><samp><font color="0f0fff">argv[0] == "10"</font></samp>.There is no separate argument to tell you how many words there are,but in Java, you can tell how big <em>any</em> array is by using<samp><font color="0f0fff">length</font></samp>.In this case <samp><font color="0f0fff">argv.length == 1</font></samp>, meaning <samp><font color="0f0fff">argv</font></samp>contains only one word.<p>The third difference to note is the way I/O is done in Java.<samp><font color="0f0fff">System.out</font></samp> in Java is roughly equivalent to <samp><font color="0f0fff">cout</font></samp>in C++ (or <samp><font color="0f0fff">stdout</font></samp> in C), and<pre><font color="0f0fff"> System.out.println(whatever);</font></pre>is (even more) roughly equivalent to<pre><font color="0f0fff"> cout << whatever << endl;</font></pre><p>Our C++ program used three functions from the standard library,<samp><font color="0f0fff">atoi</font></samp>, <samp><font color="0f0fff">random</font></samp>, and <samp><font color="0f0fff">exit</font></samp>.<samp><font color="0f0fff">Integer.parseInt</font></samp> does the same thing as <samp><font color="0f0fff">atoi</font></samp>:It converts the character-string "10" to the integer value ten,and <samp><font color="0f0fff">System.exit(1)</font></samp> does the same thing as <samp><font color="0f0fff">exit(1)</font></samp>:It immediately terminates the program, returning an exit status of 1(meaning something's wrong).The library class <samp><font color="0f0fff">Random</font></samp> defines random-number generators.The statement <samp><font color="0f0fff">Random r = new Random()</font></samp> create an instance ofthis class, and <samp><font color="0f0fff">r.nextFloat()</font></samp> uses it to generate a floatingpoint number between 0 and 1.The <em>cast</em> <samp><font color="0f0fff">(int)</font></samp> means the same thing in Java as in C++.It converts its floating-point argument to an integer, throwing awaythe fraction.<p>Finally, note that the <samp><font color="0f0fff">#include</font></samp> directives from C++ havebeen replaced by <samp><font color="0f0fff">import</font></samp> declarations.Although they have roughly the same effect, the mechanisms are different.In C++, <samp><font color="0f0fff">#include <iostream.h></font></samp> pulls in a source filecalled <samp><font color="0f0fff">iostream.h</font></samp> from a source library and compiles it alongwith the rest of the program.<samp><font color="0f0fff">#include</font></samp> is usually used to include files containing declarationsof library functions and classes, but the file could contain any C++source code whatever.The Java declaration <samp><font color="0f0fff">import java.util.Random</font></samp> brings inthe pre-compiled class <samp><font color="0f0fff">Random</font></samp> from a <em>package</em> called<samp><font color="0f0fff">java.util</font></samp>. The declaration <samp><font color="0f0fff">import java.io.*</font></samp>bring in <em>all</em> of the classes defined in the package<samp><font color="0f0fff">java.util</font></samp>. A package is sort of like a library.You won't have to worry much about packages for this course.<a name="objects"><h2> Values, Objects, and Pointers </h2></a><p>It is sometimes said that Java doesn't have pointers.That is not true.In fact, objects can <em>only</em> be referenced with pointers.More precisely, variables can hold primitive values (suchas integers or floating-point numbers) or <em>pointers</em>to objects.A variable cannot hold an object, and you cannot make a pointer toa primitive value.Since you don't have a choice, Java doesn't have a special notationlike C++ does to indicate when you want to use a pointer.<p>There are exactly eight primitive types in Java,<samp><font color="0f0fff">boolean</font></samp>,<samp><font color="0f0fff">char</font></samp>,<samp><font color="0f0fff">byte</font></samp>,<samp><font color="0f0fff">short</font></samp>,<samp><font color="0f0fff">int</font></samp>,<samp><font color="0f0fff">long</font></samp>,<samp><font color="0f0fff">float</font></samp>, and<samp><font color="0f0fff">double</font></samp>.Most of these are similar to types with the same name in C++.We mention only the differences.<p>A <samp><font color="0f0fff">boolean</font></samp> value is either<samp><font color="0f0fff">true</font></samp> or<samp><font color="0f0fff">false</font></samp>.You cannot use an <samp><font color="0f0fff">integer</font></samp>where a <samp><font color="0f0fff">boolean</font></samp> is required(e.g. in an <samp><font color="0f0fff">if</font></samp> or<samp><font color="0f0fff">while</font></samp> statement)nor is there any automatic conversion between<samp><font color="0f0fff">boolean</font></samp>and<samp><font color="0f0fff">integer</font></samp>.<p>A <samp><font color="0f0fff">char</font></samp> value is 16 bits ratherthan 8 bits, as it is in C or C++, to allow for all sorts of internationalalphabets.As a practical matter, however, you are unlikely to notice the difference.The <samp><font color="0f0fff">byte</font></samp> type is 8 bits.<p>A <samp><font color="0f0fff">short</font></samp> is 16 bits and an<samp><font color="0f0fff">int</font></samp> is 32 bits, just as in C orC++ on most modern machines (in C++ the size is machine-dependent, butin Java it's guaranteed).A Java <samp><font color="0f0fff">long</font></samp> is <em>not</em>the same as in C++ -- it is 64 bits long.The types <samp><font color="0f0fff">float</font></samp>and <samp><font color="0f0fff">double</font></samp> are just like in C++:32-bit and 64-bit floating point.<p>As in C++, <em>objects</em> are <em>instances</em> of <em>classes</em>.There is no prefix <samp><font color="0f0fff">*</font></samp> or <samp><font color="0f0fff">&</font></samp> operator orinfix <samp><font color="0f0fff">-></font></samp> operator.<p>As an example, consider the class declaration (which is the same inC++ and in Java)<center><pre><font color="0f0fff">class Point { int x, y; }</font></pre><table frame="box" bgcolor="#e0e0ff" width="70%" border=3 cellpadding=1 cellspacing=1><tr><th>C++<th>Java<tr><td><samp><font color="0f0fff">Point origin;</font></samp><td><samp><font color="0f0fff">Point origin = new Point();</font></samp><tr><td><samp><font color="0f0fff">Point *p, *q, *r;</font></samp><td><samp><font color="0f0fff">Point p, q, r;</font></samp><tr><td><samp><font color="0f0fff">origin.x = 0;</font></samp><td><samp><font color="0f0fff">origin.x = 0;</font></samp><tr><td><samp><font color="0f0fff">p = new Point;</font></samp><td><samp><font color="0f0fff">p = new Point();</font></samp><tr><td><samp><font color="0f0fff">p -> y = 5;</font></samp><td><samp><font color="0f0fff">p.y = 5;</font></samp><tr><td><samp><font color="0f0fff">q = p;</font></samp><td><samp><font color="0f0fff">q = p;</font></samp><tr><td><samp><font color="0f0fff">r = &origin;</font></samp><td><i>not possible</i></table><p><!WA17><!WA17><!WA17><!WA17><!WA17><!WA17><IMG ALIGN=TOP SRC="http://www.cs.wisc.edu/~cs537-1/objects.gif">.</center><a name="garbage"><h2> Garbage Collection </h2></a><p>New objects are create by the <samp><font color="0f0fff">new</font></samp> operator in Java just like C++(except that an argument list is required after the class name, evenif the constructor for the class doesn't take any arguments so the listis empty).However, there is no <samp><font color="0f0fff">delete</font></samp> operator.The Java system automatically deletes objects when no references to themremain.This is a much more important convenience than it may at first seem.<samp><font color="0f0fff">delete</font></samp> operator is extremely error-prone.Deleting objects too early can lead to <em>dangling</em> reference, as in<pre><font color="0f0fff"> p = new Point(); // ... q = p; // ... much later delete p; q -> x = 5; // oops!</font></pre>while deleting them too late (or not at all) can lead to <em>garbage</em>,also known as a <em>storage leak</em>.<a name="access"><h2> Static, Final, Public, and Private </h2></a><p>Just as in C++, it is possible to restrict access to members of a classby declaring them <em>private</em>, but the syntax is different.<br>In C++:<pre><font color="0f0fff"> class C { private: int i; double d;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -