You are on page 1of 24

Programming NC IV

1
Fundamental Concepts of Java Programming Language

Fundamental Concepts of Java Programming


Language

This module discusses Java as a programming language and its


characteristics; installing Java Development Kit and Netbeans will also be
covered in this module as well as compiling and running simple programs
using the command line interface and Netbeans; protocols in naming Java
elements and spacing are likewise included.
Upon completing this module, students are expected to:
1. understand the basic concepts of Java as a programming language ;
2. install Java Development Kit (JDK);
3. run simple Java programs using the command line interface (CLI);
4. install Netbeans and compile simple programs;
5. follow standard protocols in naming convention, spacing and indentions.

Java as a Programming Language


Java programming language is designed to be simple enough that many
developers and programmers can become experts in this language. It is an
object-oriented, class-based, concurrent and general-purpose language. Java
is related to C and C++ but it has a different structure.
Java, as defined by Gosling, et. al. (2015) is statically and strongly typed as
well as a high-level programming language. During run-time, classes are
loaded and linked.

Java Installation
Java Standard Edition (SE) Development Kit is a software development
platform used in developing Java applications and applets. It includes an
interpreter or loader (java), Java Runtime Environment, a compiler (javac),
an archiver (jar) and document generator (javadoc) along with other tools
required in Java development.
To download and install Java Development Kit, follow these steps:
Step 1: Click this link or copy and paste it in your browser’s address bar:
http://www.oracle.com/technetwork/java/javase/downloads/index-jsp-
138363.html

Step 2. You should see the screen below. Click the Download button under
Java. We will download Netbeans later.
Course Module
Figure 1. Download Java

Step 3. You will be redirected to this page. Scroll down until you see Java SE
Development Kit 8u11. Choose the compatible one for your operating
system.
Note: If you are using a 32-bit Windows OS, choose Windows x86.
Choose Windows x64 if you’re using a 64-bit Windows OS. Otherwise,
select the installer compatible for your operating system if it is Mac,
Solaris or Linux.

Figure 2. JDK Platform

Step 4. Your download should start as soon as you click the link. Wait for it to
finish then click the file downloaded.

Figure 3. JDK Downloading

Step 5. If prompted to install the program, allow it. Then, you should see the
screen below. This is the beginning of the installation process.
This screen is basically a welcome note stating the version number of the JDK
you are installing. Click Next.
Programming NC IV
3
Fundamental Concepts of Java Programming Language

Figure 4. JDK Installation

Step 6. Next is an option for you to install features of JDK and where to install
it. By default, it will be installed in Drive (C:) in your Programs folder. Leave
it as it is.
Click Next.

Figure 5. Java Installation Options

Course Module
The installer will be extracted. Wait for it to finish. The status is indicated by
the progress bar.

Figure 6. JDK Installer Extracted

Step 7. Next, you will have an option to change the destination folder or
where Java will be installed. By default, it will be installed in Drive (C:) under
Program files folder.
Click Next.

Figure 7. Java Destination Folder


Programming NC IV
5
Fundamental Concepts of Java Programming Language

The Status should now read “Installing Java”.

Figure 8. Java Installation

Step 8. You should see this prompt if Java is installed successfully. If you
want to watch tutorials, scan documentation, developer guides and release
notes, click Next.
If not, click Close.

Figure 9. Java Installation Complete

Course Module
Java Program Using CLI
Now it’s time to test the JDK we installed. Let’s try and run simple programs
with the all-time favorite “Hello, World!”.
Follow these steps carefully.
Step 1: Open a text editor like Notepad.
Step 2. Copy and paste the sample program below:

public class Test


{
public static void main(String[] args)
{
System.out.println("Hello, World!");
}
}

Step 3. Save it as Test.java. If you will notice, the class is also named Test.
Look at the program above.
public class Test
{

}
If you are going to change the name of the class, make sure to save it as the
filename as well followed by filename extension .java.
See the screenshot below. If you want to follow these steps EXACTLY, create
a folder in Drive (D:) and name it Java Projects.

Figure 10. Saving a Java file


Programming NC IV
7
Fundamental Concepts of Java Programming Language

Observe carefully. Java Projects folder has only ONE file right now and that
should be Test.java.

Figure 11. Saving a Java file

Step 4. Now it’s time to compile and run the program. Click the Start button,
type “cmd” and press Enter.

Step 5. By default, your command line interface (CLI) should display


C:\Users\YourComputerName>.
Type the following in order:
d: then press Enter. (This will move us to Drive (D:))
cd Java Projects then press Enter. (This will move us to the Java
Projects folder.)
javac Test.java (This will compile our program. If you get an error, go
to Page 10 of this document.)

Figure 12. Compiling a Java Program

Course Module
Before we continue, take a look at your Java Projects folder , it should now
have two (2) files – Test.java (our code) and Test.class (what the compiler
added when we typed “javac Test.java”.

Figure 13. Java Class Created

Step 6. While still in the CLI, type java Test and press Enter.

Figure 14. Running the Java Program

You should now see the output of the program “Hello, World!”

Figure 15. Java Program Output


Programming NC IV
9
Fundamental Concepts of Java Programming Language

Pretty easy, right?


Now let’s edit our file Test.java and add the two lines after Hello, World!
Save it.

Figure 16. Editing the Java Program Output

Open another CLI and repeat Steps 5-6. You should now have a different
output.
It should say “Hello, World! This is my first Java program. I’m a
programmer!”

Figure 17. Java Program Output

Course Module
Javac Error
In case you get an error that says “javac is not recognized as an internal or
external command…”, we can fix that.

Follow these steps:


Step 1. Go to where Java was installed in your PC and find the “bin” folder.
Copy and paste its address.
In this case, the address is “C:\Program Files\Java\jdk1.8.0_111\bin”.

Figure 18. Java Folder

Step 2: Next, click the Start button then the Control Panel. Choose System
then go to Advanced Settings.

Figure 19. Advanced System Settings


Programming NC IV
11
Fundamental Concepts of Java Programming Language

Step 3: You should see the screen below (right). Click “Environment
Variables” and you should see the screen at the right.
Select “Path” and click “Edit…”.

Figure 20. Environment Variables

Step 4: Make sure the Variable name is “PATH”.


In the Variable value box, paste the address you copied earlier.
In this case, it’s “C:\Program Files\Java\jdk1.8.0_111\bin”.

Figure 21. User Variable

Step 5: Click OK. The error should have been fixed by now. Try running that
simple program again using the CLI.

Course Module
Netbeans Installation
Now, it’s about time we start writing, compiling and running our Java
programs using the Netbeans IDE.
What is an IDE? IDE or Integrated Development Environment is a software
program which has all the tools a programmer need to write and test codes
or programs.
Netbeans is an IDE for Java programming language. Though it’s true that you
can write Java programs using a Notepad, compile and run it using the CLI, it
will be troublesome if you are working on a big Java project.
Just imagine writing your final report or research using a Notepad, how sure
are you that when it is printed, it has the format that you need? Crazy, right?

Let’s get started by downloading the Netbeans installer.


Follow these steps carefully:

Step 1. Click this link or copy and paste it in your browser’s address bar:
https://netbeans.org/downloads/

Step 2. You should see the screen below. Under Java SE (Standard Edition),
click the Download button.

Figure 22. Netbeans Download


Programming NC IV
13
Fundamental Concepts of Java Programming Language

Step 3. Go to Downloads, or to the folder you set as a default Download


folder, and double click the Netbeans installer.

Figure 23. Netbeans Installer

Step 4. The installer should be configured now. Wait for this process to
finish.

Figure 24. Configuring Netbeans installer

Course Module
Step 5. You will be greeted with Netbeans’ welcome note where the version
is also stated.
Click Next.

Figure 25. Netbeans Welcome Note

Step 6. Scan the license agreement and if don’t have any problem with it,
click Next.

Figure 26. Netbeans License Agreement


Programming NC IV
15
Fundamental Concepts of Java Programming Language

Step 7. Choose where to install Netbeans. By default, it should be in Drive


(C:) in the Program files folder. Click Next.

Figure 27. Destination Folder

Step 8. Click Install.

Figure 28. Install Netbeans

Course Module
The installation process will begin. Wait for it to finish.

Figure 29. Installation Progress

Step 9. If there are no problems encoutered, the installation should be


completed by now.
Click Finish.

Figure 30. Installation Complete


Programming NC IV
17
Fundamental Concepts of Java Programming Language

A desktop icon for Netbeans should be visible in your desktop by now.


Congratulations! You have successfully installed Netbeans!

Figure 31. Netbeans Desktop Icon

Netbeans IDE
Like any other software, you must be familiar of the different parts of the
Netbeans IDE to fully utilize it.
Look at the screenshot below. There are 8 major parts that we will focus on.
The other windows / parts will be discussed and displayed in the later
modules.

Figure 32. Netbeans IDE

Course Module
As for the basic parts of the Netbeans environment, take note of the
following:
1. Menu Bar – this is the main menu of the Netbeans IDE. Here you will find
menu items such as File, Edit, View, Navitage and more. Each item has a
set of sub-items which offer different options and actions.
2. Quick Launch Bar / Toolbar – the most commonly used actions are
displayed in this toolbar
3. Projects Windows – this is where you will see the project files, classes and
other Java files. Click the + or – sign beside each folder to collapse or
expand the list.
4. Navigator – this feature allows Java programmers to be oriented in source
code and conduct basic actions.
5. Working Area / Code editor – this is where you write codes
6. Assist Icons – icons that allow you to add comments and bookmarks, find
selected text or code and change indentation of codes.
7. Line Number – this tells you the line number of each line of code
8. Output Window – this is where the output is displayed

Java Compilation and Execution


I know you’re itching to write some programs right now. Well my friend, this
is the moment you’re waiting for.
Launch Netbeans by double-clicking on its icon or by using the Start Menu.
You will be greeted by this interface. Where do you write your code? Hang on
there, mate. Just a few little steps and we’ll be writing that ultimate code of
yours.

Figure 33. Netbeans Interface


Programming NC IV
19
Fundamental Concepts of Java Programming Language

First, we have to create a Java Project. To do that, click File then New Project.
By default, Java is selected under Categories while Java Application is chosen
under Projects. Leave it as is.
Click Next.

Figure 34. Java Project

Choose the location or folder of your new project and give it a name.
Click Finish.

Figure 35. Java Project

Course Module
Automagically, you will be greeted with this code:

Figure 36. Java Hello World Class

Don’t worry, it will do you no harm. The text in gray are just comments,
whatever you write in the comment section will not have an effect in your
program (we’ll discuss this later in detail).
The only thing that matters now is the code in the main class.
Type the following inside the main class as shown in the image below.
System.out.println(“Hello World”);

Figure 37. Hello World

Now press F6 on your keyboard or click the green Run Project button.
You should see the output at the bottom of the Netbeans screen. Easy, right?
To run a class that is NOT the main class, press Shift + F6. Each project has its
own main class which is the default class that it runs. If you have other
classes in a project, press Shift + F6 instead of F6.
Programming NC IV
21
Fundamental Concepts of Java Programming Language

Syntax, Naming Convention, Spacing and Indention


This section discusses what a syntax is and why it should be followed, as well
as coding convention for Java methods, variables, classes, and proper
indentation and alignment.

Syntax
You’ve probably seen this word when you typed something wrong on your
calculator “Syntax Error”.
We also have “syntax” when it comes to programming. Basically, syntax is
one of the building blocks of programming language – it is the spelling and
grammar of programming. You should follow it or else you’ll get an error.
Some points to remember when writing a Java program:
1. Java is case sensitive.
counter is different from Counter and COUNTER.
2. There are standard naming conventions that you should follow. This will
be discussed in the next section.
3. Each Java function and features has its own syntax to be followed like
declarations, expressions, conditional and looping statements and more.

Naming Convention
Programmers have to follow standard naming conventions so that their code
will be easy to read and understand. We have discussed this topic briefly in
properly documenting a program.
Let’s take a closer look at Oracle’s naming convention for Java.
Identifier Type Naming Rules Examples
Classes 1. A class should be a noun. class
2. It should be written in mixed case EmployeeRecord;
with the first letter of each word
capitalized. class Students;
3. Keep class name simple.
4. Use whole words and avoid
abbreviations.
Methods 1. A method should be a verb. getStudentName();
2. It should be written in mixed case runFast();
as well but the first letter is in
lower case.
3. First letter of internal words are
capitalized.
Variables 1. Variables should be written in int x;
mixed case with the first letter in int counter;
lower case. float
2. First letter of internal words are averageScore;
capitalized.
Course Module
3. Do not start the variable name with
an underscore _ or dollar sign $.
4. Use short but meaningful names.
5. One-character variable names
should be avoided except for
throwaway variable.
Constants 1. Constant’s name should be written static final int
in uppercase. CEILING_GRADE =
2. Words are separated with 100;
underscore _.
static final int
MAX_LENGTH = 99;

Do not worry about classes, methods, variables and constants at this


moment. We will discuss them shortly in the following modules. For now, try
to keep in mind these naming conventions since they will come in handy
later.

Indentation, Alignment and Blank Spaces


Standard convention for indentation uses four spaces as the unit of
indentation. If your code is too long, make sure it’s no longer than 80 lines. If
in case a code statement will not fit in a single line, follow these rules:
1. Break it after a comma.
Example:

INCORRECT
someMethod(longExpression1, longExpression2
, longExpression3, longExpression4
, longExpression5);

CORRECT
someMethod(longExpression1, longExpression2,
longExpression3, longExpression4,
longExpression5);

2. Break if before an operator.


INCORRECT
average = (testScore1 + testScore2 + testScore3 +
testScore4) / 4;

CORRECT
average = (testScore1 + testScore2 + testScore3
+ testScore4) / 4;
Programming NC IV
23
Fundamental Concepts of Java Programming Language

3. Use higher-level breaks to lower-level breaks.


INCORRECT
public static void main(String [] args){
int a=30, b=50, c=20;
}

CORRECT
public static void main(String [] args){
int a=30, b=50, c=20;
}

4. Place a space after a keyword followed by a parenthesis.


INCORRECT
while(true){
...
}

CORRECT
while (true){
...
}

5. Put space between binary operators.


INCORRECT
sum=score1+score2;
average=sum/2;

CORRECT
sum = score1 + score2;
average = sum / 2;

Course Module
6. There should also be blank spaces between sections of a source file, class
and interface definitions, methods, local variables in a method, before a
block and between logical sections in a method.

7. Align braces appropriately. There should be a line break after the opening
brace and before the closing brace.
INCORRECT
public class HelloUni {
public static void main(String [] args){
System.out.print(“Hello Universe”);
}
}
INCORRECT
public class HelloUni { public static void main(String []
args){
System.out.print(“Hello Universe”);
} }

CORRECT
public class HelloUni {
public static void main(String [] args){
System.out.print(“Hello Universe”);
}
}

Glossary
Applet (n.) – in Java programming, an applet is a very small application with
one or few functions
Class (v.)– in Java, a class is defined as a blueprint where individual objects
are created
Method (n.)– a set of code grouped together to perform an operation

References
Gosling, J., Joy, B., Steele, G., Bracha, G. & Buckley, A. (2015). The Java
Language Specification. Java SE 8 Edition. California: Oracle America,
Inc.
Oracle. Naming Conventions. Available at:
http://www.oracle.com/technetwork/java/codeconventions-
135099.html

You might also like