Much of this is taken from the CodeHS video and slideshow for lesson 1.2. Feel free to go there to receive this in a different format.
1
2
3
4
5
6
7
public class MyProgram
{
public static void main(String[] args)
{
//code that goes here will run when the program starts
}
}
The skeleton above is the minimum requirement to get a running program in the programming language java. You don’t need to know what most of this means right now - we will learn many of these words later. For now, just make sure not to touch any of it!
When you make a new program in any good IDE, the skeleton will always be there for you. Just make sure you don’t change any of the written lines, and you’ll be fine. If any part of this skeleton is removed, or renamed, it may cause an error.
One of the simplest things a program can do is print information out for the user to read. There are of course lots of places a computer program might print information - a popup window on the screen, into a file, on an actual printer - but the simple java programs we will be writing print to to a special place called the console, a special location where the user can interact directly with a program using only text.
You will read the output of your programs and, later, give input to your programs in that console.
Of course, most programs you run on your computer do not use the console for user input and output, and we will learn later how to use more common modern tools, but for now this gives us a simple window into the functioning of our computer programs.
System.out.println("text");
The most common command used to print in java is System.out.println("Words to Print");
. This will print the words Words to Print
on the console, then add a line break (like pressing Enter) after the words, so that the NEXT print command will appear on the next line.
What errors can you find in the lines of code below? (the beautiful syntax highlighting has been disabled to keep you from getting hints…)
System.out.PrintLn("Hello!");
System.out.println('My');
System.out.println("Name")
system.out.println("is");
System.out.println(David);
1
2
3
4
5
System.out.PrintLn("Hello!"); //Should not capitalize P or L in println
System.out.println('My'); // single quotes don't work! need double quotes
System.out.println("Name") // semi-colon is missing
system.out.println("is"); // did not capitalize System
System.out.println(David); // forgot the quotes
System.out.print("text")
This works just like the previous command, except it does NOT add a line break after the text. So if you then do another System.out.print("text");
or System.out.println("text");
after this one, the two will run together.
What will be the output of the code below?
1
2
3
4
5
6
System.out.println("Hello");
System.out.print("My");
System.out.print("Name");
System.out.print("Is");
System.out.println("David");
System.out.println("What is yours?")
1
2
3
Hello
MyNameIsDavid
What is yours?
Create flash cards or a quizlet deck with the following words, with examples, a description, and a code example for java on the back.