1.2 Why programming? Why Java?
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.
Writing a program that prints “Hello world” to the console requires a few lines of code:
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:
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!
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:
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:
println
affects the subsequent line, not the one that comes before it!
-
Incorrect
Correct
No Answer was selected
Invalid Answer
What will this code segment output?
In this program you will draw some ASCII art using
System.out.println()
.
Write a program that outputs exactly this drawing.