wiki:ControlSystems/SoftwareTeam/Training/GettingStarted/HelloWorld

You can think of a computer program as a recipe for the computer to follow. The program tells the computer exactly what to do. Programming languages have been developed to make it easy to write concise, precise instructions. Unlike human languages, computer languages are very small; they consist of a set of carefully chosen words that let you describe to the computer exactly what you want it to do. A popular programming language (C) has only 32 words! Java is a programming language that descended from C and is the language we will use to program the robot.

A Java program consists of one or more files (documents). We use a program from Microsoft called VSCode to write and run Java programs.

It's a long standing programming tradition to make your first program display the message "Hello world!":

Create your program

  1. Each program must have its own folder, so
    1. Create a folder named Java Projects in your Documents folder to hold all of your Java programs.
    2. Create another folder named Hello World inside the Java Projects folder to hold your first program.
  2. Launch VSCode and then:
    1. Select File->Open Folder and choose the JavaProjects\Hello World folder you created.
    2. Select File->New which will open the editor for you to enter a new program
    3. Cut and paste the following program into the editor:
         public class Hello {
            public static void main(String args[]) {
               System.out.println("Hello world");
            }
         }
      
    4. Select File->Save and name the file Hello.java
      NOTE: the file name is important; it must be Hello.java with a capital H (case matters to Java).
      The file name must be the same as the name of the class (more on classes later)

Run the program

There are multiple ways to run your program Java Hello World program

  • In the editor, just below the first line, you'll see an option to click "Run"
  • In the left navigation window, you can see the program file (Hello.java) with a red J next to it indicating that it is a Java file; right click on Hello.java and select Run.

When you run the program, a terminal window will open, the program will be compiled and then run;
In the terminal window below, you'll see the output:

Hello world!

Congratulations, you just wrote your first Java program!!!

Extra Credit

Change the text that's printed to say something different from Hello World. Whatever you want printed must be between the double quotation marks.

Last modified 19 months ago Last modified on Nov 4, 2019, 2:49:16 PM

Attachments (2)

Download all attachments as: .zip