Java Printing

Basic Printing

The System.out.println() command prints text to the console. It's one of the first commands you'll learn in Java programming.

Simple Print Example:

System.out.println("Hello world!");
System.out.println("Welcome to Java programming!");

// Output:
// Hello world!
// Welcome to Java programming!

Program Structure

Every Java program needs boilerplate code to function. The program execution starts from the main method.

Complete Java Program:

public class MyProgram {
    public static void main(String[] args) {
        // Your code goes here
        System.out.println("Hello from Java!");
        System.out.println("This is my first program");
    }
}

Key Concepts:

  • Parameters: Text inside parentheses that gets printed
  • Semicolon: Every command must end with ;
  • Quotation marks: Text must be enclosed in double quotes ""
  • Case sensitivity: System must be capitalized exactly

Multiple Lines and Comments:

public class Example {
    public static void main(String[] args) {
        // Single-line comment
        System.out.println("First line");
        System.out.println("Second line");
        
        /* Multi-line comment
           for longer explanations */
        System.out.println("Third line");
    }
}

Printing Best Practices:

Good Practices:
  • Use meaningful text in your print statements
  • Add comments to explain your code
  • Use the sout shortcut in IDEs (type 'sout' + Tab)
  • One command per line for readability
Avoid:
  • Forgetting semicolons at the end of statements
  • Misspelling println as printin
  • Missing quotation marks around text
  • Writing everything on one line

Try It Yourself:

Exercise 1: Basic Printing

  • Write a program that prints your name
  • Print your favorite programming quote on multiple lines
  • Add comments explaining what each print statement does
  • Try the 'sout' shortcut if you're using an IDE
Practice with these printing exercises:

Applications in FRC

In FRC, printing to the console is a powerful tool for debugging and monitoring robot behavior. It allows developers to quickly access important data without relying on external tools like SmartDashboard. This can be especially useful during testing and characterization.

FRC Console Printing Example:

System.out.println("********** Drive FF Characterization Results **********");
System.out.println("\tkS: " + formatter.format(kS));
System.out.println("\tkV: " + formatter.format(kV));

Explanation

The example above demonstrates how to print formatted feedforward characterization results directly to the console. By using System.out.println(), we can easily display values like kS and kV in a readable format. This approach eliminates the need to configure SmartDashboard or other external tools, making it a quick and efficient way to debug and analyze data during development.

Open full interactive app