advertisement
javaboutique
Search Tips
Articles  |   Tutorials  |   Reviews  |   Tools  |   by Category  |   by Date  |   by Name  |   Submit  |   Source  |   Forums  |  
javaboutique
Browse DevX


Partners & Affiliates











advertisement

Tutorials : Making the Switch to Java :

Graeme Kerry is a first year computer systems engineering student from the UK, studying at Lancaster University. He has been programming in Java for about 3 years. Outside of studies he enjoys sports and socialising with friends. Kezz's Java Tutorials are written in Graeme's spare time.

Part 1 HelloWorld.java

Introduction

Welcome to the first of my introduction to Java tutorials. These tutorials are aimed at complete beginners to programming. Java is a good language to start with because its syntax is very similar to the more powerful c++ and it has been designed in a very object oriented way. Object oriented programming (or OOP for short) is a way of programming that breaks the problem down into parts. These parts then slot together to form the solution.

What You Will Need

To follow these tutorials all you need to get your hands on is the Java SDK (sometimes called the JDK). SDK stands for Software Development Kit. The Java SDK is available free!! from here . This kit contains all you need to program in java. To write your source files I recommend using a program such as notepad or Xemacs (under *NIX environments). These are just suggestions and any plain text editor can be used as long as it can save files in raw text format.

Main Parts of the Java SDK

The SDK contains many programs and is quite a hefty download. The main parts you will be using are detailed below.

    • javac.exe - This is the java compiler. It turns your source code into files that can be read by the java interpreter.
    • java.exe - This is the java interpreter. It reads through you compiled code line by line and executes the statements.
    • appletviewer.exe - This will be used later on in the tutorials when we get into applet programming. This tool displays an applet as it would appear on an html page. (An applet is a java program that can run in a web browser).

 

HelloWorld.java

Ok enough explanation, lets start coding! This is the most basic of programs and all programming tutorials and courses I've seen seem to start with it.

Fire up your text editor and enter the code listed below and save it as HelloWorld.java.

public class HelloWorld {

public static void main (String [] args) {

System.out.println("Hello World!!!");

}

}

After saving this file open an ms-dos command box (Start->Run->command.com should do the trick, on newer versions of Windows try cmd.exe) or a *NIX terminal and change to the directory where you saved the source file. To compile a java source file we use javac, the java compiler. So to compile out HelloWorld.java program execute the following statement:  javac HelloWorld.java. If you copied the code exactly right as shown above the compiler won't say a thing and you'll just end up with a new command prompt. This is good!!! Now to run your program use the java interpreter (java.exe) by executing java HelloWorld. The computer now reads your file and should print "Hello World!!!" before leaving you at a fresh command prompt. Thats all there is to it. I realise this isn't very impressive but its essentially one line of code!.

Lets take a closer look at the source code.

public class HelloWorld {

This line of code defines a new java class called HelloWorld. A class is a java program. Note the capitalisation of the HelloWorld. To make this file compile it must be stored in a file called HelloWorld.java with exactly the same name (capitalisation etc). The { at the end signifies the start of a block of code in java and as you can see on the last line this block is ended with a }

public static void main (String [] args) {

This is the main part of the program. It is called the main method. This method is always the first thing that is executed in a java application so everything in here is very important. Once again a { must be used to start the main method block. This means that everything that follows between the { and } belongs to the main method.

System.out.println("Hello World!!!");

This line is a call to the println method of the System.out class. Don't worry about the technicalities, just remember that to print something to the screen you use System.out.println("<what you want to print>"); The speechmarks indicate that you want to print a string of text. This means that java will print exactly what is in the speechmarks.

Rounding Off

Ok thats all for this time. I hope you've been able to follow it. The next tutorial will be on getting user input and probably types of variable but lets worry about that next time!! If in the meantime you have any problems you can contact me @ graeme.kerry@btinternet.com and I will try to answer your questions.

 

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.

XML error: undefined entity at line 19
advertisement
Receive Articles via our XML/RSS feed
Receive Articles via our XML/RSS feed

JavaBytes
Internet Cyclone
This powerful, easy-to-use, internet optimizer is for Windows 95, 98, ME, NT, 2000 and XP. It's designed to automatically optimize your Windows settings, boosting your Internet connection up to 200%.

Linux Vendors Head to the Cloud in Search of Cash
iPhone 3GS: Overheating Fears, OS Update Nears
PostgreSQL 8.4 Revs Up Database Admin, Security
PHP 5.3 Accelerates PHP
Sun Releases NetBeans 6.7 IDE for Java, PHP
Why Firefox Doesn't Take Google Chrome Features
First Major PHP Update in Years Coming Soon
Red Hat CEO Calls on Oracle to Keep Java Open
Google Widens AdSense for iPhone, Android Apps
Eclipse Galileo Releases 33 Open Source Projects

A Taste of JavaFX for the Uninitiated
A Guide to Caching and Compression for High Performance Web Applications
How User-Centered Design Can Put User Stories in Proper Context
Explore C# 4's New Dynamic Types and Named/Optional Parameters
Enterprise Architecture: The Journey Begins Here, Part 2
Create a Syslog Sender/Receiver Using the MS Winsock Control
AMD CodeAnalyst Helps Developers Optimize and Tune Applications
Securing Microsoft's Cloud Infrastructure
Introducing the Azure Services Platform
An Introduction to Microsoft .NET Services for Developers

Advertising Info  |   Member Services  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About

internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Shopping | E-mail Offers | Freelance Jobs