🖥️

Understanding Java Print Methods

Oct 25, 2024

Lecture on Displaying Messages in Java

Overview

  • Topics Covered:
    • Strings in Java
    • println and print methods
    • System class

Strings in Java

  • A string is a group of characters or text.
  • Strings must be enclosed in double quotes.
    • Examples:
      • "hello"
      • "This is a string"
      • "1", "125" (as strings, not numbers)
  • An empty string: ""
  • Summary: Strings = characters between double quotes.

println Method

  • Purpose: Displays its parameter on the console.
  • Console Window: The output window where program results are shown.
  • Usage:
    • Syntax: System.out.println(parameter)
    • parameter typically a string.
    • Ends with a semicolon (statement).
  • Example Code Execution:
    • System.out.println("hello") prints "hello" (without quotes) and moves to a new line.
    • System.out.println("123") prints "123" on a new line.
    • Empty string System.out.println("") prints nothing but moves to a new line.
    • System.out.println("456") prints "456" on a new line.
  • Key Points:
    • ln in println stands for "new line."
    • Moves cursor to a new line after printing.

print Method

  • Similar to println but doesn’t move to a new line.
  • Usage:
    • Syntax: System.out.print(parameter)
  • Example Code Execution:
    • Prints everything on the same line without line breaks.
    • Example: "hello123456"
  • Key Points:
    • No line break after printing.

System Class and out Object

  • out Object:
    • An object of the PrintStream class.
    • Contains print and println methods.
    • Refers to the standard output device (screen).
  • System Class:
    • Named with Pascal Case (capital S) indicating it's a class.
    • out is a field of the System class (camel case, lowercase first letter).
  • Accessing Methods:
    • Use dot operator . to access fields and methods.
    • Syntax: System.out.print or System.out.println

Conclusion

  • Understanding how to print messages in Java using print and println methods.
  • System.out setup allows communication with the console output.

  • Note: Further exploration on displaying numbers instead of strings will be covered later.