Chapter 1
Creating
Java Programs
Objectives
• Define basic programming terminology
• Compare procedural and object-oriented
programming
• Describe the features of the Java programming
language
• Analyze a Java application that produces console
output
Java Programming, Eighth Edition 2
Objectives (cont’d.)
• Compile a Java class and correct syntax errors
• Run a Java application and correct logic errors
• Add comments to a Java class
• Create a Java application that produces GUI output
• Find help
• RESOURCES:
– https://www.jdoodle.com/online-java-compiler
– https://www.codiva.io/#
– http://www.crunchytricks.com/2016/07/offline-java-compilers.html
Java Programming, Eighth Edition 3
Think … Java
Java Programming, Eighth Edition 4
1.1 Learning Programming
Terminology
• Computer program
– A set of written instructions that tells the computer what
to do
• Machine language
– The most basic circuitry-level language
– A low-level programming language
Java Programming, Eighth Edition 5
1.1 Learning Programming
Terminology
• High-level programming language
– Allows you to use a vocabulary of reasonable terms, such
as read, write, input…
• Syntax
– A specific set of rules for the language
• Program statements
– Similar to English sentences
– Commands to carry out program tasks
Java Programming, Eighth Edition 6
1.1 Learning Programming Terminology
• Compiler or interpreter
– Translates language statements into machine code
– A compiler translates an entire program before carrying out any
statements, or executing them,
– interpreter translates one program statement at a time, excuting a
statement as soon as it is translated
• Syntax error
– Misuse of language rules
– A misspelled programming language word
Java Programming, Eighth Edition 7
1.1 Learning Programming Terminology
• Bug
– Error in a program
• Debugging
– Freeing program of all errors
• Logic errors
– Also called semantic errors (Language related errors)
– Incorrect order or procedure
– The program may run but provide inaccurate output
– Correcting logic errors is much more difficult than correcting syntax
errors.
Java Programming, Eighth Edition 8
The program
development Process
Java Programming, Eighth Edition 9
Two Truths & A Lie
Java Programming, Eighth Edition 10
Comparing Procedural and Object-Oriented
Programming Concepts
• Two popular approaches to writing computer programs are procedural
programming and object-oriented programming.
• Procedural programming
– Sets of operations executed in sequence
– Variables
• Named computer memory locations that hold values
– Procedures
• Individual operations grouped into logical units
• A procedural program defines the variable memory locations and then calls a series
of procedures to input, manipulate, and output the values stored in those
locations.
• A single procedural program often contains hundreds of variables and procedure
calls.
• Procedures are also called module , methods, functions, and subroutines.
11
How to Build Java Program
Java Programming, Eighth Edition 12
Comparing Procedural and Object-Oriented
Programming Concepts
• Object-oriented programs
– Create classes
• Blueprints for an object
– Create objects from classes
– Create applications
13
Comparing Procedural and Object-Oriented
Programming Concepts
• Object-oriented programming was used most frequently for
two major types of applications
– Computer simulations
– Graphical user interfaces (GUIs)
• Not all object-oriented programs are written to use a GUI
• Object-oriented programming differs from traditional
procedural programming
• Polymorphism
• Inheritance
• Encapsulation
Java Programming, Eighth Edition 14
1.2 Understanding Classes, Objects, and
Encapsulation
• Class
– Describes objects with common properties.
– a class definition exists before any objects are created from it.
– A class definition describes what attributes its objects will have
and what those objects will be able to do.
• Attributes
– Characteristics that define an object
– Differentiate objects of the same class
– The value of attributes is an object’s state
• Objects
– Specific, concrete instances of a class
Java Programming, Eighth Edition 15
1.2 Understanding Classes, Objects, and
Encapsulation
Java Programming, Eighth Edition 16
1.2 Understanding Classes, Objects, and
Encapsulation
• Creating an instance is called instantiation.
• You can create objects from classes that you write and from classes
written by other programmer .. including Java's creators.
• The values contained in an object's properties often differentiate instances
of the same class from one another
• The values of the properties of an object are referred to as the object's
state.
Java Programming, Eighth Edition 17
EXAMPLE OF CLASS & OBJECT
Java Programming, Eighth Edition 18
1.2 Understanding Classes,
Objects, and Encapsulation
• Method
– A self-contained block of program code that carries out an
action
– Similar to a procedure
• Encapsulation
– Conceals internal values and methods from outside sources.
• Concealing data is sometimes called information hiding, and concealing
how methods work is implementation hiding
– Provides security
– Keeps data and methods safe from inadvertent
(unintended) changes 19
1.2 Understanding Inheritance and
Polymorphism
• Inheritance
– An important feature of object-oriented programs
– Classes share attributes and methods of existing classes
but with more specific features
– Helps you understand real-world objects
• Polymorphism
– Means literally “many forms”
– Allows the same word to be interpreted correctly in
different situations based on context
Java Programming, Eighth Edition 20
1.2 Understanding Inheritance and
Polymorphism
Java Programming, Eighth Edition 21
1.3 Features of the Java Programming
Language
• Java
– Developed by Sun Microsystems
– An object-oriented language
– General-purpose
– Advantages
• Security features
• Architecturally neutral
Java Programming, Eighth Edition 22
Features of the Java Programming
Language (cont’d.)
• Java (cont’d.)
– Can be run on a wide variety of computers
– Does not execute instructions on the computer directly
– Runs on a hypothetical computer known as a Java Virtual
Machine (JVM)
• Source code
– Programming statements written in high-level
programming language
Java Programming, Eighth Edition 23
Features of the Java Programming
Language (cont’d.)
• Development environment
– A set of tools used to write programs
• Bytecode
– Statements saved in a file
– A binary program into which the Java compiler converts
source code
• Java interpreter
– Checks bytecode and communicates with the operating
system
– Executes bytecode instructions line by line within the Java
Virtual Machine
Java Programming, Eighth Edition 24
Features of the Java Programming
Language (cont’d.)
Java Programming, Eighth Edition 25
Java Program Types
• Applets
– Programs embedded in a Web page
• Java applications
– Called Java stand-alone programs
– Console applications
• Support character output
– Windowed applications
• Menus
• Toolbars
• Dialog boxes
Java Programming, Eighth Edition 26
Analyzing a Java Application that
Produces Console Output
• Even the simplest Java application involves a fair
amount of confusing syntax
• Print “First Java application” on the screen
Java Programming, Eighth Edition 27
Analyzing a Java Application that
Produces Console Output (cont’d.)
Java Programming, Eighth Edition 28
Understanding the Statement
that Produces the Output
• Literal string
– Will appear in output exactly as entered
– Written between double quotation marks
• Arguments
– Pieces of information passed to a method
• Method
– Requires information to perform its task
• System class
– Refers to the standard output device for a system
Java Programming, Eighth Edition 29
System class Arguments
Refers to the standard Pieces of information passed to a
output device for a method
system
Method
Requires information to
perform its task Literal string
Will appear in output exactly as
entered
Written between double quotation
marks
Java Programming, Eighth Edition 30
Java Programming, Eighth Edition 31
How to Build Java Program
Java Programming, Eighth Edition 32
Java Programming, Eighth Edition 33
Understanding the Statement
that Produces the Output (cont’d.)
Java Programming, Eighth Edition 34
Understanding the First Class
• Everything used within a Java program must be part
of a class
• Define a Java class using any name or identifier
• Requirements for identifiers
– Must begin with one of the following:
• Letter of the English alphabet
• Non-English letter (such as α or π)
• Underscore
• Dollar sign
– Cannot begin with a digit
Java Programming, Eighth Edition 35
Understanding the First Class
(cont’d.)
• Requirements for identifiers (cont’d.)
– Can only contain:
• Letters
• Digits
• Underscores
• Dollar signs
– Cannot be a Java reserved keyword
– Cannot be true, false, or null
• Access specifier
– Defines how a class can be accessed
Java Programming, Eighth Edition 36
Understanding the First Class
(cont’d.)
Java Programming, Eighth Edition 37
Understanding the First Class
(cont’d.)
Java Programming, Eighth Edition 38
Understanding the First Class
(cont’d.)
Java Programming, Eighth Edition 39
Understanding the First Class
(cont’d.)
Java Programming, Eighth Edition 40
Indent Style
• Use whitespace to organize code and improve
readability
• 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
• Allman style used in text
Java Programming, Eighth Edition 41
Understanding the main()
Method
• static
– A reserved keyword
– Means the method is accessible and usable even though
no objects of the class exist
• void
– Use in the main() method header
– Does not indicate the main() method is empty
– Indicates the main() method does not return a value
when called
– Does not mean that main() doesn’t produce output
Java Programming, Eighth Edition 42
Understanding the main()
Method (cont’d.)
Java Programming, Eighth Edition 43
Understanding the main()
Method (cont’d.)
Java Programming, Eighth Edition 44
Saving a Java Class
• Saving a Java class
– Save the class in a file with exactly the same name and
.java extension
• For public classes, class name and filename must match exactly
Java Programming, Eighth Edition 45
Compiling a Java Class and
Correcting Syntax Errors
• Compiling a Java class
– Compile the source code into bytecode
– Translate the bytecode into executable statements
• Using a Java interpreter
– Type javac First.java
• Compilation outcomes
– javac is an unrecognized command
– Program language error messages
– No messages indicating successful completion
Java Programming, Eighth Edition 46
Compiling a Java Class and
Correcting Syntax Errors (cont’d.)
• Reasons for error messages
– Misspelled the command javac
– A misspelled filename
– Not within the correct subfolder or subdirectory on the
command line
– Improper installation of Java
Java Programming, Eighth Edition 47
Correcting Syntax Errors
• The first line of the error message displays:
– The name of the file where the error was found
– The line number
– The nature of the error
• Next lines identify:
– The symbol
– The location
• Compile-time error
– The compiler detects a violation of language rules
– Refuses to translate the class to machine code
• Parsing
– Compiler divides source code into meaningful portions
Java Programming, Eighth Edition 48
Running a Java Application and
Correcting Logical Errors
• Run the application from the command line
– Type java First
• Shows the application’s output in the command
window
• The class is stored in a folder named Java on the C
drive
Java Programming, Eighth Edition 49
Running a Java Application and
Correcting Logical Errors (cont’d.)
Java Programming, Eighth Edition 50
Modifying a Compiled Java Class
• Modify the text file that contains the existing class
• Save the file with changes using the same filename
• Compile the class with the javac command
• Interpret the class bytecode and execute the class
using the java command
Java Programming, Eighth Edition 51
Modifying a Compiled Java Class
(cont’d.)
Java Programming, Eighth Edition 52
Correcting Logical Errors
• Logic error
– The syntax is correct but incorrect results were produced
when executed
• Run-time error
– Not detected until execution
– Often difficult to find and resolve
Java Programming, Eighth Edition 53
Adding Comments to a Java Class
• Program comments
– Nonexecuting statements added to a program for
documentation
– Use to leave notes for yourself or others
– Include the author, date, and class’s name or function
• Comment out a statement
– Turn it into a comment
– The compiler does not translate, and the JVM does not
execute its command
Java Programming, Eighth Edition 54
Adding Comments to a Java Class
(cont’d.)
• Types of Java comments
– Line comments
• Start with two forward slashes (//)
• Continue to the end of the current line
• Do not require an ending symbol
– Block comments
• Start with a forward slash and an asterisk (/*)
• End with an asterisk and a forward slash (*/)
Java Programming, Eighth Edition 55
Adding Comments to a Java Class
(cont’d.)
• Types of Java comments (cont’d.)
– Javadoc comments
• A special case of block comments
• Begin with a slash and two asterisks (/**)
• End with an asterisk and a forward slash (*/)
• Use to generate documentation
Java Programming, Eighth Edition 56
Adding Comments to a Java Class
(cont’d.)
Java Programming, Eighth Edition 57
Creating a Java Application
that Produces GUI Output
• JOptionPane
– Produces dialog boxes
• Dialog box
– A GUI object resembling a window
– Messages placed for display
• import statement
– Use to access a built-in Java class
• Package
– A group of classes
Java Programming, Eighth Edition 58
Creating a Java Application
that Produces GUI Output (cont’d.)
Java Programming, Eighth Edition 59
Creating a Java Application
that Produces GUI Output (cont’d.)
Java Programming, Eighth Edition 60
Finding Help
• Java API
– Also called the Java class library
– Provides prewritten information about Java classes
• FAQs on the Java Web site
• Java Development Kit (JDK)
– A software development kit (SDK) of programming tools
– Free to download
Java Programming, Eighth Edition 61
You Do It
• Your First Application
• Compiling a Java Class
• Adding Comments to a Class
• Creating a Dialog Box
• Exploring the Java Web Site
Java Programming, Eighth Edition 62
Don’t Do It
• Don’t forget the file’s name must match the class
name
• Don’t confuse these terms:
– Parentheses, braces, brackets, curly braces, square brackets,
and angle brackets
• Don’t forget to end a block comment
• Don’t forget that Java is case sensitive
• Don’t forget to end every statement with a semicolon
– Do not end class or method headers with a semicolon
• Don’t forgot to recompile when making changes
Java Programming, Eighth Edition 63