AP Computer Science A

- Your First Java Program


Learning Objectives:
Essential Knowledge:
Syntax
Writing, compiling and running your 1st Java program.
  1. 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
        (such as Sublime Text, Atom, jEdit or gedit)
        .
      • Now Save As: "HelloWorld.java" in the "MyFirstJavaProgram" folder you just created above.
  2. Write the Java program below.
public
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
{
Object Orientated Programming Paradigm (OOP) enables modelling of the real world by basing itself on:


public class HelloWorld
{

public static void main(String args[])

{
public class HelloWorld
{

public static void main(String args[])

{

System.out.println("Hello World!");
public class HelloWorld

{

public static void main(String args[])

{

System.out.println("Hello World!");

} // End of main

} // End of HelloWorld class
{ }
Formatting Issue
public class HelloWorld

{

public static void main(String args[])

{

System.out.println("Hello World!");

} // End of main

} // End of HelloWorld class
public class HelloWorld {

public static void main(String args[]) {

System.out.println("Hello World!");

} // End of main

} // End of HelloWorld class
How to compile and run a Java program
  1. 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".
  1. Compile the program.
javac HelloWorld.java
  1. Run the program.
java HelloWorld
What's your name?
Comments
1 / 7
Sample 1

2 / 7
Sample 2

3 / 7
Sample 3

4 / 7
Sample 4

5 / 7
Sample 5

6 / 7
Sample 6

7 / 7
Sample 7

Multi-Line Comments