Java (程式語言)
閱讀設定
![]() |

Java係一種可以撰寫跨平台應用軟件嘅物件導向嘅程式語言,由昇陽電腦嘅占士哥斯連響1990年代初開發,本來用響智能家庭電器度,但後尾發展成網絡程式語言。
語言特性
Java之所以被開發,係要達到以下五個目的:
- 應當使用物件導向程式設計方法學
- 應當允許同一程式喺唔同嘅電腦平台執行
- 應當包括內建嘅對電腦網絡嘅支援
- 應當被設計成安全地執行遠端程式碼
- 應當易於使用,並借鑑以前嗰啲物件導向語言(例如C++)嘅長處。
Java技術主要分成幾個部分:Java語言、Java執行環境、類別庫。一般情況下說Java時並唔區分指嘅係邊個部分。
Java喺1.5版本時,做咗重大改變,Sun公司並將1.5版本重新命名為Java 5.0。
例子:出「Hello World」
public class HelloWorld {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
基本條件例子
public class IF {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int i=sc.nextInt();
if(i>=1)
{
System.out.println("Hello, World!");
}
}
}
public class IfElse {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int i=sc.nextInt();
if(i>=1)
{
System.out.println("Hello, World! \n And i >= 1. ");
}
else
{
System.out.println("Hello, World! \n But i < 1.");
}
}
}
public class SwitchCase {
public static void main(String[] args) {
Scanner sc =new Scanner(System.in);
int i=sc.nextInt();
switch(i)
{
cass 1:
System.out.println(" i = 1 ");
break;
cass 2:
System.out.println(" i = 2 ");
break;
cass 3:
System.out.println(" i = 3 ");
break;
cass 4:
System.out.println(" i = 4 ");
break;
cass 5:
System.out.println(" i = 5 ");
default:
System.out.println(" i >= 5 ");
}
}
}