You are on page 1of 14

Programming NC IV

1
Java Architecture

Java Architecture

This module covers the basic Java architecture including comments,


identifiers, keywords, literals, separators, primitive and integer data types,
expressions and declarations. Appropriate syntax for each are discussed as
well.
After completing this module, students are expected to:
1. write traditional, Javadoc and end-of-line comments;
2. determine identifiers, reserved keywords, literals and separators;
3. enumerate different primitive data and integer types and identify their
difference; and
4. distinguish expressions and declarations in a Java program.

Comments, Identifiers and Keywords


This section introduces the different types of comments, identifiers and Java
reserved keywords that you should keep in mind when writing Java
programs.

Comments
Comments, as a part of Java internal documentation, provide explanation for
the purpose of a particular piece of code in a program. They are usually
written in plain and simple English language.
Comments are ignored by the compiler. It will not affect the program except
add to the number of lines.
There are three types of comments:
1. Traditional comment. All text from /* and */ is ignored by the compiler.
2. End-of-line comment. All text from // up to the end of the line is ignored.
3. Javadoc comments. All text from /** and */ is ignored by the compiler. A
Javadoc comment is written for people who do not read the Java program
itself. There’s a certain program called Javadoc which extracts all the
Javadoc comments and turn them into a web page.

Syntax for a traditional comment is shown below. Double forward slash


followed by the comment text.
// comment
Example:
// Compute for the average

Course Module
Syntax for a traditional comment is shown below. Forward slash, asterisk,
the comment text, asterisk and forward slash again.
/* comment */
Example:
/*
This program is designed to initialize three score
variables and get the average of the scores.
*/

Syntax for a Javadoc is shown below. Forward slash, two asterisks, the
comment text, an asterisk and forward slash again.
/** comment */
Example:
/**
* The 3 Test Average Program computes
* the average of 3 scores.
* @author Archer Chapwick
* @version 1.0 12/05/2016
*/

Look at the screenshot below. You’ll notice that the comments automatically
change to gray.

Figure 1. Java Comments

If you run this program, you will not see the comments since they are ignored
by the compiler.
Programming NC IV
3
Java Architecture

The following text are not included in the output.


This program is designed to initialize three score
variables and get the average of the scores.

Compute for the average

Figure 2. Java Comments Output

In other words, no matter what you write in the comments, as long as they
are in the /* */ or // symbols, they will not be printed.
We’ve learned in the previous modules that comment is also a part of
internal documentation. So side comments for humor purposes are not a
standard practice.

One more thing to remember, traditional comments may take up as many


lines as they require, as long as the comment is “in” the /* */ symbols.
You can see in the sample above that the traditional comment is written from
lines 1-3. Compared to the end-of-line comment, it only takes up a single line
as shown in lines 11 and 17.
If you accidentally press Enter while writing an end-of-line comment, you
will receive an error warning.

Figure 3. Java Comment Error

One way to get rid of the error is to place another // symbol before the
comment in line 12 as shown below.

Figure 4. Java End-of-Line Comments

Course Module
Or convert it to a traditional comment.

Figure 5. Java End-of-Line Comments

One last thing, DO NOT FORGET to place the closing symbol for a traditional
comment */. Or else, the program will convert everything into a comment
from the opening symbol /*.

Figure 6. Java Traditional Comments

You can see in the example above that everything from the /* symbol was
converted to a comment. Make sure you close the traditional comment.

Identifiers
Identifiers are basically names you give to a variable, method, class or other
Java elements. Its meaning can change from one program to another, unlike
keywords.
Examples of identifiers are:
1. String
2. weight
3. MAX_VALUE
4. perfectScore
5. isPerfect
Remember that an identifier cannot have the same spelling as a keyword, a
Boolean data type or a null literal.
There are also identifiers from Java API (Application Program Interface):
1. Integer
2. File
3. String
4. JWindow
5. JButton
What kind of names are those? Don’t worry about JWindow or JButton for
now. We’ll discuss them in the GUI (Graphical User Interface) module.
Programming NC IV
5
Java Architecture

Keywords
The words listed in the table below are Java keywords or reserved words.
They have their special meaning in Java programming language and this
meaning remains constant from one program to another.
You cannot use these words as identifiers or name for your variables and
constants so you better keep these keywords in mind.
abstract assert boolean Break
byte case catch char
class const continue default
do double else enum
extends final finally float
for goto if implements
import instanceof int interface
long native new package
private protected public return
short static strictfp super
switch synchronized this throw
throws transient try void
volatile while

Look at the screenshot below:

Figure 7. Java Keywords

You’ll notice that the reserved words package, public, class, static,
void and double change color when you type them in Netbeans. This is one
sign that they are the Java reserved words.
So naming a variable after them will yield an error, as shown in the sample
below:

Course Module
Figure 8. Java Keywords as Identifiers

If you replace the variable name average and set it to some keyword like
void, that red underline and circle with an exclamation point are symbols to
warn you that there’s an error.
Be careful in using those keywords, it’s better to keep those keywords in
mind for they will be useful when you’re writing Java programs.

Separators
Java uses the following separators or punctuators to divide code into
segments.

Character Character Name Usage


( Open parenthesis Used in control statements and
parameter list in a method.
) Close parenthesis
{ Open curly brackets / braces This is used to define the scope
of Java code, class and methods.
} Close curly brackets / braces
[ Open square bracket Used in array declaration.
] Close square bracket
; Semicolon Used to terminate a Java
statement.
, Comma Used in method parameter list
and variables declared in a
single line.
. Period Used in package name and class
name and in separating a
variable or method from its
instance or object.

What are the use of these separators? When are they used? Separators
basically help define or outline the structure of a program.
Imagine a Java program without any separators, it would be disaster.
Programming NC IV
7
Java Architecture

Compare the one above, to this:

Primitive Data Types


In Java, there are primitive data types, or simple types, that we will be
working with most of the time. These are the lowest-level objects in Java
programming since they are non-grouped pieces of data.
These primitive data types are listed in the table below along with the
sample and value range.
Type Name Sample Literal Value Range
Whole Number
Byte (byte) 42 –128 to 127
Short (short) 42 –32768 to 32767
Int 42 –2147483648 to 2147483647
Long 42L –9223372036854775808 to
9223372036854775807
Decimal Number
Float 42.0F –3.4 × 1038 to 3.4 × 1038
Double 42.0 –1.8 × 10308 to 1.8 × 10308
Character
Char ‘A’ Thousands of characters,
Symbols and glyphs
Logical
Boolean true true, false

Integer Data Type


Integers, or simply whole numbers, are used in Java programming if you are
dealing with numbers WITHOUT decimals.
There are four (4) types of integer data type – byte, short, int and long.

Course Module
Each integer data type can handle different range of value, as shown in the
table above.
The syntax in declaring an integer data type is:
<data type> <name> = <value>;
Examples:
int weight = 120;
int weightLimit = 1400;
int numPeopl = 0;
byte minWeight = 100;

Let’s look at the example below.


The answer for numPeople = weightLimit / weight; is equal to 11.
Just 11. Not 11.0, not 11.667, not 11.66666666666667.
This is because integers deal with whole numbers only.

Figure 9. Java Character

Most of the time, we will be using int. BUT, be careful in declaring other
integer types.
Let’s say you assigned byte weight = 150; knowing that byte can only
handle from -128 to 127, then you’ll surely get an error as shown below.
Or assigning 9999999999999999 to a long integer type.

Figure 10. Java Byte Data type


Programming NC IV
9
Java Architecture

Decimal Data Types


Floating-point numbers, or decimal data types, are equivalent to real
numbers in Mathematics (e.g. 3.141516, -77.7) Don’t worry, we’re not doing
Math here.
Java has two (2) kinds of floating point data types – float and double. Float is
a single precision floating point while double is a double precision floating
point.
They also vary in the value range they can handle as shown in the table
above.
The syntax in declaring an integer data type is:
<data type> <name> = <value>;
Examples:
float ceilingGrade = 100.0;
double piValue = 3.141516;

Let’s take the elevator sample above and change the data type to double.
You’ll notice that the value of numPeople changed to 9.333333333333334.
But obviously, people can’t be measure by 9.333333333333334, that’s why
the original program used int and the result is simple 9 or 9 Archer persons
can fit in the elevator.

Course Module
Character Data Type
Character literals are specified with single quotes (‘’) such as ‘a’, ‘A’,
‘@’ or ‘7’. Characters from the ASCII set are considered character literals as
well as escape sequence. There are 128 ASCII characters composed of letters,
numbers and special characters.
The syntax in declaring a character data type is:
char <character name> = ‘<value>’;
Examples:
char firstLetter = ‘a’;
char lastLetter = ‘z’;
char specialCharacter = ‘%’;

The character can only have ONE (1) character as its value – whether it is a
letter, number or special characters. And the value is enclosed in single
quotes (‘’).

Take a look at the example below. The output will obviously display My
initials are ABC.

Figure 11. Java Character

But if you change the value of fName to X, mName to Y and lName to Z. Then
the output will change, as shown in the screenshot below.
Programming NC IV
11
Java Architecture

Figure 12. Java Character

One character value for character? That’s too short. What if I put something
like char fName = ‘Archer’;

Then, as you can see in the sample above, you’ll get an error.
More than one character requires what we call String and we will discuss this
in a later module.

There are also ASCII characters which are not readily available through the
keyboard but you may include them in your code using their special
characters.

Escape Meaning
\n New line
\t Tab
\b Backspace
\r Carriage return
\f Formfeed
\\ Backslash
\' Single quotation mark
\" Double quotation mark

Course Module
You can use this in printing output, say you want a new line and a tab space
before Nice to meet you and a double quote in the initials. Take a look at
the sample below.

Figure 13. Java Character

Boolean Data Type


A Boolean data type can only have two values – true or false. Other
programming languages have boolean values of 0 for false and 1 for true.
The syntax in declaring a boolean data type is:
boolean <boolean name> = <value>;
Examples:
boolean canFit = numPeople >= 10;
boolean cannotFit = false;

The first example assesses if the variable numPeople is greater than or


equal to 10; if it is, then canFit = true, otherwise canFit = false.
Let’s look at the example below.
Programming NC IV
13
Java Architecture

Figure 14. Java Boolean

Let’s say we’ll fit ten (10) Archer persons instead of 13, then obviously, the
output will change as well.
Since 10 Archer persons will only weigh 1200 lbs. while the elevator limit is
1400.

Course Module
Glossary
API (n.) – Application Program Interface is a software that facilitates
interaction between other software.
Literal (n.)– In Java programming, a literal is a representation of boolean,
character and numbers.

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. Java™ Platform Standard Ed. 7. Retrieved from:
https://docs.oracle.com/javase/tutorial/java/

You might also like