JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 新手區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 新手常見問題解答 [精華]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:00 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
以下為轉錄自java連線版的新手系列
歡迎大家補足


worookie edited on 2003-08-27 06:56
reply to postreply to post
作者 1.java.lang.NoClassDefFoundError [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:02 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1. 在什麼情況下會發生 java.lang.NoClassDefFoundError , 以及如何避免這種情況發生?

Java.lang.NoClassDefFoundError 是 Java 無法在所給予的 classpath 中找到提及的類別或介面的定義,因而丟出的執行時期例外。這個錯誤通常是因為 classpath 的設定錯誤而丟出的。

如果這個錯誤被丟出,請確定你在 classpath 中有包含目前的目錄( 以句點 . 表示)。所有在套件中的使用者定義類別也應該在 classpath 中。也請確定你的路徑有參考到 Java 類別庫檔案,位於 JDK 軟體安裝目錄的 bin 目錄中。

在 Solaris 環境中

set PATH=${PATH}:/usr/java/bin
set CLASSPATH=.:/usr/java/lib/

下列的程式碼可以檢查你的 JVM 軟體使用什麼 classpath:

1
2
3
4
5
6
7
8
9
10
11
public void displayClassPath()
{
  try
  {
    System.out.println(System.getProperty("java.class.path"));
  }
  catch(Throwable t)
  {
    t.printStackTrace();
  }
}


如果你使用 java Test.java 命令來執行類別,也會丟出這個錯誤。這個應該是使用 java Test,因為類別名稱是 Test 而不是 Test.java,而這是引數 JVM 會搜尋的正規值。


swanky edited on 2003-09-16 21:55
reply to postreply to post
作者 2. 啟動在 MS Windows 平台上的外部程式 [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:04 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
2. 你如何以 Java 程式語言開發的程式來啟動在 MS Windows 平台上的外部程式?

下列將會啟動 MS Windows NT 中的記事本:

Runtime.getRuntime().exec("cmd /c notepad.exe");

要啟動 MS Windows 95/98 中的,則使用:

Runtime.getRuntime().exec("c:\\windows\\notepad.exe");

Runtime 類別讓程式和他的環境互動。第一個字串命令指示命令行的直譯器 cmd 來開啟應用程式。

exec() 方法並不會使用 shell;任何參數都必須使用到 shell 的完整的路徑名稱。

例如要執行 Unix 平台的 shell,鍵入:

Runtime rt = Runtime.getRuntime();
Process p = rt.exec("/usr/bin/sh -c date");

要執行在 MS Windows 95/98 下的批次檔:

Process p = rt.exec("command.com /c c:\\mydir\\myfile.bat");

要執行在 MS Windows NT 下的批次檔:

Process p = rt.exec("cmd /c c:\\mydir\\myfile.bat");

'cmd' 和 'command.com' 是 MS Windows 機器的命令行直譯器。

在某些平台上的一些程序中,Runtime.exec() 方法可能不會很有效率的運作。要特別關心 native 視窗,daemon,Win16/Dos 程序或一些 shell。


swanky edited on 2003-09-16 21:58
reply to postreply to post
作者 3. 下載最新版的 JDK [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:05 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
3. 可以在哪裡下載最新版的 JDK ?

你可以在 http://java.sun.com/j2se/downloads.html 找到最新版的 JDK。

JDC 網站的 J2SE 產品群下尋找。也有 API 文件,以及上一版的細節


swanky edited on 2003-09-16 22:00
reply to postreply to post
作者 4. 設定永久的 classpath [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:06 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
4. 如何在 Solaris 和 MS Windows 系統環境中設定永久的 classpath。

要使用任何的 JDK 軟體,你只需要將相對於 JDK 的 bin 目錄的路徑加到 PATH 環境變數中。你使用 CLASSPATH 變數來參考到其它的類別庫或是使用這定義的類別。例如,令 /myclasses 是使用這定義的套件。

OS: Solaris
Shell: C-shell, T-shell
將下列放在 $USER_HOME/.cshrc 中來永久的維持設定:

setenv JAVA_HOME /home/myname/jdk1.3
setenv CLASSPATH .:/myclasses
setenv PATH $PATH:$JAVA_HOME/bin

Shell: Bourn, K-Shell
將下列放在 $USER_HOME/.profile 中:

JAVA_HOME = /home/myname/jdk1.3
CLASSPATH = .:/myclasses
PATH = $PATH:$JAVA_HOME/bin

OS: MS Windows 95/98
將下列放在系統的 AUTOEXEC,BAT 檔案中:

set JAVA_HOME=C:\jdk1.3
set CLASSPATH=.;%JAVA_HOME%\myclasses
set PATH=%PATH%:%JAVA_HOME%\bin

OS: MS Windows NT

開啟控制台的系統圖示。從系統屬性對話框中選擇環境標籤,並將下列放在使用者變數區中:

JAVA_HOME C:\jdk1.3
CLASSPATH .;%JAVA_HOME%\myclasses
PATH %PATH%;%JAVA_HOME%\bin

更深入的資訊,請見
http://java.sun.com/j2se/1.4/docs/tooldocs/solaris/classpath.html


swanky edited on 2003-09-16 22:02
reply to postreply to post
作者 5. 載入在使用者的 classpath 中所定義的類別 [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:07 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
5. 你如何明確的載入在使用者的 classpath 中所定義的類別?

1. 使用 System.getProperty("java.class.path");
2. 將 classpath 分割成它的元件路徑名稱. 檢查每個路徑名稱的目錄來搜尋 Java 的類別檔, 並且使用 ClassLoader 載入類別. 關於載入類別和使用 java reflection 的相關文件如下:

執行時期類別資訊的設計, 位於 JavaWorld 的
http://www.javaworld.com/javaworld/jw-02-1999/jw-02-techniques_p.html

以 Reflection 來動態執行, 位於 Java Developer Connection 網站
http://developer.java.sun.com/developer/qow/archive/86/index.html

使用 Java 技術 Reflection, 位於 Java Developer Connection 網站
http://developer.java.sun.com/developer/technicalArticles/ALT/Reflection/

Reflection API Java 教學, 位於
http://java.sun.com/docs/books/tutorial/reflect/index.html


swanky edited on 2003-09-16 22:03
reply to postreply to post
作者 6. 拿到 Java 的認證 [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:08 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
6. 我如何拿到 Java 的認證?

Sun 有四種不同的 Java 認證:Java Programmer, Java Developer, Java Architecture, 以及 Web Component Developer. 建議依照這個順序來取得認證。
比較後方的認證是最近才加上的。那是朝向用來做網頁開發的 J2EE 技術。所有 Sun 的 Java 認證可以在 http://suned.sun.com/HQ/certification/ 中找到。

與認證有關的相關資訊請參考Java Certification


swanky edited on 2003-09-16 22:06
reply to postreply to post
作者 7. Sun 認證網站資源 [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:09 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
7. 有任何網站可以讓我為 Sun 認證的 Java 平台測驗做準備嗎?

下列網站有這個測驗的資源:

http://javaprepare.com/
http://www.javaranch.com/
http://www.digitalthink.com/
http://www.jaworski.com/java/certification
http://www.jchq.net/
http://www.certificationguru.com/

與認證有關的相關資訊請參考Java Certification


swanky edited on 2003-09-16 22:07
reply to postreply to post
作者 8. 在基本資料型別和 java.lang.String 之間轉換 [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:11 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
8. 如何在基本資料型別和 java.lang.String 之間轉換?

所有的基本資料型別都可以使用在 java.lang 底下的包裝類別轉換成字串。
例如 java.lang.Integer,java.lang.Byte,等等。

Byte myByte = new Byte(289);
int myInt = myByte.intValue();
String myHexString = Integer.toHexString(myInt);

所有除了 byte 之外的基本資料型別都可以用過載的 String.valueOf() 方法來轉
換成字串。

String intStr = String.valueOf(1);
String doubleStr = String.valueOf(Math.PI);
String boolStr = String.valueOf(true);


reply to postreply to post
作者 9.使用 '==' 運算子和 java.lang.String.equals()的不同 [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:12 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
作者: contagious (傳染) 看板: java
標題: [合作翻譯]Java新手 9
時間: Mon Jun 17 23:13:25 2002

9.當比較兩個字串(String)時,使用 '==' 運算子和 java.lang.String.equals()這個
method有什麼不同呢?

'=='這個運算子會看看這兩個字串的references是不是指向同一個字串object。而
java.lang.String.equals()這個method則會比較這兩個字串的"值"是不是一樣的。換句
話說,比較這兩個字串是不是有相同的字元序列。

當使用String literals(一串被雙引號括住的字元)時,使用 '=='運算子和使用equals
method 的結果會是一樣的。所有的String literals都是指向同一個String 類別的
instances。系統中有一個pool,當有新的String literals出現時,系統會先去檢查pool
之中,是不是已經存在一個和這個新的String literals有相同內容的物件。如果存在,
則會傳回一個指向這個此物件的reference。若不存在,則會將此String literals放到
pool中,然後傳回這個物件的reference。

舉個例子:
String s1 = "hello";
String s2 = "hell"+"o";
System.out.println("Using equals op"+ (s1==s2)); //True
System.out.println("Using equals method" + (s1.equals(s2))); //True

當字串是由"new"這個關鍵字所造出來的時候,則不是這麼一回事。

String s3 = new String("hello");
String s4 = new String("hello");
System.out.println("Using equals op" + (s3==s4)); //False
System.out.println("Using equals method" + (s3.equals(s4))); //True

傳用"new"這個關鍵字時,會造出兩個不同的物件,所以會有兩個不同的references,即
使在底層的string literal是一樣的。在上面的例子中,'=='運算子傳回false,因為兩
個references是不同的。而equals method則傳回true,因為這兩個物件所代表是同樣的
字元序列。

9. When testing for equality between strings, what is the difference between
using '=='operator and java.lang.String.equals(String)?

The '==' operator compares two string references to see if they point to the
same string object. The java.lang.String.equals() method checks if the objects' values are equal.That is, if the referent string objects have the
same character sequence.

In the case of string literals (a sequence of characters enclosed in double
quotes) applying '==' operator and the equals method will yield the same
results. All string literals reference an instance of class string. String
literals are "interned" to allow the sharing of String instances. A pool of
strings are maintained. When a new string literal is created, the pool is
checked for a string instance with the same character sequence. If such an
instance exists then a reference to that object is returned. Otherwise the
string object is added to the pool, and a reference to that object is returned.

For example:
String s1 = "hello";
String s2 = "hell"+"o";
System.out.println("Using equals op"+ (s1==s2)); //True
System.out.println("Using equals method" + (s1.equals(s2))); //True

This is not the case with strings that are created with the keyword, "new".

String s3 = new String("hello");
String s4 = new String("hello");
System.out.println("Using equals op" + (s3==s4)); //False
System.out.println("Using equals method" + (s3.equals(s4))); //True

Using the new operator you will create two distinct objects and thus, two
different references - although the underlying string literal is the same.
With deference to the above code, the '==' operator returns false
because the two references are different, while equals returns true, since
both object values represent the same sequence of characters.


reply to postreply to post
作者 10.在Java中是否可以移動/刪除檔案 [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:14 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
作者: contagious (傳染) 看板: java
標題: [合作翻譯]Java新手 10
時間: Mon Jun 17 22:37:57 2002

10.在Java中是否可以移動/刪除檔案 (Java的基本的檔案處理)

新增檔案:
java.io.File.createNewFile() throws IOException

例如:

1
2
3
4
5
6
7
8
9
try {
    File myFile = new File("File");
        if(myFile.createNewFile())
            System.out.println(
                        "No previous file with this name existed: file created");
        else
            System.out.println(
                        "File not created as a file with this name exists");
}catch(IOException e){}


新增目錄:
java.io.File.mkdir()

例如:
1
2
        File myDir = new File("newDir/");
        myDir.mkdir();


移動檔案:
java.io.File.rename(String DestPath)

DestPath可以是相對的或是絕對的路徑

刪除檔案或是目錄

java.io.delete();

10. Is there any functionality to move/delete files on the underlying filesystem in
the Java programming language?

To create a new file on filesystem:
java.io.File.createNewFile() throws IOException

For example...
1
2
3
4
5
6
7
8
9
try {
        File myFile = new File("File");
        if(myFile.createNewFile())
                System.out.println(
                        "No previous file with this name existed: file created");
        else
                System.out.println(
                        "File not created as a file with this name exists");
}catch(IOException e){}


To create a new directory on the filesystem:
java.io.File.mkdir()

For example...
File myDir = new File("newDir/");
myDir.mkdir();

To move a file on the filesystem:
java.io.File.rename(String DestPath)
The path can be absolute or relative to the current directory.

To delete a file or directory:
java.io.delete();


reply to postreply to post
作者 11.deprecated是什麼意思? [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:15 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
作者: contagious (傳染) 看板: java
標題: [合作翻譯]Java新手 11
時間: Mon Jun 17 22:40:57 2002

11.deprecated是什麼意思?

有時在編譯完 java 的原始碼後會出現Deprecation warings。 Deprecation 表示你使
用了一些舊的API,這些API在新版的JDK都有其他可用的API可以取代他們。你應該使用
新的API來確定這個程式可以和未來的JDK版本維持相容。在編譯時使用 -deprecate 來
列出所有你所用的 deprecated methods和相對應的新的methods.

更詳細的資料請參考

Deprecation of APIs
http://java.sun.com/products/jdk/1.1/docs/guide/misc/deprecation/index.html

11. What does deprecated mean?

Deprecation warnings sometimes occur after the compilation of java file.
Deprecation indicates that you are using an older API which has been
replaced in a later JDK version. You should use the newer versions of these
APIs in your Java technology files to ensure compatibility with future JDK
versions. Run your Java technology file with the -deprecate flag which will
list all deprecated methods and the corresponding replacement methods.

For more information see Deprecation of APIs.


reply to postreply to post
作者 12.如何取得由Java程式呼叫的外在程式的執行結果? [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:16 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
作者: contagious (傳染) 看板: java
標題: [合作翻譯]Java新手 12
時間: Mon Jun 17 22:46:54 2002

12.如何取得由Java程式呼叫的外在程式的執行結果?

每個application都會有一個Runtime類別的instance,讓你可以和執行時期的環境做互動。
你可以呼叫 getRuntime()來取得這個instance。

Runtime.exec()的參數指的是要執行的程式。Runtime.exec()會產生一個原生的process並
傳回一個 Process的子類別,可以用來控制或者取得這個原生process的資訊。這個process
既不使終端機介面,也不用命令列介面,而是將他的I/O操作都轉向到這三個串流:

Process.getOutputStream(), Process.getInputStream(), Process.getErrorStream().

下面是一個例子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.io.*;
public class ReadingProcess {
    public static void main(String args[]){
        //Provide a command as an input argument
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(args[0]);
        BufferedWriter buffOut = new BufferedWriter(
                                 new OutputStreamWriter(
                                        process.getOutputStream());
        BufferedReader buffIn = new BufferedReader(
                                new InputStreamReader(
                                        process.getInputStream());
 
        buffOut.write("//Some String");
        buff.flush(); //Ensure that the output reaches the process
 
        String s;
 
        if((s=buffIn.readLine())!= null)
        System.out.println(s);
 
        buffOut.close();
        buffIn.close();
    }
}


注意!
根據不同的底層作業平台,你可能會遇到一些停滯的狀況。有一些平台的I/O串流只有很小
的buffer,如果不能快速的對標準I/O串流做讀寫,則可能會發生停滯的狀況。

12. How do you retrieve output from external programs that have been executed by
a program developed on the Java Platform?

Every application written on has one instance of the class Runtime to allow
interaction with its runtime environment. You retrieve this instance by calling
getRuntime().

The arguments of Runtime.exec() are commands to be executed in the runtime
environment. The Runtime.exec() methods create native processes and return a
subclass of Process which provides methods to control and retrieve information
from the underlying process. The process has neither a terminal nor a console
thus all its standard I/O operations are redirected to the parent process via
three streams: Process.getOutputStream(), Process.getInputStream(),
Process.getErrorStream().

Note the following sample:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import java.io.*;
public class ReadingProcess {
    public static void main(String args[]){
        //Provide a command as an input argument
        Runtime runtime = Runtime.getRuntime();
        Process process = runtime.exec(args[0]);
        BufferedWriter buffOut = new BufferedWriter(
                                 new OutputStreamWriter(
                                        process.getOutputStream());
        BufferedReader buffIn = new BufferedReader(
                                new InputStreamReader(
                                        process.getInputStream());
 
        buffOut.write("//Some String");
        buff.flush(); //Ensure that the output reaches the process
 
        String s;
 
        if((s=buffIn.readLine())!= null)
        System.out.println(s);
 
        buffOut.close();
        buffIn.close();
    }
}


A note of warning - you may experience some hanging depending on the type of
native platform you are running on. Some native platforms have small buffers for
standard I/O streams, failure to quickly write/read from the standard
output/input streams into those of the subprocess may cause the subprocess to
block.


reply to postreply to post
作者 13.抽象類別和介面有什麼不同? [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:18 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
作者: contagious (傳染) 看板: java
標題: [合作翻譯]Java新手 13
時間: Wed Jun 19 00:59:26 2002

13.抽象類別(abstract classes)和介面(interfaces)有什麼不同?
抽象類別和介面是兩個不同的物件導向觀念,用於不同的地方。抽象類別接近於
Abstract Datatype,他宣告了一群類別所共同擁有的一些特性。你可以在抽象類別中宣
告methods 和變數來宣告這些特性。在Java中,抽象類別或者是method會被 'abstract'
這個關鍵字所標示。舉例來說,定義一個抽象類別:Vehicle。

1
2
3
4
public abstract class Vehicle{
    public int wheels;
    public abstract void move();
}


Vehicle這個類別宣告了所有交通工具的共同特性和行為。你可以繼承Vehicle來定義各種
是交通工具但又有別的特性和行為的類別,像是car或是bicycle。Vehicle是一個抽象的
概念,而 car 和 bicycle 則是更精細的將Vehicle的行為做分類,來指出car和bicycle
的特性。

1
2
3
4
5
6
7
8
9
10
11
12
public class Car extends Vehicle{
    public void move(){}
    //Additional behaviour characterise a car
    public void typeOfEngine(){}
    wheels = 4;
}
 
public class Bicycle extends Vehicle{
    public void move(){}
    wheels = 2;
    public void peddelling(){}
}


介面定義了一組協定的行為,讓很多的類別來實做(implement)他。介面定義了一些
methods和一些final static 變數。任何類別,實作了所有這些methods之後,就保證他
會有像這個介面所要求的行為。使用介面可以確保物件之間可以某種方式互相溝通。

下面是個介面的例子:

1
2
3
4
public interface TCPProtocol{
        public void open();
        public void close();
}


在Java中,介面和抽象類別有三個主要的不同點:

1.一個類別可以implemt很多個介面,但只能extends一個父類別。
2.抽象類別可以有一些不是abstract的method,但是介面不能。
3.介面並不屬於類別階層之中。

更多的參考資料:
Writing Abstract Classes and Methods
http://java.sun.com/docs/books/tutorial/java/javaOO/abstract.html
Creating Interfaces
http://java.sun.com/docs/books/tutorial/java/interpack/interfaces.html

13. What are the differences between abstract classes and interfaces?

Abstract classes and interfaces are two different OO concepts and are used
differently. An abstract class is similar to an abstract data type. It defines
characteristics that are common to any of its subclasses or subtypes. You
define characteristics by declaring methods and variables in an abstract
class. For example, define an abstract type, Vehicle. In the Java [tm]
programming language, an abstract class or method is flagged with the keyword,
'abstract'.

1
2
3
4
public abstract class Vehicle{
    public int wheels;
    public abstract void move();
}


The class Vehicle declares behaviours and characteristics that are common to
all vehicles. You can define classes which extend vehicle, that are types of
vehicles but require more behaviours, such as car or bicycle. Class Vehicle is
the abstraction, and car and bicycle add more details to refine the Vehicle
behaviour to characterise a car and bicycle object.

1
2
3
4
5
6
7
8
9
10
11
12
public class Car extends Vehicle{
    public void move(){}
    //Additional behaviour characterise a car
    public void typeOfEngine(){}
    wheels = 4;
}
 
public class Bicycle extends Vehicle{
    public void move(){}
    wheels = 2;
    public void peddelling(){}
}


An interface defines a protocol of behaviour that any class can implement. An
interface defines methods, and final static variables. Any class that
implements all the interfaces' methods guarantees to behave according to the
protocol defined by the interface. Using interfaces ensures that objects can
communicate in certain manners.

Following is an example of an interface:

1
2
3
4
public interface TCPProtocol{
        public void open();
        public void close();
}


In terms of the Java Platform, there are three main differences in abstract
and interface implementation:

1.A class can implement multiple interfaces but only one superclass.
2.Abstract classes can implement non-abstract methods, while interfaces
can not.
3.Interfaces are not part of the class hierarchy.

For more information see
Writing Abstract Classes and Methods
Creating Interfaces


reply to postreply to post
作者 14.如何改變現在的工作目錄? [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:19 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
作者: contagious (傳染) 看板: java
標題: [合作翻譯]Java新手 14
時間: Wed Jun 19 01:10:24 2002

14.如何改變現在的工作目錄?

在JDK1.3中,使用 Runtime.exec(String command,String [] envp, File dir)
會產生一個原生的process來執行command所指定的程式。這個程式會使用 enbp 做為參數
,以 dir 為工作目錄。如果 dir 為 null 的話,這個程式會使用他的parent process
(就是你呼叫他的程式)的工作目錄。在Jdk1.3之前,並沒有內建這種功能。在unix下,有
一個解決的方法是使用 exec("cd /dir; app.sh")。在 WindowsNT 下就不能這樣作了。

14. How do you change the current working directory within a java program?

In JDK 1.3 release, use exec(String command, String [] envp, File dir) of
class, Runtime. A native process will execute a command, with its arguments
provided by envp, in the working directory dir. If dir is null, then the
process inherits the current working directory of its parent process. Before
the JDK 1.3 release there was no in-built functionality. A possible workaround
is, process.exec("cd /dir; app.sh") for the UnixR platform. There is no
equivalent in Microsoft WindowsNT.


reply to postreply to post
作者 15.前置和後置的increment運算子有什麼不同? [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:20 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
作者: contagious (傳染) 看板: java
標題: [合作翻譯]Java新手 15
時間: Wed Jun 19 01:28:47 2002

15.前置和後置的increment運算子有什麼不同?
(a++ 和 ++a 有什麼不同)

不同的地方在於發生累加的順序。前置的運算子(++/--)會把運算加一(或減一)後,再使
用運算過後的新值。而後置運算子會使用原來的值,然後才做運算的動作。在後置的情況
下,新的值要等到下一次用碰到這個運算元的時後才會被用到。

例一:
1: int p = 3, q = 4;
2: int r = p + q++; //r = 7, q = 5
3: q++;//q=6

在有使用 assignment 的時候,這個順序就顯得非常的重要。在例二的第三行,後置的動
作變成沒有作用。這是因為 expression 被處理的方式的關係:後置的運算子的動作會比
assignment 的的動作要早。當"+="的左邊被處理時,b的值7會先被取出,存在一個暫時
的變數中。然後後置的運算子會把b減一成為6。在暫時的變數中的值會被加兩次到b中(因
為+=的關係),所以後置運算子算出來的結果6會被assignment的結果14給覆蓋掉。

例二:
1: int b = 3;
2: b += ++b; //b = 7
3: b += b--; //b = 14

15. What is the difference between the prefix and postfix increment operators?

The difference lies in the order in which the increment occurs. Prefix
operators (++/--) will add one to the operand and then use the new value,
while postfix uses the current value of the operand and then increments the
value. In the latter case, the incremented value cannot be used until the
operand is referenced again.

Example 1:
1: int p = 3, q = 4;
2: int r = p + q++; //r = 7, q = 5
3: q++;//q=6

The order of evaluation is particularly important when applying assignment and
postfix operator to the same variable reference. At line 3 of Example 2, the
application of the postfix operator is lost because of the way the expression
is evaluated. The postfix operator is applied before assignment operators.
When the LHS and RHS of '+=' are evaluated, b's value is taken as 7 and is
then stored in a temporary variable. The postfix operator is then applied so
that b's value is then 6. The value in the temporary variable is added twice
to variable b so that the previous value of b, 6 is overwritten with 14.

Example 2:
1: int b = 3;
2: b += ++b; //b = 7
3: b += b--; //b = 14


reply to postreply to post
作者 16.遞移運算子:為什麼 3<<3 和 3<<1 是一樣的? [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:21 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
作者: contagious (傳染) 看板: java
標題: [合作翻譯]Java新手 16
時間: Wed Jun 19 01:42:42 2002

16.遞移運算子(Shift operators):為什麼 3<<3 和 3<<1 是一樣的?

在Java中,整數是二的補數的方式儲存。所有的負數在二進位表示法中的第一個bit都是1
。把最大的整數加上一之後,結果會是最小的整數。這樣旋轉的特性使得overflow和
underflow都不會發生。在使用遞移運算子,shift的距離超過了這個資料型態的bits數時
,這樣旋轉就變得很明顯。舉個整數的例子,整數在Java中是以32bits來存,所以可以被
shift的的距離是指定的數字被32除之後的餘數。所以3<<33時,(33 modulo 32)=1,所以
和 3<<1一樣。

16. Shift operators: Why is 3<<33 equvialent to 3<<1?

In the Java Programming language, integers are stored in two's complement
form. All negative integers in bit form have a '1' in the most significant
bit. Adding 1 to the maximum integer value would give a result equivalent to
the minimum integer value. This wrap-around feature ensures that neither over
-flow nor under-flow occurs. In the case of shift operators, this wrap-around
is evident when a number is shifted with a shift distance that exceeds the bit
width of that number's type. For example for an integer type, the bit width is
32. The number of bits shifted is in the range modulus 32. So for 3 << 33, the
effective shift distance is (33 modulo 32) = 1.


reply to postreply to post
作者 17.如何對兩個日期做相減? [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:22 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
作者: contagious (傳染) 看板: java
標題: [合作翻譯]Java新手 17
時間: Wed Jun 19 01:52:18 2002

17.如何對兩個日期做相減?

做日期相減的一個有效的方法是先將日期轉成時間,再來做相減的動作。先產生兩個
Calender這個類別的 instances來表示日期,再用他們產生兩個Date 物件表示時間(這個
時間是以milliseconds來表示)。將兩個時間相減,再將結果除以一天有多少
milliseconds,就可以得多兩個日期相差多少天。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.*;
 
public class DateSubtraction{
    public static void main(String [] args){
        Calendar cal = Calendar.getInstance();
        cal.set(2000, Calendar.AUGUST, 24);
        Date d1 = cal.getTime();
        cal.set(2000, Calendar.AUGUST, 28);
        Date d2 = cal.getTime();
        long daterange = d2.getTime() - d1.getTime();
        long time = 1000*3600*24; //A day in milliseconds
        System.out.println(daterange/time);
    }
}


更多有關 Date 和 Calender 的資訊請看:
Class Calender
http://java.sun.com/j2se/1.3/docs/api/java/util/Calendar.html
Class Date
http://java.sun.com/j2se/1.3/docs/api/java/util/Date.html

17. How do you do date subtraction between two dates?

An effective way to do date subtraction is to convert the dates into a time
representation and then do subtraction.You create two Calendar instances to
represent the dates, then create Date objects of these calendar instances to
represent the time in milliseconds. Subtract the time representation and
divide by the number of milliseconds in one day to retrieve the difference in
days.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import java.util.*;
 
public class DateSubtraction{
    public static void main(String [] args){
        Calendar cal = Calendar.getInstance();
        cal.set(2000, Calendar.AUGUST, 24);
        Date d1 = cal.getTime();
        cal.set(2000, Calendar.AUGUST, 28);
        Date d2 = cal.getTime();
        long daterange = d2.getTime() - d1.getTime();
        long time = 1000*3600*24; //A day in milliseconds
        System.out.println(daterange/time);
    }
}


For more information on Date and Calendar objects see:

Class Calender

Class Date


reply to postreply to post
作者 18.我可以在哪裡找到Java程式的寫作慣例(coding convetions)? [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:23 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
作者: contagious (傳染) 看板: java
標題: [合作翻譯]Java新手 18
時間: Wed Jun 19 01:58:52 2002

18.我可以在哪裡找到Java程式的寫作慣例(coding convetions)?

下面有兩個有關寫作慣例不錯的資源,包括好的版面設計的準則和變數的標準命名方式:

The Java Language Specification 中第六章有關命名慣例nhttp://java.sun.com/docs/books/jls/second_edition/html/names.doc.html

昇陽定的 Code Conventions for the Java Programming language
http://java.sun.com/docs/codeconv/

18. Where can I find information on the Java programming language coding
conventions?

Following are two good resources on the Java programming language coding
conventions, including the principles of good layout, the standard format for
variable and class names, etc:

Chapter 6 of The Java Language Specification describes naming conventions.
Code Conventions for the Java Programming language, as set by Sun.


reply to postreply to post
作者 19.介面(interface)可以從別的地方繼承任何的行為嗎? [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:25 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
作者: contagious (傳染) 看板: java
標題: [合作翻譯]Java新手 19
時間: Wed Jun 19 02:07:29 2002

19.介面(interface)可以從別的地方繼承任何的行為嗎?

介面可以繼承別的介面。下面告訴你如何做:

1
2
3
4
5
6
7
8
9
public interface superInterface{
    public void superMethod1();
    public in superMethod2();
}
 
public interface subInterface extends superInterface{
    public void subMethod1();
    public void subMethod2();
}


不同於類別,介面可以extend很多個其它的介面。用這個特性可以達到多重繼承的功能。
子介面和子敗別一樣,定義了一些父介面所沒有的行為。任何implement子介面的類別,
必須將所有在父介面和子介面中所定義的method 都實做出來。

19. Can an interface inherit any behaviour from other sources?

An interface can extend other interfaces. The coding to extend an interface is
as follows:

1
2
3
4
5
6
7
8
9
public interface superInterface{
    public void superMethod1();
    public in superMethod2();
}
 
public interface subInterface extends superInterface{
    public void subMethod1();
    public void subMethod2();
}


An interface, unlike a class, can extend several interfaces. Using this
feature, it is possible to implement multiple inheritance. A subinterface,
like a subclass is defining more behaviours inherited from a superinterface.
The subinterface is defining stricter protocol behaviours. Any class that
implements a subinterface must implement all methods in both the super and
subinterfaces.


reply to postreply to post
作者 20.如何取得記憶體的統計資料(memory statistics)? [Re:swanky]
swanky

暴走熊

版主

發文: 461
積分: 9
於 2003-08-21 09:26 user profilesend a private message to usersend email to swankyreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
作者: contagious (傳染) 看板: java
標題: [合作翻譯]Java新手 20
時間: Wed Jun 19 02:16:54 2002

20.如何取得記憶體的統計資料(memory statistics)?

你可以使用Runtime類別來取得這些統計資料:

1
2
3
4
5
6
7
8
public class MemoryStats{
    public static void main(String args[]){
        long totalMem = Runtime.getRuntime().totalMemory();
        long freeMem = Runtime.getRuntime().freeMemory();
        System.out.println("Total memory"+ totalMem);
        System.out.println("Free memory" + freeMem);
    }
}


取得的資料是從Java 虛擬機器(JVM[ym] software)而來的。單位是bytes。你可以使用下
面這些Java執行環境(Java Runtime Enviroment)的旗標來增加heap 和 stack 的大小:

JDK version 1.1:
Heap: java -mx200m MyApp
Thread Stack: java -oss300m MyApp

JDK version 1.2 and above:
Heap: java -Xmx200m MyApp
Thread Stack: java -Xss300m MyApp

更多有關調整 JVM 記憶體的資訊請參考Java Virtual Machine
http://java.sun.com/docs/books/vmspec/2nd-edition/html/Overview.doc.html#1732

20. How do you retrieve memory statistics from a program written in the Java
programming language?

You can retrieve these statistics from the Runtime class:

1
2
3
4
5
6
7
8
public class MemoryStats{
    public static void main(String args[]){
        long totalMem = Runtime.getRuntime().totalMemory();
        long freeMem = Runtime.getRuntime().freeMemory();
        System.out.println("Total memory"+ totalMem);
        System.out.println("Free memory" + freeMem);
    }
}


These memory statistics are for the Java virtual machine (JVM[tm] software)
and are in bytes. You can increase the allocated heap and stack size to a
maximum for an application by using the following flags for the Java[tm]
runtime environment:

For JDK version 1.1:
Heap: java -mx200m MyApp
Thread Stack: java -oss300m MyApp

For JDK version 1.2 and above:
Heap: java -Xmx200m MyApp
Thread Stack: java -Xss300m MyApp

For more information on adjusting memory for JVM software, see Java Virtual
Machine.


reply to postreply to post
作者 Re:11.deprecated是什麼意思? [Re:swanky]
寒冰





發文: 7
積分: 0
於 2003-12-25 20:01 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
deprecated啊﹐以前也碰到過﹗
像響應事件時常會有這個warning
JDK的版本問題而已
不影響程序的執行效果


reply to postreply to post
作者 Re:17.如何對兩個日期做相減? [Re:swanky]
dinos





發文: 2
積分: 0
於 2005-02-15 17:48 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
跨月好像會有問題耶???
還是我寫錯了!!!Black Eye


reply to postreply to post
*********************************
Best Regards
*********************************
作者 Re:16.遞移運算子:為什麼 3<<3 和 3<<1 是一樣的? [Re:swanky]
小波





發文: 1
積分: 0
於 2005-07-21 19:49 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這篇標題跟內容少到一個3
3<<"33"和3<<1是一樣的
swanky wrote:
作者: contagious (傳染) 看板: java
標題: [合作翻譯]Java新手 16
時間: Wed Jun 19 01:42:42 2002

16.遞移運算子(Shift operators):為什麼 3<<3 和 3<<1 是一樣的?

在Java中,整數是二的補數的方式儲存。所有的負數在二進位表示法中的第一個bit都是1
。把最大的整數加上一之後,結果會是最小的整數。這樣旋轉的特性使得overflow和
underflow都不會發生。在使用遞移運算子,shift的距離超過了這個資料型態的bits數時
,這樣旋轉就變得很明顯。舉個整數的例子,整數在Java中是以32bits來存,所以可以被
shift的的距離是指定的數字被32除之後的餘數。所以3<<33時,(33 modulo 32)=1,所以
和 3<<1一樣。

16. Shift operators: Why is 3<<33 equvialent to 3<<1?

In the Java Programming language, integers are stored in two's complement
form. All negative integers in bit form have a '1' in the most significant
bit. Adding 1 to the maximum integer value would give a result equivalent to
the minimum integer value. This wrap-around feature ensures that neither over
-flow nor under-flow occurs. In the case of shift operators, this wrap-around
is evident when a number is shifted with a shift distance that exceeds the bit
width of that number's type. For example for an integer type, the bit width is
32. The number of bits shifted is in the range modulus 32. So for 3 << 33, the
effective shift distance is (33 modulo 32) = 1.


reply to postreply to post
作者 Re:1.java.lang.NoClassDefFoundError [Re:swanky]
nfrog8108





發文: 5
積分: 0
於 2005-08-25 17:03 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
照指教试运行这个test文件,还是一样不行。
想问,设定好path,classpath设定为.; (就是任何路径)。这样,运行java命令时是敲
java "(源文件所在路径)“
还是
java (源文件名)


reply to postreply to post
go to first page go to previous page  1   2  go to next page go to last page
» JWorld@TW »  Java 新手區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8