Hello again.
Welcome back to another one of our hopefully really good articles on Java
programming. If you’re reading this, then I’m assuming you read the last post
on installing java software (if you haven’t, you can do so by clicking the link
here). If you have read the last post, then by now you should have successfully
installed the Java Runtime Environment, the Java Development Kit and the
NetBeans IDE software.
(N.B.: The
majority of these tutorials will be done using NetBeans, but those using other
IDEs like Eclipse are also free to follow through).
This week, we’re
going to take a quick look at the NetBeans environment, explain how coding in
Java works, and at the end, we’ll run our first piece of code.
So let’s start
learning.
Before go ahead see the last articles
THE JAVA_HOME VARIABLE
I want to
believe that all those reading this have successfully downloaded and installed
everything we mention in the last post. If you want to make sure that the JRE
has been installed, or you want to know the directory that it is saved into (I
don’t know, maybe you’re just curious), what you can probably do is access the
JAVA_HOME environment variable.
In case this is
new to you, an environment variable is a string variable (we’ll talk about
string variables in a later post) that stores information like the drive, path
or name of a particular file. Now, what the JAVA_HOME variable does is point to
the directory where your JRE is saved.
To access this,
all you have to do is open the Control Panel, access your system settings, and
look for “Advanced System Settings”. This should open the following dialog box:
Click on
“Environment Variables”, and you should see the JAVA_HOME variable in the new
dialog box, under the “System Variables” tab. In fact, if you double-click it,
you will see a dialog box that will allow you to change the variable name and
directory.
Now let’s move
on.
How Java Coding Works
Traditionally,
java code (and all code for that matter) is written using a text editor
(NetBeans, and most IDEs, comes with a special area for writing code. The
source code (or code written by you) is saved with the extension “.java”, and
the compiler (a program known as Javac) then converts the source code into
machine code (1s and 0s), in a process known as Compiling.
When the compiling
process is over, and if there are no errors, Javac creates a new file with a
“.class” extension, which is then run by the Java Virtual Machine.
NetBeans takes
care of all of this for you, creating the files, compiling the source code, and
running these programs in its own software, thereby saving you the trouble of
writing long strings of code in a console window. (Aren’t IDEs great?)
Now that you
know the basics of coding, time to start your engines! (By that, I mean run the
NetBeans, or whatever it is you’re using).
NETBEANS…WOW…
When you launch
the software, it should look something like this:
It’ll take a
little while to load though, so maybe you can play a quick game of Minesweepers
while you wait. Or, I don’t know…
To open the
coding window, go to “File > New Project” or click the keyboard shortcut
“Ctrl-Shift-N”. You could also look under the toolbar for this symbol
Since
we’ll be making a Java application, select “Java” under Categories, then under
Projects, select “Java Application”, like was mentioned before. You’ll then see
this dialog box:
Yeah, I know; so
many dialog boxes, right! Don’t worry, this is the last one. Type in the
project name in, well, the Project Name text box (it’s actually quite
self-explanatory). Let the name be TutorialProject (with no space). You’ll
notice what happens in the text area next to the “Create Main Class” checkbox
(make sure this is checked by the way; I’ll explain why later). As you write
“TutorialProject” in the Project Name text box, the same will appear in the
second text box, but this time with lowercase. In simple terms, it will
generate something like this:
tutorialproject.TutorialProject
The
“TutorialProject” with upper case is the name of the class file created by the
IDE, while the one in lower case is the name of the package it is contained in.
You can set the default location of your java project in “Project Location” or
you can leave it at the default (NetBeans will create a folder with the
projects name) and when you’re satisfied, click “Finish”.
You should
almost instantly be taken to the coding window, which should look a lot like
this:
Now, if you
would please direct your attention to the left of this, you should notice this
window:
Your “Project”
window would most likely be mostly empty (save for the TutorialProject) since
the software is newly installed. Click the plus by the little coffee mug and
you’ll see this:
Click
the plus by the Source Packages, and you’ll see the package name (which is the
same as your project name). Expand that and you’ll see the java source file:
But enough of
all the software stuff, let’s take a look at some java code.
A Look at some code
Now when you
look at the coding window, you’ll probably notice some lines of code written
there. You’ll see some words in blue, some in black and some that are greyed
out, and you seem kind of overwhelmed. It’s kind of like standing in a glass
museum; everything looks beautiful and new, but you’re afraid to touch
anything, because doing so might wreck the entire thing.
Don’t get me
wrong, this is quite true. But don’t worry, that’s why I’m here. To walk you
through this complicated minefield we call Java.
Now first off
all…
COMMENTS
…you’ll probably
notice some words that are greyed out in the code. You also notice that they
have a lot of slashes and asterisks in them, and at first, they probably look
like some sort of cool, complicated code that you shouldn’t mess around with.
But don’t be scared, these greyed out lines are
called comments, and they’re relatively useless. In fact, I could delete all of
them right now, like so:
Ta da! No
consequences! And the code looks a lot less complicated too!
Now, I’m sure
you’re wondering right now “Wait, if those things aren’t. Say, you just wrote
this mad 300-line code for, like, a game or something, and then feeling proud
of yourself, you close your system and go to sleep. The next day, you want to
show it off to your fellow coding buddy, and he (also impressed) asks you what
something does. You open your mouth, about to tell him what is obviously
painstakingly obvious…
But you can’t
tell him anything. You code is long and complex, you can’t remember what
anything does. And now, you can’t tweak it, because you’re too afraid to touch
it, because you have no idea what anything does.
Sad, but true.
Now, you can
prevent this with comments. These greyed out lines are very useful in
describing what each part of the code is used for. The best thing is, the
compiler completely ignores them when run, so you can write anything you want.
I prefer to delete the comments that NetBeans generates, but you can leave them
if you want.
To create a
comment, simply start whatever you want with two forward slashes, e.g.:
//This is a
comment
But this will
only last for one line. If you want to comment out more than one line, you
could do this:
//This is a
comment
//This is another
comment
Or, even simpler
and more efficient, start the line you wish to comment with a forward slash and
two asterisks, and end it with an asterisk and forward slash, i.e.:
/** This is
a
Multiline comment
*/
NOW, THE REST
If you removed
the comments, the first thing you would see is this line:
package
tutorialproject;
This is just the
name of the java package you created. Note the semicolon at the end. It’s quite
necessary to end all lines of code with a semicolon, or else it won’t work.
Also, it’s not compulsory for the package name to be the same as the project
name, or class name. If you had renamed it while creating the project earlier,
it wouldn’t really matter.
The next thing
would be:
public class
TutorialProject {
}
This is the name
of the class file created while compiling. You can think of it as the area
where code is written, with the curly brackets ({}) defining the borders of
this area. Any code run outside these curly brackets will not be run with the
remaining code, and will most likely generate an error message.
Looking in the
curly brackets now, you’ll see this:
public static void main (String[ ] args ) {
}
And it comes
with its own curly brackets too (the same conditions apply). This is called the
main method (hence the name “main”). Never mind what a method is now; we’ll
talk about that in a later post. Just think of it now as a chunk of code, and
the main method is the chunk of the code that the compiler runs first. You’ll
likely receive error messages if there is no main method, and for now, this is
where you’ll input most of your code.
(Don’t worry
about all those words like “static” and “public” and those stuff in the
bracket, we’ll explain those when we explain methods).
Now before we
round off, it’s only fair that we run at least one code.
OUR FIRST CODE… YAY!!!
What we’re going
to do is print out something to the console window. Quick, add this in between
the curly brackets of your main method:
System.out.println(“Hello
World”);
When you type in System and put the dot,
this happens:
Double click
“println” and a bracket will also appear with the statement. In the bracket,
put quotation marks and type “Hello World”, then outside the brackets, put a
semicolon (always remember the semicolon. It will save you a lot of sleepless
nights as you advance as a programmer).
At the end, your
code should look like this:
WHAT DOES ALL THIS MEAN AGAIN?
Okay, so what
this is going to do is print out “Hello World” into the system console. The
word “System” is the keyword which calls the console out. The keyword “out”,
well, think of it as short for output. And “println”, obviously enough, is the
keyword which tells the system to print whatever you want. (Keep in mind, “ln”
is small letter ‘L’ and n, not capital letter ‘I’ and ‘n’).
After the
println, there’s a bracket, with “Hello World” in quotation marks. It is very
important to put it in quotes, because if not, the computer will mistake the
output for code, and there will be an error. Watch what happens when I remove
the quotes:
You notice that
red line? Yeah, that means there’s an error.
In short, you’re
saying “Computer, print out whatever I put in that bracket, which is “Hello
World”. You can put anything you want in the quotes. It will print out!
Now let’s run
this thing. There’s a number of ways to do so. One is to right click on the
window and click “Run File” from the pop-up window that pops up. Another way is
to click this symbol
on the toolbar, or you could go to the Run menu and click Run. Or,
if you prefer keyboard shortcuts, click F6 or Shift-F6.
Either way, when you run it, you’ll notice some
processes taking place, and this will pop-up with your message:
If you see this,
then congrats! You’ve just run your first successful java code.
See you next
post, where we’ll most likely be discussing variables and data structure.
If you have any other issue , let us know or if you like the article please share it with your friends and colleagues
Thanks for Reading
Noeik
0 comments:
Post a Comment