Rules of language. • Program statements. – Similar to English sentences. – Carry
out tasks of program. Java Programming, Fifth Edition. 2. Learning About ...
Learning About Programming • Program – Set of written instructions that tells computer what to do
Java Programming Fifth Edition
• Machine language – Most basic circuitry-level language – Low-level programming language
• High-level programming language – Allows you to use vocabulary of reasonable terms
• Syntax – Rules of language
Chapter 1 Creating Your First Java Classes
• Program statements – Similar to English sentences – Carry out tasks of program
Java Programming, Fifth Edition
Learning About Programming (continued)
Introducing Object-Oriented Programming Concepts
• Compiler or interpreter
• Procedural programming
– Translates language statements into machine code
– Sets of operations executed in sequence – Variables
• Syntax error
• Named computer memory locations that hold values
– Misuse of language – Misspelled programming language word
– Procedures • Individual operations grouped into logical units
• Debugging
• Object-oriented programs
– Freeing program of all errors
– Create classes – Create objects from classes – Create applications
• Logic errors – Also called semantic errors – Incorrect order or procedure Java Programming, Fifth Edition
2
3
Java Programming, Fifth Edition
4
Introducing Object-Oriented Programming Concepts (continued)
Understanding Objects and Classes • Objects
• GUI system
– Made up of attributes and methods
– Allows you to treat files as objects – Not all object-orientated programs written to use GUI
• Object-oriented programming differs from traditional procedural programming – Basic concepts
– Characteristics that define object – Differentiate objects of same class – Value of attributes is object’s state
• Class
• Encapsulation • Inheritance • Polymorphism
Java Programming, Fifth Edition
• Attributes
– Describes objects with common properties – Definition – Instance 5
Understanding Objects and Classes
Java Programming, Fifth Edition
6
Understanding Objects and Classes (continued) • Method – Self-contained block of program code – Similar to procedure
• Encapsulation – Refers to hiding of data and methods within object – Provides security – Keeps data and methods safe from inadvertent changes
7
Java Programming, Fifth Edition
8
Learning About Java
Understanding Inheritance and Polymorphism
• Java – – – –
• Inheritance – Important feature of object-oriented programs – Classes share attributes and methods of existing classes but have more specific features – Helps you understand real-world objects
Developed by Sun Microsystems Object-oriented language General-purpose Advantages • Security features • Architecturally neutral
• Java (continued) – Can be run on wide variety of computers – Does not execute instructions on computer directly – Runs on hypothetical computer known as Java virtual machine (JVM)
• Polymorphism – Means “many forms” – Allows same word to be interpreted correctly in different situations based on context
• Source code – Programming statements written in high-level programming language
Java Programming, Fifth Edition
9
10
Learning About Java (continued)
Learning About Java (continued) • Bytecode – Statements saved in file – Java compiler converts source code into binary program
• Java interpreter – Checks bytecode and communicates with operating system – Executes bytecode instructions line by line within Java virtual machine
Java Programming, Fifth Edition
11
12
Analyzing a Java Application That Uses Console Output
Java Program Types • Applets
• Even simplest Java application
– Programs embedded in Web page
– Involves fair amount of confusing syntax
• Java applications
• Print “First Java application” on screen
– Called Java stand-alone programs – Console applications • Support character output
– Windowed applications • Menus • Toolbars • Dialog boxes
13
14
Understanding the Statement That Prints the Output (continued)
Understanding the Statement That Prints the Output • Literal string – Will appear in output exactly as entered – Written between double quotation marks
• Arguments – Pieces of information passed to method
• Method – Requires information to perform its task
Java Programming, Fifth Edition
15
16
Understanding the First Class (continued)
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
• Requirements for identifiers – Can only contain: • • • •
– Must begin with: • Letter of English alphabet • Or non-English letter (such as
or )
Letters Digits Underscores Dollar signs
– Cannot be Java reserved keyword – Cannot be true, false, or null
– Cannot begin with digit
• Access modifier – Defines how class can be accessed Java Programming, Fifth Edition
17
Java Programming, Fifth Edition
18
Understanding the First Class
Understanding the First Class
19
20
Understanding the First Class
Understanding the main() Method • static – Reserved keyword – Means method accessible and usable • Even though no objects of class exist
• void – Use in main() method header – Does not indicate main() method empty – Indicates main() method does not return value when called – Doesn’t mean main() doesn’t produce output 21
Java Programming, Fifth Edition
22
Adding Comments to a Java Class
Shell Code
• Program comments – Nonexecuting statements added to program for documentation – Use to leave notes for yourself or others – Include author, date, class’s name or function
• Comment out a statement – Turn it into a comment – Compiler does not translate and the JVM does not execute its command
Java Programming, Fifth Edition
23
Java Programming, Fifth Edition
24
Adding Comments to a Java Class (continued)
Adding Comments to a Java Class (continued)
• Types of Java comments
• Types of Java comments (continued)
– Line comments
– Javadoc comments
• Start with two forward slashes (//)
• Special case of block comments • Begin with slash and two asterisks (/**) ( ) • End with asterisk and forward slash (*/)
• Continue to end of current line • Do not require ending symbol
– Block comments
• Use to generate documentation
• Start with forward slash and asterisk (/*) • End with asterisk and forward slash (*/)
Java Programming, Fifth Edition
25
Java Programming, Fifth Edition
26
Saving, Compiling, and Running and Modifying a Java Application
Adding Comments to a Java Class (continued)
• Saving a Java class – Save class in file with exactly same name and.java extension • For public classes • Class name and filename must match exactly
• Compiling a Java class – Compile source code into bytecode – Translate bytecode into executable statements • Using Java interpreter
– Type javac First.java Java Programming, Fifth Edition
27
Java Programming, Fifth Edition
28
Saving, Compiling, and Running and Modifying a Java Application (continued) • Compilation outcomes
Running a Java Application • Run application from command line
– javac unrecognized command
– Type java First
– Program language error messages – No messages indicating successful completion
• Shows application’s output in command window • Class stored in folder named Java on C drive
• Reasons for error messages – Misspelled command javac – Misspelled filename – Not within correct subfolder or subdirectory on command line – Java not installed properly Java Programming, Fifth Edition
29
Running a Java Application (continued)
Java Programming, Fifth Edition
30
Modifying a Java Class • Modify text file that contains existing class • Save file with changes – Using same filename
• Compile class with javac command • Interpret class bytecode and execute class using java command
Java Programming, Fifth Edition
31
Java Programming, Fifth Edition
32
Creating a Java Application Using GUI Output
Creating a Java Application Using GUI Output (continued)
• JOptionPane – Produce dialog boxes
• Dialog box – GUI object resembling window – Messages placed for display
• Package
– Group of classes
• import statement – Use to access built-in Java class 33
34
Correcting Errors and Finding Help (continued)
Correcting Errors and Finding Help • First line of error message displays:
• Parsing
– Name of file where error found – Line number – Nature of error
– Process compiler uses to divide source code into meaningful portions
• Logic error
• Next lines identify:
– Syntax correct but produces incorrect results when executed – Usually more difficult to find and resolve
– Symbol – Location
• Compile-time error
• Java API
– Compiler detects violation of language rules – Refuses to translate class to machine code Java Programming, Fifth Edition
Java Programming, Fifth Edition
– Also called the Java class library – Prewritten Java classes 35
Java Programming, Fifth Edition
36