0% found this document useful (0 votes)
5 views

chapter 1

Chapter 1 introduces the fundamentals of Java programming, covering key concepts such as programming terminology, procedural vs. object-oriented programming, and the features of the Java language. It explains the process of compiling Java classes, correcting syntax and logic errors, and the importance of comments in code. The chapter also discusses different types of Java applications and provides insights into the structure and function of the main() method.

Uploaded by

2sdhhtp5sd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

chapter 1

Chapter 1 introduces the fundamentals of Java programming, covering key concepts such as programming terminology, procedural vs. object-oriented programming, and the features of the Java language. It explains the process of compiling Java classes, correcting syntax and logic errors, and the importance of comments in code. The chapter also discusses different types of Java applications and provides insights into the structure and function of the main() method.

Uploaded by

2sdhhtp5sd
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

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

Java Programming, Eighth Edition 3


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 4


Learning Programming
Terminology (cont’d.)
• High-level programming language
– Allows you to use a vocabulary of reasonable terms
• 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 5


Learning Programming
Terminology (cont’d.)
• Compiler or interpreter
– Translates language statements into machine code
• Syntax error
– Misuse of language rules
– A misspelled programming language word
• Debugging
– Freeing program of all errors
• Logic errors
– Also called semantic errors
– Incorrect order or procedure
– The program may run but provide inaccurate output

Java Programming, Eighth Edition 6


Comparing Procedural and Object-
Oriented Programming Concepts
• Procedural programming
– Sets of operations executed in sequence
– Variables
• Named computer memory locations that hold values
– Procedures
• Individual operations grouped into logical units
• Object-oriented programs
– Create classes
• Blueprints for an object
– Create objects from classes
– Create applications

Java Programming, Eighth Edition 7


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 8


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 9


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 10
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 11


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 12


Analyzing a Java Application that
Produces Console Output (cont’d.)

Java Programming, Eighth Edition 13


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 14


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 15


Understanding the First Class
(cont’d.)

Java Programming, Eighth Edition 16


Understanding the First Class
(cont’d.)

Java Programming, Eighth Edition 17


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 18


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 19


Understanding the main()
Method (cont’d.)

Java Programming, Eighth Edition 20


Understanding the main()
Method (cont’d.)

Java Programming, Eighth Edition 21


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 22


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 23


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 24


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 25


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 26


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 27


Adding Comments to a Java Class
(cont’d.)

Java Programming, Eighth Edition 28

You might also like