Java Programming, Second Edition Chapter One Creating Your First Java Program.
-
Upload
andrea-bridges -
Category
Documents
-
view
226 -
download
0
Transcript of Java Programming, Second Edition Chapter One Creating Your First Java Program.
Java Programming, Java Programming, Second Second EditionEdition
Chapter OneChapter One
Creating Your First Java ProgramCreating Your First Java Program
In this chapter, you will:In this chapter, you will:
Learn about programmingLearn about programming Understand object-oriented programming Understand object-oriented programming
conceptsconcepts Learn about the Java programming languageLearn about the Java programming language Start a Java programStart a Java program Add comments to a Java programAdd comments to a Java program Run a Java programRun a Java program Modify a Java programModify a Java program
Learning about programmingLearning about programming
Program - A set of instructions that you write Program - A set of instructions that you write to tell a computer what to doto tell a computer what to do
Machine language - The language a machine Machine language - The language a machine works inworks in
High-level programming languages - High-level programming languages - Languages that allow programmers to use Languages that allow programmers to use terms such as “read,” “write,” and “add”terms such as “read,” “write,” and “add”
Syntax-The rules of the languageSyntax-The rules of the language Logic – correct statements and order to Logic – correct statements and order to
produce outcome desiredproduce outcome desired
Converting high level-language Converting high level-language codecode
Compiler - All instructions converted at Compiler - All instructions converted at once from high-level language to machine once from high-level language to machine language; new program is then runlanguage; new program is then run
C/C++, COBOL, FORTRAN, etc. compileC/C++, COBOL, FORTRAN, etc. compile Interpreter - High-level language code is Interpreter - High-level language code is
read and converted at run time line by lineread and converted at run time line by line Basic, Perl, PHP, Python are interpretedBasic, Perl, PHP, Python are interpreted Java is compiled, then interpretedJava is compiled, then interpreted
Procedural ProgrammingProcedural Programming
Procedural programming – step-by-step Procedural programming – step-by-step approach to problem-solvingapproach to problem-solving
Variables – hold valuesVariables – hold values Operations – read, write, mathOperations – read, write, math Procedures are groups of operationsProcedures are groups of operations Calls – when a program jumps to a Calls – when a program jumps to a
procedureprocedure
OOP ConceptsOOP Concepts OOP is a different approach to problem-OOP is a different approach to problem-
solving; conceptualizing objects instead of solving; conceptualizing objects instead of stepssteps
Create objects, then applications that use Create objects, then applications that use these objectsthese objects
Similar to concrete objects in the real worldSimilar to concrete objects in the real world Objects have states and methodsObjects have states and methods States are also called attributesStates are also called attributes Methods are functions – blocks of codeMethods are functions – blocks of code
OOP Concepts cont.OOP Concepts cont.
Class – a group of objects with common Class – a group of objects with common propertiesproperties
Instance – single existing object of a classInstance – single existing object of a class Inheritance – objects inherit attributes from Inheritance – objects inherit attributes from
the parent classthe parent class Encapsulation – “data hiding”; programmer Encapsulation – “data hiding”; programmer
only needs to know the only needs to know the interfaceinterface for a for a class, not its internal workingsclass, not its internal workings
Java Programming LanguageJava Programming Language
Developed by Sun Microsystems as an Developed by Sun Microsystems as an object-oriented language that is both used object-oriented language that is both used for general-purpose business programs for general-purpose business programs and for interactive World Wide Web-based and for interactive World Wide Web-based Internet programs Internet programs
Architecturally NeutralArchitecturally Neutral
The Java programming language can be used to The Java programming language can be used to write a program that will run on any platform or write a program that will run on any platform or operating system using the JVM (Java Virtual operating system using the JVM (Java Virtual Machine).Machine).
Java runs on a hypothetical computer known as Java runs on a hypothetical computer known as the Java virtual machine (JVM)the Java virtual machine (JVM)
When your source code program is compiled When your source code program is compiled into byte code, an interpreter within the JVM into byte code, an interpreter within the JVM interprets the byte code and interfaces with the interprets the byte code and interfaces with the operating system to produce the program resultsoperating system to produce the program results
Java Program TypesJava Program Types
Java applets – programs embedded in a Java applets – programs embedded in a Web pageWeb page
Java applications – stand-alone programsJava applications – stand-alone programs Applications can be Applications can be
Console-based: DOS-style input/output from Console-based: DOS-style input/output from keyboard to screen, or keyboard to screen, or
Windowed – GUI interfaceWindowed – GUI interface
Starting a Java ProgramStarting a Java Program
Literal string- A series of characters that will Literal string- A series of characters that will appear exactly as enteredappear exactly as entered
Arguments- Information that a method requires to Arguments- Information that a method requires to perform its taskperform its task
System.out.println(“First Java Program”);System.out.println(“First Java Program”);
Java class name requirementsJava class name requirements
Must begin with a letter of the alphabet, Must begin with a letter of the alphabet, [or an underscore, or a dollar sign][or an underscore, or a dollar sign]
Can only contain letters, digits, Can only contain letters, digits, underscores, or dollar signsunderscores, or dollar signs
Cannot be a Java reserved keywordCannot be a Java reserved keyword Cannot be one of the following values: Cannot be one of the following values:
true, false, or nulltrue, false, or null
Printing a StringPrinting a String
Access Modifier- Defines the Access Modifier- Defines the circumstances under which a class can be circumstances under which a class can be accessedaccessed
static- Ensures that the method is static- Ensures that the method is accessible even though no objects of the accessible even though no objects of the class existclass exist
void- The method does not return any void- The method does not return any value when it is calledvalue when it is called
Program CommentsProgram Comments
Program Comments- Nonexecuting Program Comments- Nonexecuting statements that you add to a program for statements that you add to a program for the purpose of documentationthe purpose of documentation
Line comments- //Line comments- // Block comments - /* */Block comments - /* */ javadoc comments - /** */javadoc comments - /** */
Running a Java ProgramRunning a Java Program
1.1. You must compile the program you wrote You must compile the program you wrote (called the source code) into bytecode.(called the source code) into bytecode.
2.2. You must use the Java interpreter to translate You must use the Java interpreter to translate the bytecode into executable statements. the bytecode into executable statements.
Running a Java ProgramRunning a Java Program
The commands for compiling and executing are:The commands for compiling and executing are: Compiling a program (from the DOS prompt)Compiling a program (from the DOS prompt)
javac Hello2.javajavac Hello2.java
Executing a programExecuting a program java Hello2java Hello2
Modifying a ProgramModifying a Program
To produce the new output:To produce the new output: Modify the text file that contains the existing Modify the text file that contains the existing
programprogram Change the class nameChange the class name Change the literal string that currently prints Change the literal string that currently prints
and then add an additional text stringand then add an additional text string
ErrorsErrors
Run-time or Logic Errors- Occur when the Run-time or Logic Errors- Occur when the syntax of the program is correct and the syntax of the program is correct and the program is compiled, but produces program is compiled, but produces incorrect results incorrect results
Syntax Errors- Programming errors that Syntax Errors- Programming errors that occur when you introduce typing errors occur when you introduce typing errors into your programinto your program