Java Programming Chapter 1: Creating Java Programs
Java Programming Chapter 1: Creating Java Programs
Objectives
Define basic programming terminology
Describe the features of the Java programming language
Analyze a Java application that produces console output
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 console output
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; it is considered a low-level programming language.
High-Level Programming Language: Allows the use of a vocabulary of reasonable terms that are more understandable than machine language.
Syntax: A specific set of rules that define the structure of statements in a programming language.
Program Statements: Similar to English sentences, these are commands to carry out tasks in a program.
Compiler or Interpreter: This tool translates language statements into machine code.
Syntax Error: A misuse of language rules; for example, a misspelled programming language word.
Debugging: The process of attempting to free a program of all errors.
Logic Errors: Also known as semantic errors; these occur when there is an incorrect order or procedure in the program. A program with logic errors may run but can provide inaccurate output.
Comparing Procedural and Object-Oriented Programming Concepts
Procedural Programming:
Involves sets of operations executed in a sequential manner.
Uses Variables: Named memory locations that hold values.
Utilizes Procedures: Individual operations grouped into logical units.
Object-Oriented Programs:
Create Classes, which are blueprints for objects.
Create Objects from classes.
Common application types for object-oriented programming include:
Development of Scientific Applications
Web, Mobile, Game, and Enterprise Applications
Graphical User Interfaces (GUIs)
Key object-oriented programming concepts include:
Polymorphism
Inheritance
Encapsulation
Understanding Classes, Objects, and Encapsulation
Class: Describes objects with common properties; serves as a definition.
Attributes: Characteristics that define an object and differentiate objects of the same class; the value of attributes represents an object's state.
Objects: Specific, concrete instances of a class.
Dog Class Definition Example
Attributes of Dog Class:
Name
Age
Breed
Shot Status
Example Objects:
Ginger: 6, Akita, Up to date
Bowser: 2, Retriever, Up to date
Roxy: 1, Beagle, Up to date
Method: A self-contained block of program code that carries out an action, analogous to a procedure.
Encapsulation: A concept that conceals internal values and methods from outside sources, providing security and safeguarding data from inadvertent changes.
Understanding Inheritance and Polymorphism
Inheritance: An important feature of object-oriented programs where classes inherit attributes and methods from existing classes but can have more specific features.
Polymorphism: This term means "many forms" and allows the same word or message to be interpreted correctly in various situations based on context.
Features of the Java Programming Language
Java: Developed by Sun Microsystems; it is an object-oriented and general-purpose programming language with notable advantages including:
Security features
Architecturally neutral execution
Java can run on a variety of computers and does not execute instructions directly on a computer. Instead, it runs on a hypothetical machine known as the Java Virtual Machine (JVM).
Development Environment and Compiling Process
Source Code: Programming statements written in a high-level programming language, saved in files with a .java extension.
Bytecode: Statements that the Java compiler generates and saves in a binary file with a .class extension.
Java Interpreter: Checks bytecode and executes instructions line by line within the JVM.
Types of Java Programs
Applets: Early Java programs embedded in web pages, now considered obsolete.
Java Applications: Stand-alone programs also known as console applications, which are the focus for this semester.
Analyzing a Java Application that Produces Console Output
Example Java code that produces console output:
public class First {
public static void main(String[] args) {
System.out.println("First Java application");
}
}
Literal String: A string that appears in output exactly as input, enclosed in double quotes.
Arguments: Information pieces passed to methods; necessary for methods to perform tasks.
System Class: Refers to the standard output device for a system.
Anatomy of a Java Statement
Each Java statement ends with a semicolon.
Breakdown of
System.out.println("First Java application");:System: A class.
out: A property of the System class.
"First Java application": A literal string argument for the println() method.
println(): A method call; method names are followed by parentheses.
Understanding the First Class
Everything in a Java program must be part of a class, defined using any name or identifier.
Identifier Requirements: Must start with a letter (English or non-English), underscore, or dollar sign; cannot begin with a digit.
Access Specifiers: Define how a class can be accessed. Key Java reserved keywords include
abstract,class,public,static, etc.
Example of Legal vs. Illegal Class Names
Legal but unconventional:
UndergradstudentBUDGET2012
Illegal:
class(a reserved keyword)Inventory Item(contains a space)2016Budget(begins with a digit)
Indent Style and Style Guidelines
Whitespace can be used to organize code and improve readability.
A corresponding closing curly brace must always match every opening brace. Placement is not important to the compiler.
Allman Style: The opening brace appears at the end of a statement, and the closing brace is aligned under the statement.
Coding guidelines suggest that:
Each statement generally appears on its own line.
Statements are usually separated by one space, with no extra spaces before a semicolon.
Understanding the main() Method
static: A keyword indicating that the method can be accessed without creating an object of the class.
void: Indicates that the method does not return a value when called.
Declaration of a typical main method:
public static void main(String[] args) {
// code
}
args: Identifier for an array of String arguments.
Shell Code: Basic structure for any Java class framework.
Saving a Java Class
A Java class must be saved in a file with the same name as the class, ending with .java.
Compiling a Java Class and Correcting Syntax Errors
To compile a Java class, use the command:
javac First.java
Compilation outcomes include:
Errors in the command, misspelled filenames, and confirmation or rejection of the compilation process.
Compile-Time Error: Detected by the compiler due to language rule violations.
Running a Java Application and Correcting Logical Errors
To run an application, use the command on the terminal:
java First
Logical errors occur when the syntax is correct but yield incorrect output results during execution.
Modifying a Compiled Java Class
Modify the existing Java class file's text, save, recompile, and execute using the previous commands.
Adding Comments to a Java Class
Comments are non-executing statements added to a program for documentation purposes.
Types of comments:
Line Comments: Start with
//and apply to the end of the line.Block Comments: Start with
/*and end with*/.Javadoc Comments: Begin with
/**and are used for documentation generation.
Commenting Example:
// This is a comment
System.out.println("Hello"); // This line is executed
/* Everything until the closing comment is ignored */
Terminology of a Java Application
Import Statement: Used to access built-in Java classes.
Package: A grouping of related classes.
Creating a Java Application that Produces GUI Output
JOptionPane: A class to create dialog boxes in GUI applications.
Example Usage:
import javax.swing.JOptionPane;
public class FirstDialog {
public static void main(String[] args) {
JOptionPane.showMessageDialog(null, "First Java dialog");
}
}
Finding Help
Java API: Also known as the Java Class Library, provides prewritten information about Java classes.
Java Development Kit (JDK): A software development kit (SDK) of programming tools; available for free.
Conclusion: Best Practices and Common Mistakes
Always ensure the filename matches the class name precisely.
Recognize different types of brackets and ensure proper closing.
Remember that Java is case-sensitive and that every statement must end with a semicolon.
Be diligent about recompiling after making changes to code to ensure updates take effect.