星期日, 2月 26, 2006

Java Homework2

1. Explain bytecode, JVM

Byte-Code 位元碼、中間碼
The java compiler translates your java program in to a language
called byte-code, which is the machine language for a fictitious
computer. It is easy to translatethis byte-code into the machine
language of any particular computer. Each type of computer will
have its own interpareter that translates and excutes byte-code
instruction.

JAVA編譯器(JAVAC)可將JAVA程式轉譯成中間碼(BYTE-CODE)
-JAVA直譯器可以瞭解的語言。如果程式編譯正確,編譯器會產生
一個副檔名為.CLASS的檔案。JAVA並不直接編譯為與平台相依的
原始機器指令,而是編譯為與系統無關的「位元碼、中間碼」
(bytecodes)。

JVM Java Virtual Machine JAVA虛擬機器
THE java interpreter is also caled the Java Virtual
Machine. JAVA為了能達到真正跨平台,於是JAVA在設計上便有
了JVM的產生。一般的程式語言,程式設計師寫出來的稱為原始碼
(Source Code),要編譯(Compile)成機器碼(Machine Code)才
能執行。 但Java的情況不同,Source Code編譯過的結果是
Byte Code,機器或是OS是看不懂的,要靠JVM在執行時解譯為
Machine Code,才能執行。也就是說, 對一般程式而言,程式本
身直接就接觸到OS或硬體資源層級,因此每遇到一種平臺,設計師
就必須針對不同的平臺做出適當的設計,但Java則不然, 程式本身
面對的是JVM,只要JVM看得懂你寫的程式,它就能順利執行。

2.Explain class, object 類別與物件

Java has two modules- methodd and classes.

類別是把事物的資料與相關的功能封在一起,形成的一種殊結構,
用以表達真實世界的一種抽象概念。一般來說類別是由「資料成員
」與「函數成員」封裝而成的,Java把資料成員稱為field(範疇
),函數成員稱為method。

物件利用類別來封裝(encapsulate)資料(attributes)和方法
(behaviors).

舉例來說:把class當作一棟房子的藍圖,其中包含房子的各種資料
與方法,而根據藍圖做出來的房子稱為物件。


資料來源 :
1.Walter Savitch, Absolute Java ,2/e, Addison Wesley, 04/08/2005
2.H.M Deitel & P.J Deitel ,JAVA HOW TO PROGRAM,5/e, Prentice Hall
3.http://www.eland.com.tw/javaland/2002_04/javatech_jvm.htm
4.http://caterpillar.onlyfun.net/Gossip/index.html



3. Let i=2;
Print i;
Print 2 * (i++);
Print i;

Ans: 2, 4, 3

--
public class Test2 {


public static void main(String[] args) {
int i=2;
System.out.println(i);
i = 2 * (i++);
System.out.println(i);
System.out.println(i-1);
}
}


--
4. Let i=2;
Print i;
Print 2 * (++i);
Print i;

Ans: 2, 6, 3

--
public class Test2 {


public static void main(String[] args) {
int i=2;
System.out.println(i);
i = 2 * (++i);
System.out.println(i);
System.out.println(i-3);
}
}

--
5. Let m=7, n=2;
Print (double) m/n;
Print m/ (double)n;

Ans: 3.5, 3.5

--
public class Test2 {


public static void main(String[] args) {
double m=7,n=2;
double a ;
a = m / n;
System.out.println(a);
System.out.println(a);
}
}


--

0 Comments:

張貼留言

<< Home

Creative Commons License
This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 2.5 Taiwan License.