Java学习笔记(1)——HelloWorld!

文件名需与类名一致

  1. Java保存的文件名必须与类名一致;
  2. 如果文件中只有一个类,文件名必须与类名一致;
  3. 一个Java文件中只能有一个public类;
  4. 如果文件中不止一个类,文件名必须与public类名一致;
  5. 如果文件中不止一个类,而且没有public类,文件名可与任一类名一致。

我的第一个Java程序:Hello World!

Linux环境下:

创建文件 HelloWorld.java

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

注:String args[] 与 String[] args 都可以执行,但推荐使用 String[] args,这样可以避免歧义和误读。

运行以上实例,输出结果如下:

$ javac HelloWorld.java
$ java HelloWorld
Hello World

执行命令解析:
以上我们使用了两个命令javacjava

javac 后面跟着的是java文件的文件名,例如 HelloWorld.java。 该命令用于将* java* 源文件编译为 class 字节码文件,如: javac HelloWorld.java

运行javac命令后,如果成功编译没有错误的话,会出现一个 HelloWorld.class 的文件。

java 后面跟着的是java文件中的类名,例如 HelloWorld 就是类名,如: java HelloWorld

注意:java命令后面不要加.class

Windows环境下使用eclipse

以下来自eclipse官方教程

Create a Hello World application
This cheat sheet shows you how to create the famous “Hello World” application and try it out. You will create a Java project and a Java class that will print “Hello world!” in the console when run.
If you need help at any step, click the (?) to the right. Let’s get started!

Open the Java perspective
If you’re not already in the Java perspective, in the main menu select Window > Open Perspective > Java or click on the “Click to Perform” link below.

Create a Java project
Before creating a class, we need a project to put it in. In the main toolbar, click on the New Java Project button, or click on the link below. Enter HelloWorld for the project name, then click Finish.

Create a Java Class
The next step is to create a new class. In the main toolbar again, click on the New Java Class button (or the link below). If not already specified, select HelloWorld/src as the source folder. Enter HelloWorld for the class name, select the checkbox to create the main() method, then click Finish.
The Java editor will automatically open showing your new class.

Add a print statement
Now that you have your HelloWorld class, in the main() method, add the following statement:
System.out.println(“Hello world!”);
Then save your changes; the class will automatically compile upon saving.

Run your Java Application
To run your application, right-click on your class in the Package Explorer and select Run As > Java Application. The Console view should appear at the bottom and display the “Hello, world!” output.
Congratulations! You have successfully created a Hello World application!

创建一个Hello World应用程序
这个“小抄”向你展示如何创建著名的“Hello World”应用程序, 去尝试一下吧。 你将创建一个Java项目和Java类,在控制台运行时来打印“Hello world!” 。
如果您需要任何步骤的帮助,请点击右侧的(?)。 让我们开始吧!

打开Java视图
如果你还没有在Java视图中,请在主菜单中选择“窗口”>“打开视图”>“Java”,或单击下面的“点击执行”链接。

创建Java项目
在创建一个类之前,我们需要一个项目来放置它。在主工具栏中,单击New Java Project按钮,或单击下面的链接。 为项目名称输入HelloWorld,然后单Finsh

创建Java类
下一步是创建一个新类。 再次回到主工具栏中,单击New Java Class按钮(或下面的链接)。 如果尚未指定,请选择HelloWorld / src作为源文件夹。 为类名输入HelloWorld,选中复选框以创建main()方法,然后单击Finish
Java编辑器将自动打开显示你的新类。

添加打印语句
现在你拥有了你的HelloWorld类,在main()方法中添加以下语句:
System.out.println(“Hello world!”);
然后保存您的更改; 该类将在保存时自动编译。

运行你的Java应用程序
要运行应用程序,请在包资源管理器中右键单击您的类,然后选择Run As > Java Application。 控制台视图应显示在底部,并显示“Hello,world!”输出。
恭喜! 您已经成功创建了一个Hello World应用程序!

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值