Getting Started - JavaProgramming
Getting Started - JavaProgramming
One of the difficult things about getting started with Java is installing
everything you need. Even before you write a single line of code,
the headaches begin! Hopefully, the following sections will make life
easier for you. We're going to write all our code using a free piece of
software called NetBeans. This is one of the most popular IDEs
(Integrated Development Environment) in the world for writing Java
programmes. You'll see what it looks like shortly. But before
NetBeans will work, it needs you to install Java
https://www.oracle.com/technetwork/java/javase/downloads/
index.html
Click the link download link, Oracle JDK. At the time of writing, the
latest version of Java is 11. But get whatever the latest version is,
because you can use any version of Java for this course.
You need to accept the licence agreement on the Oracle website,
and then select your operating system. For Windows users, you can
get the exe file by clicking the link. After that, it's just a normal
download and install.
Once Java has installed on your system, the next step is to
download and install Netbeans.
How to Install NetBeans
You write the actual code for your programmes in a text editor. (In
NetBeans, there's a special area for you to write code.) The code is
called source code, and is saved with the file extension .java. A
programme called Javac is then used to turn the source code into
Java Byte Code. This is known as compiling. After Javac has
finished compiling the Java Byte Code, it creates a new file with the
extension .class. (At least, it does if no errors are detected.) Once
the class file has been created, it can be run on the Java Virtual
Machine. So:
Create source code with the extension .java
Use Javac to create (compile) a file ending in .class
Run the compiled class
NetBeans handles all the creating and compiling for you. Behind the
scenes, though, it takes your sources code and creates the java file.
It will launch Javac and compile the class file. NetBeans can then
run your programme inside its own software. This saves you
the hassle of opening up a terminal window and
typing long strings of commands.
You can get NetBeans from here (drop me a line, if the link below is
not working):
https://netbeans.apache.org/download/nb113/index.html
The latest version is Netbeans 11, at the time of writing. But you can
use earlier versions (or later ones).
Double click the netbeans folder to see lots of folders and files:
Locate the bin folder and double click it. You'll see a few files:
Now that you have a general idea of how Java works, launch your
NetBeans software. Then click the link below to go to continue with
the lesson.
The NetBeans
Software
When you first run NetBeans, you'll see a screen something like this
one:
When you click Next, you'll see a screen like this one:
In the Project Name area at the top, type a Name for your Project.
Notice how the text at the bottom changes to match your project
name (in the text box to the right of Create Main Class):
firstproject.FirstProject
The Class created will be called FirstProject, with a capital "F",
capital "P". The package is also called firstproject, but with a
lowercase "f" and lowercase "j".
Click the plus symbol to expand your project, and you'll see the
following:
This same source code should be displayed to the right, in the large
text area. It will be called FirstProject.java. If you can't see a code
window, simply double click FirstProject.java in your Projects
window above. The code will appear, ready for you to start work.
The coding window that appears should look like this:
One thing to note here is that the class is called FirstProject:
public class FirstProject {
This is the same name as the java source file in the project window:
FirstProject.java. When you run your programmes, the compiler
demands that the source file and the class name match. So if
your .java file is called firstProject but the class is called FirstProject
then you'll get an error on compile. And all because the first one is
lowercase "f" and the second one uppercase.
Note that although we've also called the package first project, this is
not necessary. We could have called the package some
programme. So the name of the package doesn't have to be the
same as the java source file, or the class in the source file: it's just
the name of the java source file and the name of the class that must
match.
/*
This is a comment spreading
over two lines or more
*/
In the comment above, note how it starts with /*. To end the
comment, we have */ instead.
There's also something called a Javadoc comment. You can see
two of these in the coding image on the previous page. A Javadoc
comment starts with a single forward slash and two asterisks (/**)
and ends with an asterisk and one slash ( */ ). Each line of the
comment starts with one asterisk:
/**
*This is a Javadoc comment
*/
Javadoc comments are used to document code. The documented
code can then be turned into an HTML page that will be helpful to
others. You can see what these look like by clicking Run from the
menu at the top of NetBeans. From the Run menu, select Generate
Javadoc. There's not much to see, however, as you haven't written
any code yet!
In the next part, you'll learn about the structure of the above code,
and how to run your programmes.