Applying Basics of Java Language: Lesson

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 13

Let Us Study

LESSON Applying Basics of


3 Java Language

https://pixabay.com/vectors/computer-desktop-workstation-office-158675/
Image by  OpenClipart-Vectors from Pixabay 

Java Technology and Java Programs

Introduction:

Java technology is both a programming language and a platform.


As a programming language, Java can create all kinds of applications that
you could create and uses Java Virtual Machine for its platform.

This lesson discourses what you need to understand about Java’s


history and how it came about to what it is today.

You will also learn different phases a java program undergoes


which are done using the Java Development Kit.
1
Trade Terms to Remember:

bytecode- compiled format for Java programs and executed by Java


Virtual Machine (JVM).
class loader- part of the Java Runtime Environment that dynamically
loads Java classes into JVM usually, classes are only loaded on
demand
source code- programming commands that are made by a programmer
with a text editor and saved in a file
JVM- Java Virtual Machine

Let Us Study
a

History of Java

James Gosling, Mike Sheridan, and Patrick Naughton initiated the


Java language project in June 1991., the small team of Sun engineers
called Green Team.

Originally designed for small, embedded systems in electronic


appliances like set-top boxes, televisions. Firstly, it was
called "Greentalk" by James Gosling, and file extension was .gt. After
that, it was called Oak, as Oak is a symbol of strength and chosen as a
national tree of many countries like U.S.A., France, Germany, Romania,
etc. Oak was developed as a part of the Green project.

In 1995, Oak was renamed as "Java" because it was already a


trademark by Oak Technologies. Java originates from a sort of espresso
bean.

Original motivation was the need for platform independent


language that could be embedded in various consumer electronic
products like toasters and refrigerators. It was awarded as one of the Ten
Best Products of 1995 by the TIME MAGAZINE.

First project developed using Java was a personal hand-held remote


control named Star 7.

Currently, Java is used in internet programming, mobile devices,


games, e-business solutions, etc.

2
What is Java Technology?

 As a programming language

It enables programmers to write computer instructions using


English-based commands instead of having to write in numeric codes
or binary numbers.

 As a development environment

Java technology provides you with a large suite of tools: a


compiler, an interpreter, a documentation generator, a class file
packaging tool, and so on.

 An application environment

Java technology applications are typically general-purpose


programs that run on any machine where the Java runtime
environment (JRE) is installed. (The Java Runtime
Environment (JRE) is a set of software tools for development
of Java applications.)

 A deployment environment

Two main deployment environments

 JRE supplied by the Java 2 Software Development Kit


(SDK)- contains the complete set of class files for all the
Java technology packages, which includes basic
language classes, GUI component classes, and so on

 web browser - Most commercial browsers supply a


Java technology interpreter and runtime environment.

The Java Virtual Machine

The Java Virtual Machine is an imaginary machine that is


implemented by emulating software on a real machine. It enables a
computer to run Java programs as well as programs written in other
languages that are also compiled to Java bytecode. A bytecode is a
special machine language that can be understood by the Java Virtual
Machine (JVM).

The JVM provides the hardware platform specifications to which


you compile all Java technology code. This specification enables the Java
software to be platform-independent because the compilation is done for a
generic machine known as the JVM.

Garbage Collection

3
Java garbage collection is the process by which Java programs
perform automatic memory management. It means the programmer does
not need to explicitly mark objects to be deleted. The garbage collection
implementation lives in the JVM. Each JVM can implement garbage
collection however it pleases; the only requirement is that it meets the
JVM specification. 

Phases of a Java Program

 Phase 1. Creating and editing a file using an editor is done by the


programmer. This is where the programmer types the needed
rectifications and save the program on a storage device as .java file
extension.
 Phase 2. Java command is used by the programmer to compile a
program. In this phase, if you are going to compile a program called
Hello.java. In this phase, if you are going to compile a program
called Hello.java, you would type: Javac Hello.java
 Phase 3. Before loading performed, the programmer will have to
place the program in the memory before it can implement a
process. The .class files containing the program’s bytecodes will be
taken by the class loader and transmits it to memory.
 Phase 4. Bytecodes are examined by bytecode verifier to validate
that it does not defy Java restrictions. It ensures that Java
programs entering over the network does not harm the user’s files
or system.
 Phase 5. The bytecodes stated in the program will be executed by
the JVM. As they are interpreted, JVM analyzes the bytecodes.

Creating Executable Java Applications


Every time you use a computer, you run many applications that
will do the tasks for you. Email software, which helps you in sending and
receiving email, your web browser that lets you open web pages, these are
just examples of an application. Programmers make those applications by
creating computer programs.
A Java application is a computer program that performs when you
utilize the java command to open the Java Virtual Machine (JVM).

4
Dissecting a Java Program
Below is an example of a java program where its output
“Hello world” is printed on the screen. Make sure to notice each line
of the code and its explanation. It is case sensitive so make sure to
notice the capital letters in the code.

1 public class Hello{


2
3 /**
4 * My first java program
5 */
6 public static void main (String[] args) {
7 //prints the string “Hello world” on screen
8 System.out.println(“Hello world!”);
9 }
10 }

Where:
Line 1 –Java programs must have at least one class declaration
that is defined.
Indicates the declaration of the name of the class which
is Hello.
class – defines the behaviors and characteristic of an
object
public- an access specifier which indicates that class
Hello is accessible to other classes from other packages
(packages are a collection of classes)
{ A left brace indicates the beginning of the body of a
class declaration
Line 2- Blank lines can be used so that programs are easier to
read.
Line 3,4,5- Indicates a multiline comment. It is not part of the
program itself but used for documentation purposes.
A comment is indicated by delimiters /* and */ is
ignored by the Java compiler and are treated as comments.
(There will be no effect on the program)
Line 4- comment within the delimiters
Line 6- Starting point of a Java application. Indicates the name
of one method in Hello which is the main method
Line 7- Another way to write a comment //. Again, this is
ignored by the Java compiler and is treated as comments.
Line 8- Command that prints the text enclosed by quotation on
screen
Line 9- The right brace ends the main method.
5
Line 10- The right brace ends the class method.

Java programs should always end with the .java extension.


Filenames should match the name of your public class. So if the
name of your public class is Hello, you should save it in a file
called Hello.java. You should write comments in your code
explaining what a certain class does, or what a certain method do.

Java statements and blocks


A statement consists of one or more lines of code terminated by a
semicolon (;).
Example: System.out.println(“Hello world!”);
A block consists of one or more statements bounded by an
opening and closing curly braces that groups the statement as one unit.
Example:
Public static void main(String[] args){
System.out.println(“Hello”);
System.ou.println(“world”);
}

You can place the opening curly brace in line with the statement,

Or next in line:

Should indent the next statement after the start of a block;

Working with Data Types

6
Primitive Data Types

The Java programming language defines eight primitive data types.


The following are, boolean (for logical), char (for textual), byte, short, int,
long (integral), double and float (floating point). They are also referred to
as simple types. They are divided into four groups namely:

1. Integers/Integral – consists of byte, short, int and long that


represent signed whole numbers.

Name or
Type Integer Length Range
-9,223,372,036,854,775,808 to
long 64 bits -263 to 263 -1 9,223,372,036,854,775,807

int 32 bits -231 to 231-1  -2,147,483,642 to 2,147,483,641

short 16 bits -215 to 215-1  -32,768 to 32,767

byte 8 bits -27 to 27 -1  -128 to 127

2. Floating point numbers - Floating point types has double as


default data type. Floating-point literal includes either a
decimal point or one of the following,

E or e //(add exponential value)


F or f //(float)
D or d //(double)

Example
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

7
3. Characters such as char are symbols in a character set that
are similar to letters and numbers.
Example
char letterA = 'A‘ //The letter A

Where: char is a data type


letterA is a variable
A is the value of letterA

4. Boolean represents true or false values


Example
boolean result = true;

where: boolean is a data type


result is a variable
true is the value of result

Declaring a Variable

A variable is an item of data used to store state of objects. It is a


name for a value stored in memory and is used in programs so that
values can be represented with meaningful names.
A variable has a data type and a name.
Data type- indicates the type of value that the variable can hold.
variable name- or the identifier.
Ex. int length; //int is the data type, and length is the
variable

To declare a variable:
Ex. <data type> <name> [=initial value];

A variable must be declared before it is used. And it is always good


to initialize as you declare them. Descriptive names are encourage to use
as variables. Declare one variable per line of code. For example, the
variable declarations, example:
double exam=0;
double quiz=10;
double grade = 0;
is preferred over the declaration,
double exam=0, quiz=10, grade=0;

8
Let Us Practice

ACTIVITY 1.1 - WORD SCRAMBLES

Direction: Unscramble the words below to get the exact word/s per item.
Use the given clues as your guide. Write your answers on your activity
notebook or any separate sheet and mark “ACTIVITY 1.1” at the top
before answering.

CLUES SCRAMBLED YOUR


WORD/S ANSWER
VIARALBE
1. It is an item of data used to store
state of objects
2. Indicates the type of value that ATAD EYTP
the variable can hold
3. It consists of byte, short, int and ITNGEARL
long that represent signed whole
numbers.
4. Variable is also known as; DNEITIEFIR
5. Represents true or false value. NOOLBEA

6. Consists of one or more CKOLB


statements bounded by an
opening and closing curly braces
that groups the statement as one
unit .
7. Consists of one or more lines of ETSATENMT
code terminated by a semicolon (;)
8. Is a special machine language ETYBEDOC
that can be understood by the
Java Virtual Machine (JVM).
9. A literal that represent single TERCARHAC
Unicode characters
10. // and MTSENMCO
/**/ are called:

9
Let Us Practice

ACTIVITY 1.2 – TRUE OR FALSE

Direction: Determine if the statement is true or false. Write your


answers on your activity notebook or any separate sheet and mark
“ACTIVITY 1.2” at the top before answering.

1. // Indicates a comment line.


2. Blank lines can be used to so that programs are easier to read.
3. Java programs must have at least one class declaration that is
defined.
4. A left brace indicates the beginning of the body of a class
declaration.
5. System.out allows Java applications to display characters in the
command window.
6. A statement consists of one or more lines of code terminated by a
colon.
7. A variable name is also called an identifier.
8. Data types could either be primitive or non primitive.
9. Char data type returns a true or false value.
10. Java programs should always end with the .gt extension

Let Us Practice more

ACTIVITY 1.3 - ESSAY

Direction: Explain the following in your own words. Write your answers on
your activity notebook or any separate sheet and mark “ACTIVITY 1.3” at
the top before answering.

1. Explain in your own words your understanding about a platform-


independent programming.
___________________________________________________________________
___________________________________________________________________
___________________________________________________________________
Let Us Remember

FILL IN THE BLANKS

Direction: Below is a short program that will print “hi” “Good


day!”.Determine the missing word/argument/command/sign. Write your
answers on your activity notebook or on any separate sheet and mark “Let
Us Remember” at the top before answering.

1 public __________ Greet _____

2 ____static void main(String[] args)___

3 ___ prints a text

4 System.out.println____________;

5 _______out.println___________;

6 ______

7 _____

Let Us Assess

ITEM IDENTIFICATION

Direction: Read each item carefully and select the correct phase from
the box that corresponds to the definition. Write your answers on your
activity notebook or on any separate sheet and mark “Let Us Assess” at the
top before answering.

Phase 1 Phase 2 Phase 3 Phase 4 Phase 5


__________1. It is the phase where it translates the Java source code
into bytecodes that represent the tasks to be performed.
__________2. It is the phase where programs perform the actions
specified by the program.
__________3. It is the phase layer where you type a Java program
(typically referred to as source code) using the editor, make any necessary
corrections, and save the program on a secondary storage device.
__________4. It is the phase where program examines their bytecodes
to ensure that they are valid and do not violate Java’s security
restrictions.
__________5. It is the phase where program must be placed in
memory before it can execute a process.

Let Us Enhance

Activity 1.4 The Tree

Direction: Create a class named: TheTree. The program should


output the following lines on the screen:
I think that I shall never see,
a poem as lovely as a tree.
A tree whose hungry mouth is pressed
Against the Earth’s sweet flowing breast.

Write your answers on your activity notebook or on any separate sheet


and mark “Let Us Enhance” at the top before answering.

Let Us Reflect

The lessons on this module have contributed knowledge in many


ways. It enabled you to familiarized the different devices that made up a
computer system.

Write your reflection here about the topic:


___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________
___________________________________________________________________________

Answers key to Activities

Let Us Enhance

public class TheTree{


public static void main(String[] args){
System.out.println(“I think that I shall never see,”);
System.out.println(“a poem as lovely as a tree. “);
System.out.println(“A tree whose hungry mouth is pressed”);
System.out.println(“Against the Earth’s sweet flowing breast.”);

}
}

Let Us Assess Let Us Remember Let Us Practice 1.3


1. 1. class { Essay:
2 2. public {
3 3. //
4 4. (“hi”);
5 5. System.out.println(“Good
day”);
6. }
7. }

Let Us Practice 1.2 Let Us Practice 1.1 Let Us Try


1. TRUE 1. VARIABLE Pretest 1.1
2. TRUE 2. DATA TYPE 1.
3. TRUE 3. INTEGRAL 2.
4. TRUE 4. IDENTIFIER 3.
5. BOOLEAN 4.
5. FALSE
6. BLOCK 5.
6. FALSE 6.
7. TRUE 7. STATEMENT
7.
8. BYTECODE
8. TRUE 8.
9. CHARACTER
9. FALSE 9.
10. COMMENTS
10. FALSE 10.

You might also like