Reviewer for Computer:
Lesson 1: Getting acquainted with Java
Object-oriented Programming – (OOP) it was conceived to model real-world
objects. In OOP, a class defines the common variables (attributes) and
methods (functions) of a set of Objects. An Object is an instance of a class.
For Example:
CLASS – Person
VARIABLES – Name, Age, Birthday
METHODS – Sing, Speak, Read
Class – template of an object
Benefits of Java
Java is simple
Java is reliable
Java is dynamic
Java is used in networking
Java is secure
Java is free
Java is portable
Compiling and executing a Java Program using an IDE
BlueJ – It is an Integrated Development Environment (IDE) that is freely
available on the internet. It is used to compile and run the Java applications
provided.
To download it, type in your browser:
http://www.bluej.org/download/download.html
STEPS IN USING BLUEJ TO COMPILE AND RUN JAVA APPLICATION
1. Run BlueJ by double-clicking the BlueJ icon on the desktop or running it from
Start > All programs > BlueJ > BlueJ
2. Click Advanced so that BlueJ window looks similar to the figure below
3. Click browse for a specific Java Version and browse the bin directory within your
Java installation folder. Click on the file named java and click Open.
4. Click the Java Version that appears and click Launch BlueJ
5. To create a new project, click Project > New Project
6. The New Project dialog box will appear. In the file name field, input the name of
your project. Click Create.
7. The BlueJ window will look similar to the following picture. Double-click this icon
to open README.TXT.
8. Type the appropriate information.
9. After updating the README.TXT, click Class > Close
10. Click Edit > New Class or press Ctrl + N at the same time
11. Type “Welcome” as the new class then Ok.
12. Double-click Welcome icon to open the Welcome class.
13. Delete all the text included within the editor and type the following:
//Welcome to Java
public class Welcome {
public void printWelcome ( ) {
System.out.println (“Welcome to Java!”);
}
}
14. Click Class > Close or press Ctrl + W at the same time
15. Create another class and name it Main.
COMPILING YOUR PROGRAM USING BLUEJ
To compile all classes in a specific package, go to the package to be compiled
and click Compile. Or click Tools > Compile or press Ctrl + K at the same time
EXECUTING YOUR PROGRAM USING BLUEJ
1. Right-click the main icon and select void main (String[] args).
2. Click Ok or click Tools > Compile or press Ctrl + K at the same time
3. The terminal window of BlueJ will appear to show the output
4. After you’re done, close all BlueJ windows
COMPILING AND RUNNING YOUR JAVA PROGRAM USING THE COMMAND
PROMPT
Java file can be encoded using any plain text editor (e.g. Notepad)
1. Open Notepad from Start > All Programs > Accessories and type Java code
from Welcome.java.
2. Open another instance of Notepad and type the Java code from Main.java
3. Save both files as Welcome.java and Main.java respectively in the bin directory
within your Java installation. When saving in Notepad, place the file name within
your quotation marks. When saving in notepad, place the filename within
quotation marks. The extension name in notepad is .txt, you must enclose the
filename within quotation marks to change the extension name to .java. Other
option is to save your Java program on a separate folder.
To compile java application using the Java Software Development Kit (SDK), we
use the javac command followed by the filename and its file extension.
Go to the bin directory within your java installation folder using the command line
prompt and press Enter after typing the following:
javac Welcome.java
If there are no errors encountered while the file was compiled, then a file with the
same name will be created with an extension “.class”
To run the java application, we use the java command followed by the filename
(without its extension). Press Enter after typing the following:
java Main
SOME COMMON PROGRAMMING ERRORS
Errors encountered when you compile your application are called compile-time
errors.
Errors encountered during program execution are called runtime errors.
The error is highlighted
The error message is shown on the status bar
BlueJ also provides its users with suggestions on the possible causes of
the detected error through dialog boxes which will appear if you click the
question mark.
To see the pictures, watch the videos in the google classroom.
Lesson 2: Understanding the Java Program
Here is our program in Welcome.java:
Line 1 - //Welcome to Java
Line 2 - public class Welcome {
Line 3 - public void printWelcome ( ) {
Line 4 - System.out.println (“Welcome to Java!”);
Line 5 - }
Line 6 - }
LINE 1 - //Welcome to Java
Any text that follows 2 slash symbols is considered a comment.
It is a good programming style to place comment within the code.
It is a single-line comment
A comment is read by the java compiler but as a command, it is actually ignored.
LINE 2 – public class Welcome {
When you declare a class public, it can be accessed and used by other classes.
Notice that there is also an open brace to indicate the start of the scope of the
class.
It defines the beginning of the Welcome class.
To declare class:
<modifier> class <class_name> {
LINE 3 - //public void
printWelcome ( ) {
Void is the return type for the printWelcome method. A method that returns void
returns nothing.
It shows the start of the method of the printWelcome ().
The syntax for defining a method is:
<modifier> <return_type> <method_name> (<argument_list>) {
<statements>;
}
LINE 4 – System.out.println(”Welcome to Java!’)
It shows how to print text in Java.
The println() method displays the message inside the parentheses on the screen,
and then placed the cursor on the next line. If you want your cursor to go to the
next available space after printing, use the print method instead of println.
LINE 5-6 - }
These lines contain two closing braces
The brace online 5 closes the method printWelcome() and the brace on line 6
closes the class Welcome().
Opening brace in line 3 is paired with the closing brace on line 5.
The opening brace on line 2 is paired with the closing brace on line 6.
ANALYZING MAIN.JAVA
LINE 1-3
These lines contain a multi-line comment.
Anything between slash & asterisk and asterisk slash is considered a comment.
Multi-line comment = /* (start of the comment), */ (end of the comment)
LINE 5
It declares the main class
LINE 6 – public static void main (String [ ] args)
Program execution starts from this line.
First function which java reads is the main function. The Java interpreter should
see these main method definitions as is, except for args which is user-defined.
LINE 7 – Welcome Greet = new welcome ( );
It shows how an object is defined in java.
Here, the object greet is created. The word “greet” is user-defined. You can even
have your name in its place.
The syntax for defining an object is:
<class_name> <object_name> = new<class_name> (<arguments>);
LINE 8 – Greet.printWelcome ( );
It illustrates how a method of class is called.
If you look at the welcome class, you will notice that we declared a method
named printWelcome. By declaring an instance of the welcome class, in this
case, the greet variable is an instance of the welcome class, courtesy of line 7,
you can execute a method within a specific class.
The basic syntax is:
<class_name>.<method_name> (<arguments>);
LINE 9 - }
LINE 10 - }
The brace on line 9 closes the main method.
The brace on line 10 indicates the end of the scope of the class main.
Lesson 3: Java names and Labels
Java Keywords – Keywords in Java are reserved for specific use.
All Java keywords are in lower case.
Java is case-sensitive. If you capitalize any of the keywords, it becomes an
identifier.
The Java compiler recognizes these keywords for their specific purpose so
programmers are not allowed to use them as identifiers.
Identifiers – are user-defined names for methods, variables, constants, and
classes. It starts with a letter, an underscore, or a dollar sign followed by
alphanumeric characters, and underscores.
Create identifiers that are descriptive of their purpose.
You identifier must contain no spaces.
If your identifier is composed of several words, capitalizing the first letter of each
word is a good programming practice.
Do not use keywords as identifiers.
Examples:
VALID IDENTIFIERS INVALID IDENTIFIERS
$rate 3_rate
LastName Name,pls!
Last_Name const
_main Full Name
DATA TYPES
Java has two sets of Data Types: Primitive and Reference (Non-primitive)
Below is a table of data types:
DATA TYPE EXAMPLES
string “hello world”
boolean true, false
char ‘A’, ‘z’, ‘\n’
byte 1
short 11
int 167
long 11167L
float 63.5F
double 63.5
String – Reference data type often used in many applications. It is actually a
class that let us store a sequence of characters.
Boolean –This is a logical data type. In java, it has only two possible values; true
or false.
Char – Data type used for single characters. When assigning literals to identifiers
declared as char, you need to enclose literals in single quotes.
Byte, short, int, and long – Hold integer values
DATA TYPE LENGTH RANGE
7 7
byte 8 bits -2 to 2 - 1
short 16 bits -215 to 215 - 1
int 32 bits -231 to 231 - 1
long 64 bits -263 to 263 - 1
Float and double are floating point literals (numbers with possible decimal
parts)
Floating point literals like 1.63 are considered double unless you explicitly
declare them as float by adding uppercase F at the end.
Float is 32 bits in length
Double is 64 bits long
Primitive Data Types – starts with small letters
Reference Data types – starts with capital letters
Variables
are identifiers whose values can be changed
Syntax:
<data_type> <identifier>;
Example of Java program using Variables is in the video in the Google
classroom.
Constants
are identifiers whose values never change once declared
Syntax:
final <type> <identifier> = <literal>;
Example of Java program using Constants is in the video in the Google
classroom.
Casting
A process of assigning a value or variable of a specific type to a variable of
another type.
Example of Java program of Casting is in the video in the Google classroom.
Downward Cast
Casting from a value that occupies more memory to a value that occupies less.
Not automatic in Java.
Lesson 4: Connecting Data Through Operators
Operators
These are symbols that perform logical or mathematical functions on operands
such as variables, constants, and objects.
Unary Operators
requires only one operand
change the value of operand without using an assignment operator
negation (-)
bitwise complement (~)
increment (++)
decrement (- -)
Binary Operators
require two operands
Arithmetic Operators
OPERATORS DESCRIPTION
+ Addition
- Subtraction
* Multiplication
/ Division
% Modulo
++ Increment
-- Decrement
Example of Java program using arithmetic operators is in the video in the Google
classroom.
Comparison / Relational Operators
All relational operators are binary operators primarily because they indicate the
relationship among two operands.
The result of a relational operation is a Boolean value either true or false
depending on whether the expression to be evaluated is true or false.
Example:
OPERATORS SAMPLES DESCRIPTION
> a >b a is greater than b
>= a>= b a is greather than or
equal to b
< a<b a is less than b
<= a <= b a is less than or equal to
b
== a==b a is equal to b
!= a!=b a is not equal to b
Example of Java program using Comparison / Relational operator is in the vdeo
in the Google classroom.
Conditional / Logical Operators
The result is either true or false depending on the expression whether to be
evaluated is true or false.
OPERATORS DESCRIPTION
! NOT
|| logical OR
&& logical AND
^ Exclusive OR
Truth Tables
Will help us demonstrate the result of a logical operation
NOT operator
Unary operator that negates the value of the operand.
Operand1 Result
! true false
! false true
OR operator
Binary operator that returns true if at least one of its operands is true.
Operand1 Operand2 Result
true || true true
true || false true
false || true true
false || false false
Bitwise Exclusive OR/XOR
Binary operator that returns true when both operands have different values
otherwise, it will return false.
Operand1 Operand2 Result
true ^ true false
true ^ false true
false ^ true true
false ^ false false
AND operator
Binary operator that returns true only when both operands are true otherwise, it
will return false.
Operand1 Operand2 Result
true && true true
true && false false
false && true false
false && false false
Example of Java program using logical operators is in the video in the Google
classroom.
Shortcut / Assignment Operators
These short-hand operators include the assignment operator equal with the
arithmetic operator.
Operators Description
+= Assignment with Addition
-= Assignment with Subtraction
*= Assignment with Multiplication
/= Assignment with Division
%= Assignment with Modulo
Example of java program using Short-hand operators is in the video in the
Google classroom.
Operator Precedence
Precedence – refers to the order which operators are executed.
OPERATOR PRECEDENCE TABLE
()
++ -- ! -
* / %
+ -
< <= > >=
== !=
^
&&
||
= *= /= %= += -=
Unary operators and NOT operators are performed from right to left.
The rest of the operators are performed from left to right.
Java Programs
For the Java Programs, please refer to our Hands-On Activities and our
Computer Book: Programming and Databases.