Sample 1
Learning Objectives:
Essential Knowledge:
-
System.out.println
displays information on the computer monitor. -
Types of comments in Java include
/* */
, which generates a block of comments,//
, which generates a comment on one line.
Syntax
-
Computers are picky about how you tell them to do something. Truth be told, they are kind of dumb.
-
They can't "read between the lines" or "figure out" what you really meant.
-
Programming languages have a particular format you have to stick to when telling the computer what to do.
-
This format is called syntax.
-
-
When code is written in a way that the computer can understand, we say the code is syntactically correct.
-
When the code is written in a way the computer cannot understand, we say there is a syntax error.
-
Please also note that Java is case sensitive.
-
Writing, compiling and running your 1st Java program.
-
In Windows open a text editor, like Notepad and save it as HelloWorld.java in a new folder named "My First Java Program"(My Documents - AP CS A - My First Java Program)or (My Documents - AP CS A F - My First Java Program).
-
For mac:
-
Create your folder names with no spaces and create APCSA in your home directory(Launch "Finder" -> "Go" -> "Home" ; Select "File" -> "New Folder" -> "APCSA" or "APCSAF"; Then create another folder inside APCSA named "MyFirstJavaProgram").
-
Either use Mac OS X's default text editor "TextEdit", you need to open a new file -> choose "Format" -> "Make Plain Text" or use a programming text editor.
-
Now Save As: "HelloWorld.java" in the "MyFirstJavaProgram" folder you just created above.
-
-
- Write the Java program below.
-
Please note you will not see the "Pretty Print" colour coding when writing code with a text editor.
public
-
This keyword is an "access modifier" which specifies the accessibility or "scope" of what comes next.
-
Keywords = Words special to the language and have defined meanings for that language.
-
-
Public means it is accessible "everywhere", what this exactly means is shown by the table below(although it is unlikely that you will understand the terms in the column headings at this stage).
Access Modifier | within class | within package | outside class by subclass only | outside package |
---|---|---|---|---|
Private | Y | N | N | N |
Default | Y | Y | N | N |
Protected | Y | Y | Y | N |
Public | Y | Y | Y | Y |
public class HelloWorld
{
-
Every Java program must have at least 1 class definition that consists of keyword "class" followed by the class name(which can be anything you choose).
-
Note standard Java formatting dictates that class names start with a capital followed by lower case letters(for multiple words each word must start with a capital letter - no spaces are allowed so this helps make the name more readable).
-
The name you saved the text document with earlier has to be the same as the name of the public class(see below).
-
-
Java expects that at least 1 class is public and this public class contains the main() method(see later), Java will throw an error if it isn't.
-
The JVM would not be able to access it, if it is not public, as the JVM is "outside" of the class and package.
-
Multiple classes are possible but only one class can be public in the same file(if multiple public classes are required they must be saved in different files).
-
-
The
{
is a "curly bracket" and indicates the start of the class(see below for some explanation of classes).-
One standard Java formatting convention(see later for details)dictates that the
{
should start on a new line(indented if necessary to line up with the previous line - see later).
-
Object Orientated Programming Paradigm (OOP) enables modelling of the real world by basing itself on:

public class HelloWorld
{
public static void main(String args[])
{
-
Note the indentation on the 2nd line, you can use 5 spaces or the tab key. It is standard Java formatting to indent methods and other code groups(more about this in later presentations).
-
public:
-
Java expects the main method and its class to be "public", Javac will throw an error if it isn't.
-
-
A power button must publically available otherwise nobody could switch on a device.
-
-
-
static:
-
Allows the method to be run without creating an object. See previous slide and note that we will deal with this more later.
-
-
void:
-
States that the method does not return anything.
-
main:
-
Is the method(mini sub program)name - see above.
-
-
(String[] args):
-
Used for command line arguments that are passed as strings.
-
You can pass strings(text)to the main method when you run a Java program.
-
e.g. to send '1 2 3' to a Java program whose public class is Demo:
-
java Demo 1 2 3
-
See later topic 10(d) for details but note that this is not on your syllabus.
-
-
-
-
-
This appears to be part of the expected construction of the main method, and JVM will throw an error if it is not followed, even if it is not actually used.
-
Note that AP Computer Science does not require you to do so.
-
-
public class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World!");
-
This code statement prints what is inside the (), in this case the text inside the "..." into the console and inserts a newline after.
-
The console is the window where you will type commands to run Java programs and where all Java programs print to.
-
-
;
semicolons must be used to end almost all Java single code statements.-
Code statements = Lines that express some action to be carried out.
-
They are like sentences of a language.
-
-
public class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World!");
} // End of main
} // End of HelloWorld class
-
}
-
Indicates the end of the main method and class
-
It is standard formatting to place
}
on its own on the next line, indented to line up with its starting}
.
-
-
-
-
Begins a single line comment.
-
A comment is text you want Java to ignore and allow the programmer to describe code or keep notes.
-
By using comments in the Java code, you help yourself and other programmers understand the purpose of code that a comment refers to.
-
//
Allows comments to span a single line only and traditionally only used for short comments.
-
-
{ }
Formatting Issue-
I will leave it up to you to decide which one of the conventions below you use, but you must choose one and be consistent!
public class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World!");
} // End of main
} // End of HelloWorld class
-
Studies have shown that this style of indenting leads to fewer errors than other styles.
public class HelloWorld {
public static void main(String args[]) {
System.out.println("Hello World!");
} // End of main
} // End of HelloWorld class
-
Some programmers use this style to "save" lines.
-
I use this style to save vertical space on slides in presentations and because I am comfortable with it(many websites/books also use it).
-
How to compile and run a Java program
-
In Windows only navigate to the folder where you saved your Java Program(e.g. My Documents - AP CS A - My First Java Program), shift right click in the folder and choose "Open PowerShell window here" or "Open command window here".

-
Mac: Open Terminal.
-
Type and enter:
cd ~/APCSA/MyFirstJavaProgram
or
cd ~/APCSAF/MyFirstJavaProgram
-
i.e. The folder where you saved your Java Program.
-
-
Compile the program.
-
Type and enter:
javac HelloWorld.java
-
This compiles the .java file and produces a .class file.

-
Run the program.
-
Type and enter:
java HelloWorld
-
Runs the .class file shown above.
-
Common error: typing
java HelloWorld.java
instead ofjava HelloWorld
produces the error "Error: Could not find or load main class
".
-
What's your name?
-
Write a program which prints your name to the console.
-
Write comments for every line and explain each keyword/symbol in your own words.
-
Includes every
{ } ( ) " " ; // /* ... */
-
See below for details of how to write comments.
-
-
Please submit the(.java file).
Comments
-
For the program above "What's your name?" I expect comments about absolutely everything(see above).
-
Comment either before, after each line or all at the end but, whatever you choose, be consistent.
-
Comments for lines which are indented should also be indented.
-
The indentation of comments and the line which is being commented on must be the same.
- Split long comments which go beyond the longest code statement in your program, into multiple lines so that there is no need to scroll right to read, and your code looks "tidy".
- Below are some samples, please take a look(hover/click an image to magnify)and decide if they follow the guidelines above.
-
Note that the actual wording of the comments or the code itself is not what we are talking about here, it is the way it looks i.e. the elegance of the code that I am referring to!
-
Multi-Line Comments
-
Start with
/*
and end with*/
-
Allow long comments that can span multiple lines.
-
e.g.
-
/*
Hello,
Java!
*/
-