Comprog1 W Module 3
Comprog1 W Module 3
PROGRAMMING
WEEK 3 - MODULE
Learning Outcomes:
In this section, we will be discussing a little bit of Java history and what is Java
Technology. We will also discuss the phases that a Java program undergoes. At the end of the
lesson, the student should be able to:
• Describe the features of Java technology such as the Java virtual machine, garbage
collection and code security
• Describe the different phases of a Java program
Java was created in 1991 by James Gosling et al. of Sun Microsystems. Initially called
Oak, in honor of the tree outside Gosling's window, its name was changed to Java because there
was already a language called Oak. The original motivation for Java was the need for platform
independent language that could be embedded in various consumer electronic products like
toasters and refrigerators. One of the first projects developed using Java was a personal hand-
held remote control named Star 7. At about the same time, the World Wide Web and the Internet
were gaining popularity. Gosling et. al. realized that Java could be used for Internet
programming.
The name Java was the developers’ name initials too.
JAVA - James Gosling,
Arthur Van Hoff,
Andy Bechtolsheim
Code Security
Code security is attained in Java through the implementation of its Java Runtime
Environment (JRE). The JRE runs code compiled for a JVM and performs class loading
(through the class loader), code verification (through the bytecode verifier) and finally
code execution.
The Class Loader is responsible for loading all classes needed for the Java program. It
adds security by separating the namespaces for the classes of the local file system from
The first step in creating a Java program is by writing your programs in a text editor.
Examples of text editors you can use are notepad, vi, emacs, etc. This file is stored in a
disk file with the extension .java.
After creating and saving your Java program, compile the program by using the Java
Compiler. The output of this process is a file of Java bytecodes with the file extension
.class.
The .class file is then interpreted by the Java interpreter that converts the bytecodes into
the machine language of the particular computer you are using.
Objectives:
In this section, we will be discussing on how to write, compile and run Java programs.
There are two ways of doing this; the first one is by using a console and a text editor. The second
one is by using NetBeans/JCreator/Eclipse which is an Integrated Development Environment
or IDE.
At the end of the lesson, the student should be able to:
• Create a Java program using any text editor
• Differentiate between syntax-errors and runtime errors
• Create a Java program using NetBeans/JCreator/Eclipse
ERRORS
What we've shown so far is a Java program wherein we didn't encounter any problems in
compiling and running.
Objectives
In this section, we will be discussing the basic parts of a Java program. We will start by
trying to explain the basic parts of the Hello.java program introduced in the previous section. We
will also be discussing some coding guidelines or code conventions along the way to help in
effectively writing readable programs.
C++-Style Comments
C++ Style comments starts with //. All the text after // are treated as comments.
For example,
// This is a C++ style or single line comments
C-Style Comments
C-style comments or also called multiline comments starts with a /* and ends with
a */. All text in between the two delimeters are treated as comments. Unlike C++ style
comments, it can span multiple lines.
For example,
/* this is an exmaple of a
C style or multiline comments */
Special Javadoc Comments
Special Javadoc comments are used for generating an HTML documentation for
your Java programs. You can create javadoc comments by starting the line with /** and
ending it with */. Like C-style comments, it can also span lines. It can also contain certain
tags to add more information to your comments.
For example,
/**
This is an example of special java doc comments used for \n
generating an html documentation. It uses tags like:
@author Florence Balagtas
@version 1.2
*/
JAVA IDENTIFIERS
Identifiers are tokens that represent names of variables, methods, classes, etc.
Examples: Hello, main, System, out.
Java identifiers are case-sensitive. This means that the identifier: Hello is not the same as
hello. Identifiers must begin with either a letter, an underscore “_”, or a dollar sign “$”.
Letters may be lower or upper case. Subsequent characters may use numbers 0 to 9.
Identifiers cannot use Java keywords like class, public, void, etc. We will discuss more
about Java keywords later.
JAVA LITERALS
Literals are tokens that do not change or are constant. The different types of literals in
Java are:
Integer Literals
Integer literals come in different formats: decimal (base 10), hexadecimal (base
6), and octal (base 8). In using integer literals in our program, we have to follow some
special notations. For decimal numbers, we have no special notations. We just write a
decimal number as it is. For hexadecimal numbers, it should be preceded by “0x” or
“0X”. For octals, they are preceded by “0”. For example, consider the number 12. It's
decimal representation is 12, while in hexadecimal, it is 0xC, and in octal, it is equivalent
to 014. Integer literals default to the data type int. An int is a signed 32-bit value. In some
cases, you may wish to force integer literal to the data type long by appending the “l” or
“L” character. A long is a signed 64-bit value. We will cover more on data types later.
Floating-Point Literals
Floating point literals represent decimals with fractional parts. An example is
3.1415. Floating point literals can be expressed in standard or scientific notations. For
example, 583.45 is in standard notation, while 5.8345e2 is in scientific notation. Floating
point literals default to the data type double which is a 64-bit value. To use a smaller
precision (32-bit) float, just append the “f” or “F” character.
Character Literals
Character Literals represent single Unicode characters. A Unicode character is a
16-bit character set that replaces the 8-bit ASCII character set. Unicode allows the
inclusion of symbols and special characters from other languages.
To use a character literal, enclose the character in single quote delimiters.
For example,
the letter a, is represented as ‘a’.
To use special characters such as a newline character, a backslash is used
followed by the character code. For example, ‘\n’ for the newline character, ‘\r’ for the
carriage return, ‘\b’ for backspace.
String Literals
String literals represent multiple characters and are enclosed by double quotes.
An example of a string literal is, “Hello World”.
An example is,
boolean result = true;
The example shown above, declares a variable named result as boolean type and
assigns it a value of true.
Textual – char
A character data type (char), represents a single Unicode character. It must have
its literal enclosed in single quotes(’ ’).
For example,
‘a’ //The letter a
‘\t’ //A tab
For example,
'\'' //for single quotes
'\"' //for double quotes
Although, String is not a primitive data type (it is a Class), we will just introduce
String in this section. A String represents a data type that contains multiple characters. It
is not a primitive data type, it is a class. It has it’s literal enclosed in double quotes(“”).
For example,
String message=“Hello world!”
Examples are,
2 //The decimal value 2
077 //The leading 0 indicates an octal value
0xBACC //The leading 0x indicates a hexadecimal value
Integral types have int as default data type. You can define its long value by
appending the letter l or L.
Examples are,
3.14 //A simple floating-point value (a double)
6.02E23 //A large floating-point value
2.718F //A simple float size value
123.4E+306D //A large double value with redundant D
In the example shown above, the 23 after the E in the second example is
implicitly positive. That example is equivalent to 6.02E+23.
VARIABLES
A variable is an item of data used to store state of objects. A variable has a data type
and a name. The data type indicates the type of value that the variable can hold. The variable
name must follow rules for identifiers.
Note: Values enclosed in <> are required values, while those values enclosed in []
are optional.
For example, suppose we have two variables with data type’s int and String.
int num = 10;
String name = "Hello"
Suppose, the illustration shown below is the actual memory of your computer,
wherein you have the address of the memory cells, the variable name and the data they
hold.
As you can see, for the primitive variable num, the data is on the actual location
of where the variable is. For the reference variable name, the variable just holds the
address of where the actual data is.
References:
JEDI Course Notes