AP Computer Science in Java
  • Introduction
  • Introduction to Programming in Java with Karel the Dog
    • Introduction to Programming with Karel
    • More Basic Karel
    • Java Programs and the Run Method
    • Karel Can't Turn Right
    • Methods in Karel
    • Top Down Design and Decomposition in Karel
    • Commenting Your Code
    • SuperKarel
    • For Loops
    • While Loops in Karel
    • If Statements
    • If/Else Statements
    • Control Structures Example
    • How To Indent Your Code
  • Basic Java
    • Printing in Java
    • Variables and Types
    • User Input
    • Arithmetic Expressions
    • Casting
    • Booleans
    • Logical Operators
    • Comparison Operators
    • For Loops
    • While Loops
    • If Statements
    • Loop-and-a-Half
    • Short-Circuit Evaluation
    • De Morgan's Laws
    • Strings
  • Methods
    • Java Methods
    • Methods and Parameters
    • Methods and Return Values
    • Javadoc and More Methods
    • Strings Methods
    • Strings and Characters
    • Exceptions
    • String Processing
  • Classes and Object-Oriented Programming
    • Introduction To Classes and Objects
    • Classes vs. Objects
    • Using a Class as a Client
    • Writing Classes
    • Writing Classes and Instance Methods
    • Getter and Setter Methods
    • Class Methods and Class Variables
    • Method Overloading
    • Local Variables and Scope
    • Key Terms for Classes
    • Objects vs Primitives
    • Inheritance
    • Class Design and Abstract Classes
    • Polymorphism
    • Interfaces
  • Data Structures
    • What Are Data Structures?
    • Introduction to Arrays
    • Using Arrays
    • ArrayList Methods
    • Arrays vs ArrayLists
    • 2D Arrays (Matrices or Grids)
    • Hashmaps
  • Algorithms and Recursion
    • What is an Algorithm?
    • Pseudocode
    • Linear Search
    • Binary Search
    • Selection Sort
    • Insertion Sort
    • Advanced: Recursion
    • Mergesort
Powered by GitBook
On this page
  • Environment Set Up
  • Writing Hello World

Was this helpful?

  1. Basic Java

Printing in Java

PreviousBasic JavaNextVariables and Types

Last updated 5 years ago

Was this helpful?

Now that you're comfortable using basic Java commands with Karel the Dog, it's time to move into the Java console environment.

Environment Set Up

Using the CodeHS editor makes writing Java programs simple and straightforward. The Java console environment is similar to the Karel environment you've used already, with a few key differences. Instead of showinga grid world where Karel will execute the program, the Java environment presents a console area that will print the output of your program. There is also a list of files for each program on the left side of the page. Many of the Java programs you will write will make use of more than one file -- all of the files will be listed in this section.

Writing Hello World

Writing a program that prints "Hello world" to the console requires a few lines of code:

public class HelloWorld extends ConsoleProgram
{
    public void run()
    {
        System.out.println("Hello world");
    }
}

There are a few key parts to pay attention to here:

  • This program extends ConsoleProgram instead of Karel. This tells the program that it's going to use the console instead of a Karel grid world.

  • The text is printed to the console with the System.out.println() command. This command looks a bit long, but you can think of it as telling the console to print whatever is inside the parentheses.

Now that you can print to the console, you'll be able to write programs that can display information to the user!

Environment