軟體工程師手冊/語言詞典/Java
外觀
(重定向自 軟體工程師手冊/語言詞典/面向物件/Java)
這是 Java維基百科條目.
Java 是一種完整的、過程式的、面向物件的語言。
public static void main(String args[])
{
// some functionality here
}
典型的語句以分號結束。將 b 賦值給 a,使用
a = b;
// this is an inline comment. Everything after the // is a comment.
塊註釋由起始 /* 和結束 */ 指定,可以跨越多行。
/*
* this is a block comment
*/
int x = 9;
Integer y = new Integer(4);
// declaration
private return_type class_name::function_name(argument_1_type arg_1_name,
argument_2_type arg_2_name,
default_argument_type default_arg_name)
{ // implementation
// work with arg_1_name, arg_2_name, and default_arg_name
// depending on the argument types the variables are passed by
// value, reference, or are constant
// don't forget to return something of the return type
return 36;
}
作用域由花括號定義。
{ // this the beginning of a scope
// the scope is about to end
}
當且僅當 A 等於 B 時,將 C 賦值給 D,否則,將 E 賦值給 F。
if( A == B )
{
D = C;
// more code can be added here. It is used if and only if A is equal to B
}
else
{
F = E;
// more code can be added here. It is used if and only if A is not equal to B
}
或者
if( A == B )
D = C; //more lines of code are not permitted after this statement
else
F = E;
或者,可以使用 switch 語句進行多重選擇操作。此示例將數字輸入轉換為文字。
switch( number_value )
{
case 37:
text = "thirty-seven";
break; // this line prevents the program from writing over this value with the
// following code
case 23:
text = "twenty-three";
break;
default: // this is used if none of the previous cases contain the value
text = "unknown number";
}
此程式碼從 0 到 9 計數,累加陣列中的內容。
int i = 0;
for( int index = 0; index < 10; index = index + 1 )
{
i = array[index];
}
此程式碼重複執行,直到找到數字 4。如果此程式碼超出陣列末尾,則可能存在問題。
int index = 0;
while( 4 != array[index] )
{
index = index + 1;
}
此程式碼在進行檢查之前遞增計數器,因此它從元素 1 開始。
int index = 0;
do
{
index = index + 1;
}
while( 4 != array[index] );
System.out.println( "Hello World!" );
容器繼承自 Collection 類。請參閱 java.util 包以獲取特定容器,包括 List、LinkedList、Queue、Stack、Dictionary 和 HashMap。
Collection 類具有 sort 等演算法。
垃圾回收是自動的。
程式碼通常儲存在副檔名為 .java 的檔案中。它被編譯成 Java 位元組碼,並存放在副檔名為 .class 的檔案中。
- Java 包中的類是首字母大寫的,方法不是。
- 一切都是指標。使用克隆方法來避免操作 Collection 的原始元素。
- 陣列從索引 0 開始。
- 不要混淆這兩個
= // assignment == // comparison, is equal to
通常,使用你不想要的那個會編譯,並且會產生你沒有預期的結果。
- Java 華夏公益教科書頁面
- The Sun java page 包含 API 線上以及程式碼示例。
紙質參考文獻在此處