Java Printing Basics and Examples

Aug 12, 2024

Lecture on Printing in Java

Introduction

  • Topic: Printing in Java.
  • Example Program: Traditional "Hello World" program.

Key Concepts

Basic Printing Command

  • Command: System.out.println
    • Syntax: System.out.println("Hello, World!");
    • Explanation:
      • System with a capital 'S'
      • .out.println method
      • Text to print inside parentheses and quotes
      • End with a semicolon

Custom Messages

  • Replace "Hello, World!" with any string to print different messages.
    • Example: System.out.println("Woohoo!");

Writing the Program

Extending ConsoleProgram

  • Instead of extending Karel, extend ConsoleProgram in Java.
    • Class Declaration: public class HelloWorld extends ConsoleProgram

Structure of the Program

  1. Class Declaration: public class HelloWorld extends ConsoleProgram
  2. Run Method:
    • public void run()
    • Use curly brackets {} to define the method body.
  3. Printing Statements:
    • `System.out.println(