Please enable JavaScript to use CodeHS

Chapter 1

Primitive Types

1.2 Why programming? Why Java?

Environment Set Up

Using the CodeHS editor makes writing Java programs simple and straightforward. 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.

Editor Setup

Writing Hello World

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

public class HelloWorld
{
  public static void main(String[] args)
  {
    System.out.println("Hello world");
  }
}
Plain text

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

The code that you want to execute is written inside a function called main. Java always looks to the main function as a place to start the program execution. The main function always has this header:

public static void main(String[] args)
Plain text

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.

Inside the () for the println statement, you have a string literal enclosed inside the double quotes. In this case, our string literal is Hello world.

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

Hello World

Println vs Print

When using the System class, there are two different ways that you can print output: println and print. The big difference between these two is that println will go to a new line after the print statement, while the print statement will stay on the same line. In other words, if you want to print your statement then go to a new line, you should use the println command. If you want to print your statement and stay on the same line, you should use the print command:

System.out.println("Hello world!"); 
System.out.println("Hello world!");
System.out.print("Hello world!");
System.out.print("Hello world!");
Plain text

When println is called twice, the output is displayed on two separate lines. The print statement, however, displays the output on the same line.

If you were to use println followed by print, the print statement will appear on the next line, but if the print statement comes before the println, the statement will stay on the same line:

System.out.println("Hello world!");
System.out.print("Hello world!");

System.out.print("Hello world!");
System.out.println("Hello world!");
Plain text

println affects the subsequent line, not the one that comes before it!

Printing Multiple Lines

Check Your Understanding

  1. Incorrect Correct No Answer was selected Invalid Answer

    What will this code segment output?

    System.out.print("Hello");
    System.out.print("World");
    Plain text

Exercise: ASCII Art

In this program you will draw some ASCII art using
System.out.println().

Write a program that outputs exactly this drawing.

    ()____
   /     O)___
  /           )
 /      _____/
/      /
Plain text