CSCI 1302 Chapter 1
Chapter 1: Introduction to Java Foundations
Introduction to the Chapter
Authors: John Lewis, Peter DePasquale, Joseph Chase
Focuses on the basics of Java programming and introduces fundamental concepts in program design and data structures.
Chapter Scope
Introduction to the following topics:
The Java programming language
Program compilation and execution
General problem solving techniques
The software development process
Overview of object-oriented principles
Basics of Java
Hardware and Software
Components of a Computer:
Hardware: physical components that enable functioning.
Software: includes programs (instructions) and the data they use.
What is Java?
A programming language defines the syntax and rules for creating programs.
Created by Sun Microsystems, Inc., introduced in 1995.
Quick rise in popularity due to ease of use and capabilities.
Structure of a Java Program
Classes and Methods
A Java program consists of:
Classes: a blueprint for creating objects.
Methods: blocks of code that execute specific tasks.
The main method (
main()) is essential for execution.
Example: Basic Java Application
// Lincoln.java
public class Lincoln {
public static void main(String[] args) {
System.out.println("A quote by Abraham Lincoln:");
System.out.println("Whatever you are, be a good one.");
}
}This demonstrates the structure of a simple Java application.
Java Program Structure
Components
Class Header and Body
Comments can be included to provide explanations within the code.
Comments in Java
Purpose:
Explain the purpose and processing of the program.
Three forms of comments are:
Single-line:
// commentMulti-line:
/* comment */Javadoc:
/** comment */
Identifiers
Definition: Words used to name variables, classes, methods, etc.
Rules:
Can include letters, digits, underscores (_), and dollar signs ($).
Cannot start with a digit and are case sensitive.
Naming conventions:
Title case for class names (e.g., Lincoln).
UPPER_CASE for constants (e.g., MAXIMUM).
Reserved Words
Java reserved words list (examples):
abstract,class,package,public,static,void,if,else,return,for,while
These words have special meanings and cannot be used for other identifiers.
White Space
Definition: Spaces, blank lines, and tabs in code.
Used for separating tokens in code; extra whitespace is ignored.
Important for readability, hence should be formatted consistently.
Program Formatting Examples
Poorly Formatted Code
// Lincoln2.java
public class Lincoln2{public static void main(String[]args){
System.out.println("A quote by Abraham Lincoln:");
System.out.println("Whatever you are, be a good one.");
}}Program Development Mechanics
Steps involved in developing a program:
Write the program code in a specific language (e.g., Java).
Translate the program for execution by the computer.
Investigate and rectify errors.
Language Levels
Four programming levels:
Machine language
Assembly language
High-level language
Fourth-generation language
Each level aims to simplify programming for humans.
Compilation Process
Translation Requirement: Programs must be converted to machine language to execute.
Compiler Definition: Software tool that translates source code into target language (often machine code).
Java Approach: Unique as Java source code translates into bytecode.
Basic Programming Steps
Writing, compiling, and executing a program involve specific tasks.
Compilation must be successful for an executable to be created.
Java Translation to Bytecode
Java compiler converts source code into bytecode, an intermediary form.
Bytecode is interpreted into machine language by an interpreter, allowing Java's architecture neutrality.
Development Environments
Definition: Tools for creating, testing, and modifying programs.
Integrated Development Environment (IDE): Combines tools into one.
Key tools include compilers, interpreters, and debuggers.
Popular Java IDEs: Sun Java Development Kit (JDK), Eclipse, NetBeans.
Syntax and Semantics
Syntax: Rules for forming valid program statements.
Semantics: Meaning behind the program statements; a syntactically correct program is not always semantically correct.
Types of Errors
Compile-time Errors: Detected by the compiler (syntax errors).
Run-time Errors: Occur during execution (e.g., division by zero).
Logical Errors: Produce incorrect results despite execution.
Problem Solving in Programming
Multi-step process includes:
Understanding the problem
Designing a solution
Implementing the solution
Testing the solution
These steps are iterative and interactive.
Object-Oriented Programming (OOP)
Java is an OOP language, where objects represent real-world entities.
Example: Employee object manages employee data.
Objects in OOP
An object consists of:
State: Descriptive characteristics (e.g., bank account number).
Behavior: Actions an object can perform (e.g., deposit, withdraw).
Classes in OOP
A class serves as a blueprint for creating objects.
Classes encapsulate state and behaviors through methods.
Single class can generate multiple objects.
Class-Object Relationship
A class can lead to numerous encapsulated objects.
Concepts of inheritance allow class hierarchies, extending functionalities.