You are on page 1of 23

JAVA

ARCHITECTURE
WEEK 12
WEEK 12

JAVA ARCHITECTURE
After completing this module, students are expected to:

- write traditional, Javadoc and end-of-line comments;


- determine identifiers, reserved keywords, literals and
separators;
- enumerate different primitive data and integer types
and identify their difference; and
- distinguish expressions and declarations in a Java
program.
WEEK 12

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.
WEEK 12

Comments, Identifiers and Keywords


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.
WEEK 12

Comments, Identifiers and Keywords


Comments
- There are three types of comments:
- Traditional comment. All text from /* and */ is ignored by the
compiler.
- End-of-line comment. All text from // up to the end of the line
is ignored.
- 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.
WEEK 12

Comments, Identifiers and Keywords


Comments
- Syntax for a traditional comment is shown below. Double forward
slash followed by the comment text.

// comment

- Example:

//Computer for the Average


WEEK 12

Comments, Identifiers and Keywords


Comments
- 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 variable and get the
average of the score.
*/
WEEK 12

Comments, Identifiers and Keywords


Comments
- 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 Chapswick
*@version 1.0 12/05/2016
*/
WEEK 12

Comments, Identifiers and Keywords


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
WEEK 12

Comments, Identifiers and Keywords


Identifiers
- 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. JWindow
WEEK 12

Comments, Identifiers and Keywords


Keywords
- The words listed in the table
at the right 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.
WEEK 12

Separators
- Java uses the following separators or punctuators to divide code into
segments.
- ( ) Parentheses. Used in control statements and parameter list in a
method.
- { } Curly Braces. These are used to define the scope of Java code,
class and methods.
- [ ] Square Brackets. Used in array declaration.
- ; 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.
WEEK 12

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.
WEEK 12

Primitive Data Types


WEEK 12

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.
- Each integer data type can handle different range of value, as
shown in the previous table.
WEEK 12

Integer Data Type


- The syntax in declaring an integer data type is:
<data type> <name> = <value>;
- Examples:
int weight = 120;
int weightLimit = 1400;
int numPeople = 0;
byte minWeight = 100;
WEEK 12

Decimal Data Types


- Floating-point numbers, or decimal data types, are equivalent to
real numbers in Mathematics (e.g. 3.141516, -77.7)
- 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
previous table.
WEEK 12

Decimal Data Types


- The syntax in declaring an integer data type is:
<data type> <name> = <value>;

Examples:
float ceilingGrade = 100.0;
double piValue = 3.141516;
WEEK 12

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.
WEEK 12

Character Data Type


- The syntax in declaring a character data type is:
<data type> <name> = <value>;

Examples:

char firstLetter = ‘a’;


char lastLetter = ‘z’;
char specialCharacter = ‘%’;
WEEK 12

Character Data Type


- There are also ASCII characters Escape Meaning
which are not readily available \n New line
through the keyboard but you
\t Tab
may include them in your code
using their special characters. \b Backspace

\r Carriage return

\f Formfeed

\\ Backslash

\’ Single quotation

\” Double quotation
WEEK 12

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.
WEEK 12

Boolean Data Type


- The syntax in declaring a boolean data type is:
boolean <boolean name> = <value>;

Example:
boolean canFit = numPeople >= 10;
boolean cannotFit = false;

You might also like