Java入門第一個程式:Hello World!__Java

來源:互聯網
上載者:User

在編寫程式之前,推薦一款好用的線上編譯器:

https://www.shucunwang.com/RunCode/java/


代碼:

import java.io.*;class test  {public static void main (String[] args) throws java.lang.Exception{System.out.println("Hello World!");}}

運行結果:

Hello World!

 

分析:

(1) 這裡test是一個類,類代表一類對象的抽象集合。舉個例子來說,“人”是一個類,“張三”則是一個具體的對象。

 

(2) 在Java中,main()方法是Java應用程式的入口方法,也就是說,程式在啟動並執行時候,第一個執行的方法就是main()方法,這個方法和其他的方法有很大的不同,比如方法的名字必須是main,方法必須是public static void 類型的,方法必須接收一個字串數組的參數等等。

注意:Java語言中的方法跟C語言中的函數是一樣的,只不過叫法不一樣而已。

 

(3) System.out.println()的作用是在控制台輸出結果並換行。println代表print line。

Sytem.out.print()的作用是在控制台輸入結果,但是不換行。

 

比較下面兩個程式

(一)

import java.io.*;class test  {public static void main (String[] args) throws java.lang.Exception{System.out.println("Hello World!");System.out.println("123");System.out.println("hi");}}

運行結果:

Hello World!

123

hi

 

(二)

import java.io.*;class test  {public static void main (String[] args) throws java.lang.Exception{System.out.print("Hello World!");System.out.print("123");System.out.print("hi");}}

運行結果:

Hello World!123hi

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.