Java 101 : Hello World

Editor's Note :  When I first wrote this tutorial series, in late 1996, Java was a relatively new language. It had a lot of potential, but a relatively small following by today's standard. In rewriting this tutorial series, I was amazed at how things had changed. Java is now recognized as an excellent object orientated language, is used widely in commercial development and educational institutions. Still, many things have remained the same. Its speed, for example, is still a problem for commercial software applications - though this too is changing, and perhaps when I look back in two more years time, such problems will be rectified.

-- David Reilly, May 1999


Java quickly became a hot buzzword of the computing industry. People wanted to know Java - it was said to be great for creating dynamic interactive content for webpages. Yet the true power of Java lies not in applets, but in its many other uses. Java is used for developing standalone applications, and for server-side programming. The face of Java has changed, but the core language remains the same. In this tutorial series, I'll teach you the basics of Java programming. You'll still need a good book as a companion to this tutorial series, but for those who are dabbling in Java, this should be enough to get your feet wet.

Before I begin to cover the basics of Java programming, I should point out that Java is an object-orientated language, and may not be suitable for first time programmers. Learning a new language takes some time, but learning your first object-orientated language can be exceedingly difficult. Nonetheless, if you've done some C programming before, the shift into Java shouldn't be unreachable, providing you obtain a good reference book. There are also those that believe programmers should start with an object-orientated language first, and many universities have adopted this practice. Still, you've been fairly warned :)

This tutorial will presume that you have some basic programming knowledge, particularly in C, as it will not be covering such principles as sequence, selection and repetition. If you are unsure on ' for ' loops, or complex ' if ' statements, I'd suggest coming back here at a later point.

Application or Applet?

Java software comes in several flavors - the most common being the stand-alone application, and the applet. Web developers may have come across the term applet before, and perhaps even used one. An applet is an piece of software code that runs under the control of a web browser, as distinct from the application which requires an interpreter.

Applets are commonly used to enhance the interactivity of a web page, and deliver client-side content. Applets run in their own frame, and can display graphics, accept input from GUI components, and even open network connections. Due the potential security risks associated with running applets from external and potentially malicious sources, most web browsers limit file access, and impose additional restrictions on applets (such as only being able to connect to the hostname from which the applet was downloaded).

Fortunately, stand-alone applications have no such restrictions, and a full range of functionality is provided for in the way of pre-written Java classes. Stand-alone applications can run as a console application (writing text to the screen or terminal window), or they can have a graphical user-interface, by opening a new window or dialog box. You've used applications before, such as word processors, text editors, and games. The Java language is capable of all this things.

Since stand-alone applications offer more freedom to the programmer, and applets running under a browser often demonstrate a certain degree of instability depending on the platform under which it is run, this tutorial series will concentrate primarily upon the stand-alone application.

The first thing required for writing stand-alone Java applications is a java compiler/interpreter. While there are commercial offerings available, such as Visual J++ and Borland JBuilder, a freely available SDK is available from Sun, the original creators of the Java language. It contains a compiler, interpreter, debugger, and more. I highly recommend using the latest version of Sun's Java Development Kit (JDK). You should download the JDK from http://java.sun.com.

After downloading and installing the Sun Java SDK for your required platform (Windows 95, NT, or Solaris), its time to write, compile, and run your first Java application - the obligatory "Hello World".


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

Listing 1.0 - myfirstjavaprog.java

 


The source file above should be saved as myfirstjavaprog.java, using any standard text editor capable of saving as ASCII (eg - Notepad, Vi). As an alternative, you can download the source for this tutorial.

To compile your first java application, enter the following (assuming that the java directory is in your path) :

  javac myfirstjavaprog.java

Javac is a compiler included with Sun's JDK. It translates the source file into Java byte-codes. While Java is an interpreted language, it is reduced into byte-codes which are interpreted by a Java virtual machine (in much the same way assembly language / machine code for older computing systems can be executed by emulator software). The compiler stores these byte-codes in a ' .class ' file. To execute the application, the Java interpreter will run this ' .class ' file.

  java myfirstjavaprog

If everything goes according to plan, the message "Hello World!", followed by a newline should appear on your terminal/screen. You've just compiled and executed your first application.

How it works

For those new to object-orientated programming, the concept of a class will be new to you. We defined a new class, called myfirstjavaprog. Simplistically, a class is the definition for a segment of code that can contain both data (called attributes) and functions (called methods).

When the interpreter executes a class, it looks for a particular method by the name of main, which will sound familiar to C programmers. The main method is passed as a parameter an array of strings (similar to the argv[] of C), and is declared as a static method (more on this in a later tutorial).

To output text from the program, we execute the ' println ' method of System.out, which is Java's output stream. Unix users will appreciate the theory behind such a stream, as it is actually standard output. For those who are instead used to the Wintel platform, it will write the string passed to it to the user's screen.

That wraps it up for this first part of the introduction to Java tutorial series. In the next tutorial, we'll cover some more object-orientated principles, and extend your knowledge of the Java language and syntax.

Spring4GWT GWT Spring 使得在 Spring 框架下构造 GWT 应用变得很简单,提供一个易于理解的依赖注入和RPC机制。 Java扫雷游戏 JVMine JVMine用Applets开发的扫雷游戏,可在线玩。 public class JVMine extends java.applet.Applet 简单实现!~ 网页表格组件 GWT Advanced Table GWT Advanced Table 是一个基于 GWT 框架的网页表格组件,可实现分页数据显示、数据排序和过滤等功能! Google Tag Library 该标记库和 Google 有关。使用该标记库,利用 Google 为你的网站提供网站查询,并且可以直接在你的网页里面显示搜查的结果。 github-java-api github-java-api 是 Github 网站 API 的 Java 语言版本。 java缓存工具 SimpleCache SimpleCache 是一个简单易用的java缓存工具,用来简化缓存代码的编写,让你摆脱单调乏味的重复工作!1. 完全透明的缓存支持,对业务代码零侵入 2. 支持使用Redis和Memcached作为后端缓存。3. 支持缓存数据分区规则的定义 4. 使用redis作缓存时,支持list类型的高级数据结构,更适合论坛帖子列表这种类型的数据 5. 支持混合使用redis缓存和memcached缓存。可以将列表数据缓存到redis中,其他kv结构数据继续缓存到memcached 6. 支持redis的主从集群,可以做读写分离。缓存读取自redis的slave节点,写入到redis的master节点。 Java对象的SQL接口 JoSQL JoSQL(SQLforJavaObjects)为Java开发者提供运用SQL语句来操作Java对象集的能力.利用JoSQL可以像操作数据库中的数据一样对任何Java对象集进行查询,排序,分组。 搜索自动提示 Autotips AutoTips是为解决应用系统对于【自动提示】的需要(如:Google搜索), 而开发的架构无关的公共控件, 以满足该类需求可以通过快速配置来开发。AutoTips基于搜索引擎Apache Lucene实现。AutoTips提供统一UI。 WAP浏览器 j2wap j2wap 是一个基于Java的WAP浏览器,目前处于BETA测试阶段。它支持WAP 1.2规范,除了WTLS 和WBMP。 Java注册表操作类 jared jared是一个用来操作Windows注册表的 Java 类库,你可以用来对注册表信息进行读写。 GIF动画制作工具 GiftedMotion GiftedMotion是一个很小的,免费而且易于使用图像互换格式动画是能够设计一个有趣的动画了一系列的数字图像。使用简便和直截了当,用户只需要加载的图片和调整帧您想要的,如位置,时间显示和处理方法前帧。 Java的PList类库 Blister Blister是一个用于操作苹果二进制PList文件格式的Java开源类库(可用于发送数据给iOS应用程序)。 重复文件检查工具 FindDup.tar FindDup 是一个简单易用的工具,用来检查计算机上重复的文件。 OpenID的Java客户端 JOpenID JOpenID是一个轻量级的OpenID 2.0 Java客户端,仅50KB+(含源代码),允许任何Web网站通过OpenID支持用户直接登录而无需注册,例如Google Account或Yahoo Account。 JActor的文件持久化组件 JFile JFile 是 JActor 的文件持久化组件,以及一个高吞吐量的可靠事务日志组件。 Google地图JSP标签库 利用Google:maps JSP标签库就能够在你的Web站点上实现GoogleMaps的所有功能而且不需要javascript或AJAX编程。它还能够与JSTL相结合生成数据库驱动的动态Maps。 OAuth 实现框架 Agorava Agorava 是一个实现了 OAuth 1.0a 和 OAuth 2.0 的框架,提供了简单的方式通过社交媒体进行身份认证的功能。 Eclipse的JavaScript插件 JSEditor JSEditor 是 Eclipse 下编辑 JavaScript 源码的插件,提供语法高亮以及一些通用的面向对象方法。 Java数据库连接池 BoneCP BoneCP 是一个高性能的开源java数据库连接池实现库。它的设计初衷就是为了提高数据库连接池的性能,根据某些测试数据发现,BoneCP是最快的连接池。BoneCP很小,只有四十几K
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值