UtterAccess.com
X   Site Message
(Message will auto close in 2 seconds)

Welcome to UtterAccess! Please ( Login   or   Register )

Custom Search
 
   Closed TopicStart new topic
> How to get started in Java.    
 
   
freakazeud
post Jun 15 2007, 03:45 PM
Post#1


UtterAccess VIP
Posts: 31,413
Joined: 23-September 04
From: NC, USA


This is a small tutorial to get you started with Java.

The first thing we need if you don’t already have it is the JDK (Java Development Kit). Download the latest version from Sun’s website (http://java.sun.com/javase/index.jsp) and install it. Follow documentation instruction if you have problems with the installation.

After the installation is complete you are basically ready to start coding. For purpose of understanding the workings behind Java I will quickly go over how to write code without a specific IDE (Integrated Development Environment) and only by using the build in JDK command prompt components.

You can start writing source code in any text editor e.g. NotePad and save the file with a .java extension. As part of the JDK you could now use the Java compiler (javac) to compile your source code into byte code so that the JVM (Java Virtual Machine) can execute it. After compilation you would receive a file with a .class extension which is runnable by the JVM by providing the class file name (without extension) to the java command prompt.

However writing code in this manner is very time consuming and intimidating. As an example things like debugging errors or version controlling are a nightmare in text editors and constantly working with the command prompt option doesn’t make life easy either.

I would strongly recommend getting familiar with some IDE which gives you a GUI (Graphical User Interface) to work with. One of my favorite open source (free) IDE tool is Eclipse. You can download it from their website http://www.eclipse.org/downloads/index.php .

It works flawlessly in combination with Java and there are thousands of neat plug-ins (e.g. UML editors…) available, which can expand its functionality beyond everything that is already included (e.g. Workbench, Debugger, Junit testing, Concurrent Versions System (CVS)…).

Now that you have everything installed we can code our first application. It will take a little while for you to get familiar with the vast amount of features offered by Eclipse. However if you play around with it for a while you should get the hang of it pretty quickly. Furthermore, you could go through the provided documentation/samples.

The following instructions might vary based on OS/Eclipse version. When you open Eclipse for the first time you should see a welcome screen with about 5 round logo buttons. Since we want to start coding we can go directly to the workbench (arrow button). This will take you to the main development environment. If you now close and reopen eclipse it should always directly go into the workbench for you. If you want to return to the welcome screen go to Help--Welcome.

On the left side you should see the Package Explorer, on the right the Java perspective for the code and on the bottom the Consol and some other at the moment not too important things. To start a new Java project go to File--New--Project…In the new project wizard select java project and click next…give the project a name e.g. FirstProject, leave all other options on that screen as default and click Finish. You should now see a new folder in the Folder Explorer with the name of your new project.

Now we need to add a class to it so we can start writing our code. Select the new project folder and go to File--New--Class (these options are also available on the right click shortcut menu). Give the class a name. By convention a class name should be a noun with mixed cases where the first letter of each new word is capitalized e.g. SampleProject. Leave all other options as default but check the public static void main(String[] args) option under Which method stubs would you like to create? Click finish and Eclipse should add our class to the FirstProject folder and open it up in the Java perspective where you should see something similar to this:
CODE

ublic class SampleProject {
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
    }
}

The main method you are seeing was creating for us because of the option we selected earlier in the class wizard. In case you are not too familiar with Java…each Java program requires a main method of the above signature. The main method can be in any class within the project as long as it exists. It basically is the starting point for program execution.

If you would compile this code and call it as earlier mentioned from the command prompt the JVM would look for the main method and start the execution there. The only parameter this method takes is a String array of arguments which you could pass a long through the command prompt. When executing the program through Eclipse you can specify these arguments through Run--Run…(x)=Arguments.

Since we just want to get familiar with our development environment we will just add a couple lines of code into the main method e.g.:
CODE

public static void main(String[] args) {
    
    int x = 0;
    
    System.out.println(“Hello World!”)
        x = x + 1;
    System.out.println(“You are following an UtterAccess Tutorial!”)
        System.out.println(“x is now: “ + x);
}

If you go to Run--Run As--Java Application you should see the following output in the Consol: <
Congratulations…you just finished your first Java program. While typing the code you might have recognized IntelliSense giving you certain options. By default a new Java class includes the java.lang package which provides fundamental operations for you. Once you get more familiar with Java you can expand functionality by importing many other packages which let you for example ask for user input, manipulate external files, allow multi threading, GUI implementations…If you search the Java API (http://java.sun.com/j2se/1.5.0/docs/api/) you can browse through all available options.

This finishes our little Java/Eclipse introduction. Feel free to post any questions if you get stuck with doing anything. For informational purposes you might find any of the following links interesting and helpful:

Java Tutorials:
http://java.sun.com/docs/books/tutorial/

Run Java programs from command prompt:
http://java.sun.com/docs/books/tutorial/getStarted/TOC.html

Eclipse Tutorials:
http://eclipsetutorial.forge.os4os.org/in2.htm

https://eclipse-tutorial.dev.java.net/

Java/Eclipse Documentation:
http://java.sun.com/reference/docs/

http://www.eclipse.org/documentation/

thanks.gif
Go to the top of the page
 


Custom Search
RSSSearch   Top   Lo-Fi    14th December 2017 - 12:07 PM