You are on page 1of 12

IT106: Intermediate Programming

Compiled Lesson 1: Introduction to JAVA Programming

LEARNING OBJECTIVES
we are in the Technology Platform. We will focus on
Java Programming.
At the end of this module the students will be able
to.
1. Define the basic programming terminology 2.
Describe the procedural and object-oriented
programming concepts
3. Describe the features of the java
programming language
4. Analyze the Java application that produces
console output
5. Add comments to a Java class
6. Save, compile, run and modify a Java
application

TOPIC
OUTLINE

Introduction to JAVA Programming


● Concept of Programming
● Features of Java Programming
● Syntax of Java Program
● Applications Used to create Java Programs
● Steps in Running a Java Program in
command Prompt

Concept of Programming
And History of JAVA
OVERVIEW Programming
Java Program is a class-based, object-oriented
programming language that is designed to have as
few implementation dependencies as possible. LEARNING ABOUT PROGRAMMING

• Program
ACTIVATING – Set of written instructions that tells
PRIOR KNOWLEDGE computer what to do
Have you heard about Java, a Java man? • Machine language
How about Java rice or Java Programming? – Most basic circuitry-level language
– Low-level programming language
Since
• High-level programming language
Do you want to know more about Java – Allows you to use vocabulary of
Programming? reasonable terms
• Syntax
– Rules of language • Compiler or interpreter
• Program statements – Translates language statements
– Similar to English sentences into machine code
– Carry out tasks of program • Syntax error

Compiled Lesson 1 – Introduction to JAVA Programming | Page 1 of 10


– Misuse of language • Object-oriented programming differs from
– Misspelled programming language traditional procedural programming – Basic
word concepts
• Debugging • Polymorphism
– Freeing program of all errors • Inheritance
– Logic errors • Encapsulation
– Also called semantic errors
– Incorrect order or procedure

COMPARING PROCEDURAL AND UNDERSTANDING CLASSES, OBJECTS


OBJECT ORIENTED PROGRAMMING
AND ENCAPSULATION
CONCEPTS

1. PROCEDURAL PROGRAMMING • Class


– Sets of operations executed in – Describes objects with common
sequence properties
– Variables – Definition
• Named computer memory – Instance
locations that hold values
– Procedures • Attributes
• Individual operations – Characteristics that define object –
grouped into logical units Differentiate objects of same class –
Value of attributes is object’s state
2. OBJECT-ORIENTED PROGRAMS
• Objects
– Create classes
– Specific, concrete instance of a
– Create objects from classes
class
– Create applications

• Method
• Object-oriented programming was used most frequently for two major types of
applications code
– Computer simulations – Similar to procedure
– Graphical user interfaces (GUIs)
• Not all object-orientated • Encapsulation
programs written to use GUI – Refers to hiding of data and
– Self-contained block of program methods within object

Compiled Lesson 1 – Introduction to JAVA Programming | Page 2 of 10


– Provides security
– Keeps data and methods safe from
inadvertent changes
Compiled Lesson 1 – Introduction to JAVA Programming | Page 3 of 10
UNDERSTANDING INHERITANCE AND
POLYMORPHISM • Inheritance
– Important feature of object-oriented
programs • Applets
– Classes share attributes and – Programs embedded in Web page
methods of existing classes but
with more specific features • Java applications
– Helps you understand real-world – Called Java stand-alone programs
objects – Console applications
• Polymorphism • Support character output
– Means “many forms”
– Allows same word to be interpreted – Windowed applications
correctly in different situations • Menus
based on context • Toolbars
FEATURES OF THE JAVA • Dialog boxes

PROGRAMMING UNDERSTANDING THE


LANGUAGE SYNTAX OF JAVA
• Java PROGRAM
– Developed by Sun Microsystems
– Object-oriented language ANALYZING A JAVA APPLICATION
– General-purpose THAT PRODUCES CONSOLE OUTPUT
– Advantages
• Security features • Even simplest Java application
• Architecturally neutral – Involves fair amount of confusing
– Can be run on wide variety of syntax
computers • Print “First Java application” on screen
– Does not execute instructions on
computer directly
– Runs on hypothetical computer
known as Java virtual machine
(JVM)
• Source code
– Programming statements written in
high-level programming language
UNDERSTANDING THE STATEMENT THAT
PRODUCES THE OUTPUT
• Bytecode
– Statements saved in file • Literal string
– Java compiler converts source code – Will appear in output exactly as
into binary program entered
– Written between double quotation
• Java interpreter marks
– Checks bytecode and • Arguments
communicates with operating – Pieces of information passed to
system method
– Executes bytecode instructions line
by line within Java virtual machine • Method
– Requires information to perform its
task
• System class
– Refers to the standard output device
for a system

JAVA PROGRAM TYPES


Compiled Lesson 1 – Introduction to JAVA Programming | Page 4 of 10

UNDERSTANDING THE FIRST CLASS


• Everything used within Java program must
be part of a class
• Define Java class using any name or
identifier
• Requirements for identifiers
– Must begin with:
• Letter of English
alphabet
• Or non-English
letter (such
as α or π)
• Underscore
• Dollar sign
– Cannot begin with digit
– Can only contain:
• Letters
• Digits
• Underscores
• Dollar signs
– Cannot be Java reserved keyword
– Cannot be true, false, or null
• Access specifier
– Defines how class can be accessed
Indent Style

Compiled Lesson 1 – Introduction to JAVA Programming | Page 5 of 10


• For every opening curly brace ( { ) in a Java
program there must be a corresponding
closing curly brace ( } )
• Placement of the opening and closing curly
braces is not important to the compiler

UNDERSTANDING THE MAIN() METHOD

• static
– Reserved keyword ADDING COMMENTS TO A JAVA CLASS
– Means method accessible and
• Program comments
usable even though no objects of
– Nonexecuting statements added to
class exist
program for documentation
• void
– Use to leave notes for yourself or
– Use in main() method header
others
– Does not indicate main() method
– Include author, date, class’s name or
empty
function
– Indicates main() method does not
• Comment out a statement
return value when called
– Turn it into a comment
– Doesn’t mean main() doesn’t
– Compiler does not translate and the
produce output
JVM does not execute its command
Types of Java comments
– Line comments
• Start with two forward slashes (//)
• Continue to end of current line
• Do not require ending symbol
– Block comments
• Start with forward slash and asterisk (/*) •
End with asterisk and forward slash (*/) –
Javadoc comments
• Special case of block comments • Compiling a Java class
• Begin with slash and two asterisks (/**) • – Compile source code into bytecode –
End with asterisk and forward slash (*/) •
Translate bytecode into executable
Use to generate documentation
statements
• Using Java interpreter
– Type javac First.java

• Compilation outcomes
– javac unrecognized command
– Program language error messages
– No messages indicating successful
SAVING, COMPILING, RUNNING AND completion
MODIFYING A JAVA APPLICATION
• Reasons for error messages
• Saving a Java class
– Misspelled command javac
– Save class in file with exactly same name – Misspelled filename
and.java extension – Not within correct subfolder or
• For public classes subdirectory on command line.
• Class name and filename must – Java not installed properly
match exactly

Compiled Lesson 1 – Introduction to JAVA Programming | Page 6 of 10


RUNNING A JAVA APPLICATION
• Modify text file that contains existing class •
• Run application from command line Save file with changes
– Type java First – Using same filename
• Shows application’s output in command • Compile class with javac command
window • Interpret class bytecode and execute class
• Class stored in folder named Java on C drive using java command

CREATING A JAVA APPLICATION


THAT PRODUCES GUI OUTPUT

• JOptionPane
– Produce dialog boxes
• Dialog box
– GUI object resembling window
– Messages placed for display
• import statement
– Use to access built-in Java class
• Package
Modifying a Java Class – Group of classes
– Also called the Java class library
– Prewritten Java classes

LEARNING
ACTIVITY 1

Objectives: Apply the understanding of creating first


Java Program.
CORRECTING ERRORS AND FINDING HELP
• First line of error message displays: Task: Create a J Displaying: Create a program that
– Name of file where error found will display their basic information (Resume) using
– Line number notepad++ and CMD or JAVA Compiler IDE from
– Nature of error Mobile Device
• Next lines identify:
– Symbol Resources: Mobile Devices (Smart Phone),
– Location
Laptop, Computer.
• Compile-time error
– Compiler detects violation of
language rules
– Refuses to translate class to
machine code
• Parsing
SUMMARY
Let us see if you can remember the main points
– Process compiler uses to divide
source code into meaningful raised in this lesson. Below is a summary of these
portions points:
• Logic error
– Syntax correct but produces ⮚ Access modifier: defines the circumstances
incorrect results when executed under which a class can be accessed and
– Usually more difficult to find and the other classes that have the right to use a
resolve class.
• Java API

Compiled Lesson 1 – Introduction to JAVA Programming | Page 7 of 10


⮚ Allman style: the indent style in which curly appear on a line by themselves, on a line before
braces are aligned and each occupies its own executable code, or on a line after executable
line; it is named for Eric Allman, a programmer code. Block comments can also extend across
who popularized the style. as many lines as needed.
⮚ Application: a stand-alone, executable ⮚ Bytecode: consists of programming
program. statements that have been compiled into
binary format.
⮚ Architecturally neutral: used to write a
program that runs on any platform ⮚ Calls: A procedural program calls a series of
(operating system). procedures to input, manipulate, and output
values.
⮚ Arguments: information passed to a method
so it can perform its task. ⮚ Class definition: describes what attributes its
⮚ Attributes: the characteristics that define an objects will have and what those objects
will be able to do.
object as part of a class.
⮚ Class: describes a group or collection of
⮚ Block comments: start with a forward slash
objects with common properties.
and an asterisk (/*) and end with an asterisk
and a forward slash (*/ ). Block comments can ⮚ Clean build: created when you delete all
previously compiled versions of a class Java class that is contained in a package.
before compiling again.
⮚ Inheritance: the ability to create classes that
⮚ Comment out: turn a statement into a share the attributes and methods of existing
comment so the compiler will not execute classes but with more specific features.
its command.
⮚ Instance: an existing object of a class.
⮚ Compiler, or interpreter: a program that
⮚ Interactive applications: those in which a user
translates language statements into
communicates with a program by using an
machine code.
input device.
⮚ Compile-time error: one in which the
⮚ Java API: the application programming
compiler detects a violation of language
interface; a collection of information about
syntax rules and is unable to translate the
how to use every prewritten Java class.
source code to machine code.
⮚ Java applets: Java programs that are
⮚ Console applications: support character
embedded in a Web page.
output to a computer screen in a DOS
window. ⮚ Java applications: stand-alone Java
⮚ Debugging: process to remove errors from a programs.
program.
⮚ Java interpreter: a program that checks the
⮚ Dialog box: a GUI object resembling a bytecode and communicates with the
window in which you can place messages operating system, executing the bytecode
you want to display. instructions line by line within the Java
Virtual Machine.
⮚ Encapsulation: refers to the hiding of data
and methods within an object. ⮚ Java Virtual Machine (JVM): a hypothetical
(software-based) computer on which Java
⮚ Executing: carry out a statement. ⮚
runs.
FAQs: frequently asked questions.
⮚ Java: programming language developed by
⮚ High-level programming language: allows Sun Microsystems as an object-oriented
you to use a vocabulary of reasonable terms, language used both for general-purpose
such as “read,” “write,” or “add,” business applications and for interactive,
instead of the sequences of on and off Web-based Internet applications.
switches that perform these tasks.
⮚ Javadoc comments: a special case of block
⮚ Identifier: a name of a program component comments. They begin with a forward slash
such as a class, object, or variable.
⮚ import statement: used to access a built-in

Compiled Lesson 1 – Introduction to JAVA Programming | Page 8 of 10


and two asterisks (/**) and end with an current line. Line comments can appear on a
asterisk and a forward slash (*/). You can line by themselves or at the end of a line
use javadoc comments to generate following executable code.
documentation with a program named
⮚ Literal string: a series of characters that
javadoc.
appear exactly as entered. Any literal string
⮚ JDK: the Java Development Kit. in Java appears between double quotation
marks.
⮚ JOptionPane: the Java class named allows
you to produce dialog boxes. ⮚ Logic error: occurs when a program compiles
successfully but produces an error during
⮚ K & R style: the indent style in which the
execution.
opening brace follows the header line; it is
named for Kernighan and Ritchie, who wrote ⮚ Logic: executing the various statements and
the first book on the C programming procedures in the correct order to produce
language. the desired results.
⮚ Line comments: start with two forward ⮚ Low-level programming language: written to
slashes (//) and continue to the end of the correspond closely to a computer
processor’s circuitry. by a computer program.
⮚ Machine language: program written in ⮚ Program: set of instructions that you write to
circuitry-level language as a series of on and tell a computer what to do.
off switches.
⮚ Program comments: nonexecuting
⮚ Method: a self-contained block of program statements that you add to a Java file for
code, similar to a procedure. the purpose of documentation.
⮚ Object-oriented programs: involves creating ⮚ Program statements: similar to English
classes, creating objects from those sentences; they carry out the tasks that
classes, and creating applications that use programs perform.
those objects. Thinking in an object-oriented
⮚ Run-time error: occurs when a program
manner involves envisioning program
compiles successfully but does not execute.
components as objects that are similar to
concrete objects in the real world; then, you ⮚ SDK: a software development kit, or a set of
can manipulate the objects to achieve a tools useful to programmers.
desired result.
⮚ Semantic errors: occur when you use a
⮚ Objects: instances of a class; they are made correct word in the wrong context in
up of attributes and methods. program code.
⮚ Package: contains a group of built-in Java ⮚ Source code: consists of programming
classes. statements written in a high-level
programming language.
⮚ Parsing: the process the compiler uses to
divide source code into meaningful portions ⮚ Standard output device: normally the
for analysis. monitor.
⮚ Pascal casing: using an uppercase letter to ⮚ State: the values of the attributes of an
begin an identifier and starting each new object.
word in an identifier.
⮚ static: reserved keyword means that a
⮚ Passing: sending arguments to a method. method is accessible and usable even
though no objects of the class exist.
⮚ Polymorphism: describes the feature of
languages that allows the same word to be ⮚ Syntax error: a programming error that occurs
interpreted correctly in different situations when you introduce typing errors into your
based on the context. program or use the programming language
⮚ Procedural programming: a style of incorrectly. A program containing syntax
errors will not compile.
programming in which sets of operations
are executed one after another in ⮚ Syntax: the rules of the language.
sequence.
⮚ Procedures: sets of operations performed

Compiled Lesson 1 – Introduction to JAVA Programming | Page 9 of 10


⮚ Unicode: an international system of
character representation.
⮚ Variables: named computer memory
locations that hold values that might vary.
⮚ void: the keyword, when used in a method
header, indicates that the method does not
return any value when it is called.
⮚ Whitespace: any combination of nonprinting
characters; for example, spaces, tabs, and
carriage returns (blank lines).
⮚ Windowed applications: create a graphical
user interface (GUI) with elements such as
menus, toolbars, and dialog boxes.
⮚ “Write once, run anywhere” (WORA): a
slogan developed by Sun Microsystems to
describe the ability of one Java program
version to work correctly on multiple
platforms.

REFERENCES
Introduction to Java Programming Revised
Edition Pomperada, J.R. (2018)

Introduction to Java Programming


Cabaluna, MD., Tamayo, J., Gibaga,
G., Vicente, C., Baligod, M., Chua, M.,
Rodriguez, E., (2019)

Compiled by:

LORIE JELENE R. MILLON-PARAGAS, LPT,


MIT Faculty, College Information
And Technology Education

Compiled Lesson 1 – Introduction to JAVA Programming | Page 10 of 10

You might also like