簡單的java程序,Java Hello World程序

 2023-11-19 阅读 23 评论 0

摘要:Whenever we start to learn a programming language, the first program is always to print the Hello World. In the last article, we learned how to install Java on Windows 10. Now we are ready to write and run our first Hello World Java program. 每當我們開始學

Whenever we start to learn a programming language, the first program is always to print the Hello World. In the last article, we learned how to install Java on Windows 10. Now we are ready to write and run our first Hello World Java program.

每當我們開始學習編程語言時,第一個程序始終是打印Hello World。 在上一篇文章中,我們學習了如何在Windows 10上安裝Java 。 現在,我們準備編寫并運行我們的第一個Hello World Java程序。

Java Hello World程序 (Java Hello World Program)

簡單的java程序。To keep things simple and working for a new user, here is the sample hello world program that you can use.

為了使事情變得簡單并為新用戶工作,這里是您可以使用的示例hello world程序。

public class JavaHelloWorldProgram {public static void main(String args[]){System.out.println("Hello World");}
}

Save above program as JavaHelloWorldProgram.java in any directory.

java小應用程序? 將以上程序另存為JavaHelloWorldProgram.java

1.編譯并運行Java Hello World程序 (1. Compile and Run Java Hello World Program)

Open Command Prompt and go to the directory where the hello world program file is saved. Then execute the below commands in order.

打開命令提示符,然后轉到保存hello world程序文件的目錄。 然后依次執行以下命令。

$javac JavaHelloWorldProgram.java$java JavaHelloWorldProgram
Hello World
Java 11 or higher, then you can simply execute Java 11或更高版本,則只需執行java JavaHelloWorldProgram.java and it will compile and execute the program for you. No need to explicitly compile and then run the java program.java JavaHelloWorldProgram.java ,它將為您編譯并執行程序。 無需顯式編譯然后運行Java程序。

2. Java程序要點 (2. Java Program important points)

  1. A Java source file can have multiple classes but only one public class is allowed.

    一個Java源文件可以有多個類,但只允許一個公共類。
  2. The java source file name should be same as the public class name. That’s why the file name of our program is JavaHelloWorldProgram.java

    Java源文件名應與公共類名相同。 這就是我們程序的文件名為JavaHelloWorldProgram.java
  3. When we compile the code, it generates byte code and save it as Class_Name.class extension. If you look at the directory where we compiled the java file, you will notice a new file created JavaHelloWorldProgram.class

    當我們編譯代碼時,它將生成字節代碼并將其另存為Class_Name.class擴展名。 如果查看我們編譯Java文件的目錄,您會注意到創建了JavaHelloWorldProgram.class的新文件
  4. When we execute the class file, we don’t need to provide complete file name. We need to use only the public class name.

    當我們執行類文件時,我們不需要提供完整的文件名。 我們只需要使用公共類名。
  5. When we run the program using java command, it loads the class into JVM and looks for the main method in the class and runs it. The main function syntax should be same as specified in the program, else it won’t run and throw exception as Exception in thread "main" java.lang.NoSuchMethodError: main.

    當我們使用java命令運行程序時,它將類加載到JVM中并在類中查找main方法并運行它。 主要函數語法應與程序中指定的語法相同,否則它將不會運行并引發異常,因為Exception in thread "main" java.lang.NoSuchMethodError: main

Java開發?I have recently created a short video for Java Hello World Program using Notepad and then Eclipse. Watch it for a better understanding.

我最近使用記事本和Eclipse為Java Hello World程序創建了一個簡短的視頻。 觀看它以獲得更好的理解。

演示地址

hello world動畫,That’s all for this post and you can start playing with your first class. In the next post, I will get into further details of classes, JDK, JVM, and other features provided by the Java programming language.

這就是這篇文章的全部內容,您可以開始上一堂課。 在下一篇文章中,我將進一步探討類,JDK,JVM和Java編程語言提供的其他功能的詳細信息。

Update: Read this post to know about JDK vs JRE vs JVM in java.

Java程序設計、 更新:閱讀這篇文章,以了解Java中的JDK,JRE和JVM 。

翻譯自: https://www.journaldev.com/481/java-hello-world-program

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/183189.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息