Showing posts with label Java Tutorial. Show all posts
Showing posts with label Java Tutorial. Show all posts

Getting started with Java in NetBeans


Getting started with Java in NetBeans

We assume that you've obtained a JDK and IDE for your machine: these are the things you need to get started programming Java. In this guide, we'll assume that you're using the NetBeans IDE, which simplifies certain aspects of getting started for absolute beginners. However, if you have chosen another IDE such as Eclipse, it shouldn't matter terribly. Much of the principles are the same, but some of the dialog boxes you will see in Eclipse have extra options which you can generally ignore for the time being.

Creating your first project

When working with an IDE, you don't generally stick bits of program willy-nilly all over your hard disk (which was definitely a possibility in the heyday of BASIC, for example). Instead, you tell your IDE to use a particular directory to store your projects in, and each of your projects (in effect, each application you write) has its own directory inside that "master" directory.
There's generally little to say about actual installation of NetBeans (or indeed other IDEs): it's generally a bog-standard installer depending on your platform. When you run NetBeans for the first time, you'll be presented with a large window divided into sections. Ignore this for now, and look for an icon in the top left with a picture of a yellow box and a green cross. This is the New Project tool, as illustrated to the right. On other systems, if the icon isn't obvious, look for a New Project option in the File menu.
The New Project icon in NetBeans
Selecting the New Project button or menu option will bring up the following dialog box. The purpose of this dialog is to select the Project Type. Make sure that Java Application is selected on the right and click Next.




The next dialog to appear allows you to select the name and location for your project. I suggest you create a project called TestProject which you can use for 'playing about' with Java. (Even later on, it's often useful to have a test project for testing ideas, or for the occasional "throwaway" program.)




Below the project name field are two directory locations, which you may be able to leave as their default settings:
  • The Project Location directory is essentially the "master" directory where all your Java projects will set. The default is usually OK, although as you can see, Windows likes particularly long-winded directories. You may prefer to change this directory to something less convoluted– in Windows, C:\Projects would be an excellent choice for most people. From time to time, you'll inevitably need to browse to this place, e.g. to copy icons to your projects etc. So having it buried in ten levels of directory structure isn't always very convenient.
  • The Project Folder can usually be left as the default: it will be a folder with the same name as your project, inside the master directory you specified above.
For your first project, you can leave the Create Main Class option as it is.
Finally, click Finish. You will now be presented with a skeleton project.

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS

Adding your first line of Java code!


If you've followed this guide to creating a Java project in NetBeans, then you should now be staring at a window that is essentially fairly blank except for a line of icons at the top and a panel with a "tree" in the top-left, as pictured opposite. This tree allows you to browse the various files that make up your project. For now, we won't get too bogged down in the details of these files. The one that interests us is that labelled Main.java, as circled. For now, this is where our program will live. Later on, we'll see that a typical Java application will actually consist of various different little pieces of program. But to get us started, the single "main" file will do just fine.
If you double-click on Main.java, the right-hand side of the screen will change to show the actual program. Instead of providing us with a file that is literally blank, NetBeans actually helps us by providing a "skeleton" program that we can add to. Initially, it should look something like this:


This may look slightly complicated, until you consider that the parts in grey are just comments (basically, notes that the programmer adds to help him/herself and fellow programmers) and are actually ignored by Java. So our skeleton program is really just three lines and a bit of formatting (the curly braces { and } ). For now, we're going to ignore the meaning of these lines and just add a line of program to what's already there.
Locate the line starting with // TODO (indicated by the red arrow in the screenshot above). We're going to insert a line of Java code at this point. You can either add the line instead of the // TODO line, or else add the line below. Remember that the line is just a comment, so if you leave it in, Java will ignore it. But equally, Java won't care if you remove it. In the illustration below, we'll actually remove the comment.
In traditional fashion, we're going to add a line to the program so that when run, it prints the message Hello, world!. The line we're going to place instead of the comment is the following:
System.out.println("Hello, world!");
If you're new to programming, a key thing to bear in mind is that punctuation details matter. So the dots, brackets, quotes and semicolon must be in the right place. Spaces don't generally matter, but for reasons we'll see as we go along, it makes the program easier to read if you indent the line by a couple of spaces. You should end up with something looking something as follows (to save space, we omit parts of the program above and below in this next screenshot):


Notice how, as you type, NetBeans adds colouring automatically to the program. This colouring is called syntax highlighting. As you learn more about Java, you'll see that syntax highlighting helps you to read and spot mistakes in your program.

Running the program

If you've typed the line exactly as shown, then you're ready to run the program. Click the green arrow at the top of the window to run the program.
If you've typed everything correctly, then you should see something like the following appear at the bottom of the screen:


You'll see that our Hello, World! message appears in and amongst various "administration" messages. Of course, this wouldn't be a very user-friendly way of displaying a message to the user, and as you progress with Java, you'll learn about how to build "proper" user interfaces. But for now, be happy that you've entered a line of Java program, run it, and seen the result, however uninspiring that result might seem for now!

  • Digg
  • Del.icio.us
  • StumbleUpon
  • Reddit
  • Twitter
  • RSS