internet.com

Go to WebDeveloper Home


hm-v6-139x61.gif

Database Journal: The Knowledge Center for Database Professionals

find a web host with:
CGI Access
DB Support
NT Servers
UNIX Servers
Telnet Access

advanced search
jobs

Get FREE Development Info via your Email!

The Webdeveloper channel
FlashKit
GIF.com
HiermenusCentral
webdeveloper Jobs
Java Boutique
JavaScript.com
JavaScript Source
ScriptSearch
StreamingMedia World
WDJ
WDVL
WebDeveloper.com
WebReference.com
XMLFiles.com

internet.com
Internet News
Internet Investing
Internet Technology
Windows Internet Tech.
Linux/Open Source
Web Developer
ECommerce/Marketing
ISP Resources
ASP Resources
Wireless Internet
Downloads
Internet Resources
Internet Lists
International
EarthWeb
Career Resources

Search internet.com
Advertise
Corporate Info
Newsletters
E-mail Offers

internet.commerce
Be a Commerce Partner










J A V A
WebDeveloper.com

Java Starter

By Thomas Ko

So you don't know anything about Java, but you've heard so much about it you've decided it might not be that bad to learn. Or you're just a freelance programmer itching at the chance to flex your coding muscles and tackle the hottest new language in the market. Or you can't care less about Java and the only reason why you reading this is that you're forced the use it at school. Whatever reason you may have, if you ever wanted to learn Java, here's your chance.

In this tutorial and the many that will follow afterwards, I will guide you through the basics of Java and give you a slew of examples to aid in your understanding and also to help familiarize yourself with the Java syntax. I also recommend grabbing a Java IDE such as Java Workshop from Sun or Visual Age from IBM so that you can try out some of the code yourself. If you prefer the command line Unix programming or just using a text editor, make sure you grab JDK v1.2 to compile your code. After all, the fastest way to learn a language is to do some programming yourself.

First off, Java is an Object-Oriented programming language. Anyone who has used C, C++ or other high-level languages will be familiar with this type of programming. If you are not, no big deal. I won't be going in depth into that, but I would recommend those who do have a keen interest in programming or Computer Science to read up on that, since it is the foundation of high-level programming. Anyhow, Java being an OO language requires the use of classes, objects, methods and variables. The best way to think of their relationships is that a class contains everything you need to know about the object including all the things it can do. Methods and variables are contained inside the class. Methods define what the object can do and are also used to manipulate it. Variables are parts or specs of the object. If you find that hard to grasp, consider an object in real world terms such as a car. The car will then be an object and the Car class will c ontain everything you need to know about the car including things to manipulate it. Things you can do with the car (ie. tuneup, drive) will be the methods. As well, the max speed and distance travelled will be variables.

public class Car {
  public double kilometres;
  public int speed;

  public Car() {
    kilometres = 0.0;
    speed = 150;
  }

  public void tuneUp() {
    speed += 25;
  }

  public int maxSpeed() {
    return speed;
  }

  public void drive( double distance ) {
    kilometres += distance;
  }

  public void showSpecs() {
    System.out.println("Max Speed = " + speed + "km/h");
    System.out.println("Distance Travelled = " + kilometres + "km");
  }
}

In this case, the class Car defines the Car object. Inside the class you have variables such as kilometres and speed (don't worry about the other stuff in front for now) and you also have methods like tuneUp() and maxSpeed() that gives you information about Car.

Now to create a Car object, you type: Car lexus = new Car();

The general syntax is: <object name> name = new <object name>(); where "name" can be anything you want. In the above example, lexus is the same as name. When the above line of code is executed, you have just created a new Car object with the name lexus. What it actually does is declare lexus as a Car object (left side of equal sign) and run the Car() method to create a new Car object (right side of equal sign). This method is known as the Constructor since it creates Car objects. Now lexus has all of Car's methods (ie. tuneUp(), drive()) and variables (ie. kilometres and speed). To access these methods and variables with lexus, use the following dot notation:

lexus.speed; (for variables)
lexus.tuneUp(); (for methods)

Type in these lines of code will access the speed variables and run the tuneUp() method. For a better understanding on how these things work here's another class that will perform the various functions of the Car object. This type of class is referred to as the main class or driver class since all it does is run the program. I will be using "//" to precede comments about the code. The "//" tells Java that they are comments and not code.


public class Main {

  public static void main( String[] args ) {

    //Create a Car Object with the name lexus.
    //It has the attributes speed=150 and
    //kilometres=0.0.

    Car lexus = new Car();

    //Increase speed by 25.
    lexus.tuneUp();

    //Increase kilometres by 74.7.
    lexus.drive(74.7);

    //Sets the local variable max to the value
    //returned by maxSpeed(). In this
    //case max=175.

    int max = lexus.maxSpeed();

    //Print to screen.
    lexus.showSpecs();
  }
}

After compiling and running this code you should see the output:

Max Speed = 175 km/h
Distance Travelled = 74.7 km

As you can see only the lexus.showSpecs() line produced any output. That's because the rest of the program happens in the background. The user only sees the lines sent to output and these are defined by the "System.out.print" command. Now you're ready to write a few of your own methods. Try writing one that returns kilometres or one that finds the total time it takes to travel a certain distance at a certain speed. (*Hint* time = distance/speed). Then run those methods by adding the appropriate lines in the Main class.

So you're still wondering what the heck that int and double stuff in front of the variables meant, aren't ya? Well, read on to the next section to find out...

[ Click here to move to the next part of the article ]

This article first appeared May 31, 1999.

Fast Jump to Anywhere on WebDeveloper.com®:


Contact the WebDeveloper.com® staff

Last modified: 20

 

Refresh Daily
Join Editor-in-Chief David Fiedler The Editor With No Time and find truth, justice, and a clue or two.


Browse by Category
[ Site Map ]

ActiveX / VBscript
Animated GIF Archive
Browsers
CGI / Perl
Database Connectivity
Design / Graphics
E-Commerce
HTML-Advanced: DHTML, CSS
HTML / Site Authoring Tools
Intranet/Groupware
Java
JavaScript
Multimedia: Audio / Video / Streaming Technologies
Opinions
Refresh Daily: Editorial Column
Security
Servers & Server Tools
Site Design / Graphics
Site Management / Marketing / Log File Analysis
Tutorials
VRML / 3D
XML


internet.commediabistro.comJusttechjobs.comGraphics.com

Search:

WebMediaBrands Corporate Info

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