In-depth Notes on Computers, Programs, and Java

Objectives

  • Understand computer basics, programs, and operating systems.
  • Describe the relationship between Java and the World Wide Web.
  • Understand Java language specification, API, JDK, and IDE.
  • Write a simple Java program and display output on the console.
  • Explain the basic syntax of a Java program.
  • Create, compile, and run Java programs.
  • Use sound Java programming style and document programs properly.
  • Explain differences between syntax errors, runtime errors, and logic errors.
  • Develop Java programs using NetBeans and Eclipse.

What is a Computer?

  • A computer consists of various components:
    • CPU (Central Processing Unit)
    • Memory
    • Hard Disk
    • Floppy Disk
    • Monitor
    • Printer
    • Communication Devices

Central Processing Unit (CPU)

  • The CPU is the brain of the computer:
    • It retrieves instructions from memory and executes them.
    • Speed is measured in megahertz (MHz), with 1 MHz = 1 million pulses per second.
    • Example: An Intel Gold Processor can have 5 cores at 4.4 GHz (where 1 GHz = 1000 MHz).

Memory

  • Memory is used to store data and program instructions for the CPU to execute:
    • Information is stored in bytes, each holding eight bits.
    • Memory is volatile; information is lost when power is off.

Data Storage

  • Data is encoded as bits (0s and 1s):
    • Example: Character 'J' is represented by 01001010.
    • A byte is the smallest storage unit, and adjacent bytes are used for larger data.

Types of Storage Devices

  1. Disk Drives: Internal and external hard disks, USB drives.
  2. CD Drives: CD-R and CD-RW.
  3. Tape Drives

Input Devices

  • Hardware that allows the user to provide data and instructions to a computer system.

Output Devices

  • Hardware that converts data into human-perceptible form:
    • Examples include monitors, printers, speakers, and projectors.

Monitor Resolution

  • The quality of the display is determined by:
    • Resolution: Number of pixels, e.g., 1,024 x 768 pixels for a 17-inch screen.
    • Dot Pitch: Measurement between pixels; a smaller dot pitch provides sharper images.

Communication Devices

  • Devices enable communication between computers:
    • Modems for internet connection (56,000 bps for standard dial-up).
    • DSL and cable modems offer higher speeds.
    • Network Interface Card (NIC): connects PC to Local Area Network (LAN).

Software and Programming Languages

  • Programs are instructions for computers and are written in programming languages.
  • Types of programming languages:
    • Machine Language: Binary instructions for computers.
    • Assembly Language: Human-readable version of machine language using mnemonics.
    • High-Level Languages: Easier to read and write, e.g., Java, Python.

Compiling Source Code

  • High-level language programs (source code) must be translated into machine code for execution:
    • An interpreter translates code one line at a time for immediate execution.
    • A compiler translates the whole program at once to create an executable file.

Operating Systems

  • The OS manages and controls a computer's activities.
  • Popular OS include Microsoft Windows, Mac OS, and Linux.

Why Java?

  • Java enables development of applications for:
    • Standalone applications
    • Browser applications
    • Hand-held devices
    • Web servers

Characteristics of Java

  1. Simplicity: Easier syntax than C++.
  2. Object-Oriented: Supports OOP principles like encapsulation and inheritance.
  3. Distributed: Facilitates easy network programming.
  4. Interpreted: Java uses JVM for execution, making it platform-independent.
  5. Robust: Strong error checking during compile and runtime.
  6. Secure: Security features to protect systems from harmful programs.
  7. Architecture-Neutral: Write once, run anywhere capability due to bytecode.
  8. Portable: Programs can run on any system with Java Virtual Machine.
  9. Multithreaded: Supports concurrent execution for better resource usage.
  10. Dynamic: Can adapt to evolving applications without recompilation.

Java Development Kit (JDK) Editions

  • J2SE: For client-side applications.
  • J2EE: For server-side applications.
  • J2ME: For mobile applications.

Example of a Simple Java Program

// This program prints Welcome to Java!
public class Welcome {
    public static void main(String[] args) {
        System.out.println("Welcome to Java!");
    }
}
  • Creating, Compiling, and Running: Use IDEs like NetBeans or Eclipse, or text editors.

Anatomy of a Java Program

  • Class Name: Must start with an uppercase letter.
  • Main Method: Entry point of the program; must be defined as public static void main(String[] args).
  • Statements: Each action or sequence ends with a semicolon.
  • Reserved Words: Special meanings in the program.
  • Blocks: Defined by curly braces {}.

Programming Style & Documentation

  • Include descriptive comments, meaningful names, proper indentation, and consistent styles for readability.

Types of Programming Errors

  1. Syntax Errors: Detected by the compiler;
  2. Runtime Errors: Cause the program to abort during execution;
  3. Logic Errors: Produce incorrect results but do not terminate the program.