Java (程式語言)

(由Java程式語言跳轉過嚟)
  想搵第二個意思嘅話,請睇Java

Java係一種可以撰寫跨平台應用軟件物件導向程式語言,由昇陽電腦占士哥斯連英文James Gosling響1990年代初開發,本來用響智能家庭電器度,但後尾發展成網絡程式語言。

Java
編程範式多重範型中文多重範型物件導向類別基礎中文類別基礎)、結構化指令式中文指令式泛型中文泛型反射式編程中文反射並發計算中文並發計算
設計者詹姆斯·高斯林中文詹姆斯·高斯林
太陽電腦系統中文太陽電腦系統
編程員甲骨文公司 (太陽電腦系統中文太陽電腦系統)
第一次出現1995年, 29年之前 (1995)[1]
穩定版本
Java SE 17.0.1 2021年10月19號, 3年之前 (2021-10-19)[2]
類型系統靜態、安全、強類型標明類型英文Nominal type system弱類型
軟件授權GNU通用公共許可協議JCP
副檔名.java、.class、.jar
網站java.com
Major 實作版本
OpenJDKGCJ其他
衍生變體
泛型Java英文Generic JavaPizza英文Pizza (programming language)
啟發語言
Ada 83C++C#[註 1]Eiffel[5]
Generic Java英文Generics in Java
Mesa[6]Modula-3[7]Oberon[註 2]Objective-C[註 3]UCSD Pascal[9][10]Object Pascal[註 4]
影響語言
Ada 2005BeanShellC#ClojureECMAScriptGroovyHack[12]HaxeJ♯JavaScriptKotlinPHPPythonScalaSeed7英文Seed7Vala

語言特性

編輯

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 ");
        }
	}
}

基本迴圈例子

編輯
import java.util.*;
import java.text.*; 
import java.math.*; 
public class grade {
	public static void main(String [] argv){
		int i,j=0;
		float mx=0,mn=100,av,a,sum=0;
		System.out.println("逐一輸入班級分數,欲中止請輸入-1");
        Scanner sa = new Scanner(System.in);
        for(i=0;;i++){
            a=sa.nextFloat();
            if(a==-1)break;
            sum=sum+a;
            if(a>=60)j++;
            if(a>mx)mx=a;
            if(a<mn)mn=a;
        }
        av=sum/i;
		NumberFormat ddf1=NumberFormat.getNumberInstance() ;
		ddf1.setMaximumFractionDigits(2); 
        System.out.println(" 班級分數統計");
        System.out.println("==============");
        System.out.println(" 最高分數 "+mx);
        System.out.println(" 最低分數 "+mn);
        System.out.println(" 全班人數 "+i);
        System.out.println(" 及格人數 "+j);
        System.out.println(" 平均分數 "+ddf1.format(av));
        System.out.println("\n==============");
	}
}
  1. Java 5.0 增加咗多種新特性(包括foreach循環自動裝箱、拆箱英文Object type (object-oriented programming)#Autoboxing可變參數函數以及annotations), after they were introduced in the similar (and competing) C# language.[3][4]
  2. Niklaus Wirth stated on a number of public occasions, e.g. in a lecture at the Polytechnic Museum, Moscow in September, 2005 (several independent first-hand accounts in Russian exist, e.g. one with an audio recording: Filippova, Elena (2005-09-22). "Niklaus Wirth's lecture at the Polytechnic Museum in Moscow". 原先內容歸檔喺2020-12-01. 喺2015-12-22搵到.), that the Sun Java design team licensed the Oberon compiler sources a number of years prior to the release of Java and examined it: a(relative)compactness, type safety, garbage collection, no multiple inheritance for classes – all these key overall design features are shared by Java and Oberon.
  3. Patrick Naughton cites Objective-C as a strong influence on the design of the Java programming language, stating that notable direct derivatives include Java interfaces(derived from Objective-C's protocol)and primitive wrapper classes.[8]
  4. In the summer of 1996, Sun was designing the Contact Yahoo Support precursor to what is now the event model of the AWT and the JavaBeans TM component architecture. Borland contributed greatly to this process. We looked very carefully at Delphi Object Pascal and built a working prototype of bound method references in order to understand their interaction with the Java programming language and its APIs.[11]

參考資料

編輯
  1. "The History of Java Technology". 原先內容歸檔喺2012-11-06. 喺2012-10-06搵到.
  2. https://www.oracle.com/java/technologies/javase-downloads.html
  3. "Java 5 catches up with C#". www.barrycornelius.com. 原先內容歸檔喺2011-03-19. 喺2006-11-30搵到.
  4. Lévénez, Éric. "Computer Languages History". www.levenez.com. 原著喺2006-01-07歸檔. 喺2006-11-30搵到.
  5. Gosling, James; McGilton, Henry (May 1996). "The Java Language Environment". 原先內容歸檔喺2014-05-06. 喺2015-12-22搵到.
  6. Gosling, James; Joy, Bill; Steele, Guy; and Bracha, Gilad. "The Java Language Specification, 2nd Edition". 原先內容歸檔喺2013-05-12. 喺2013-02-28搵到.{{cite web}}: CS1 maint: multiple names: 作者名單 (link)
  7. "The A-Z of Programming Languages: Modula-3". Computerworld.com.au. 原著喺2009-01-05歸檔. 喺2010-06-09搵到.
  8. "Java Was Strongly Influenced by Objective-C". cs.gmu.edu. 原先內容歸檔喺2011-07-13. 喺2013-02-28搵到.
  9. TechMetrix Research (1999). "History of Java" (PDF). Java Application Servers Report. 原著 (PDF)喺2010-12-29歸檔. 喺2015-12-22搵到. The project went ahead under the name "green" and the language was based on an old model of UCSD Pascal, which makes it possible to generate interpretive code
  10. "A Conversation with James Gosling – ACM Queue". Queue.acm.org. 2004-08-31. 原先內容歸檔喺2015-07-16. 喺2010-06-09搵到.
  11. White Paper About Microsoft's "Delegates"
  12. "Facebook Q&A: Hack brings static typing to PHP world". InfoWorld. 2014-03-26. 原先內容歸檔喺2015-02-13. 喺2015-01-11搵到.