Chapter 1 |
First program
Once the environment is set up, let's write our first source code in Java. Source code is a text file that contains instructions written in a high level language Java. The source code cannot be executed (made to run) by a processor without some additional steps (translation into bytecode).
Step 1. Watch the video:
Step 2. Launch IntelliJ and start a new project. The wizard will ask you for a few names:
* Project Name - the name of a new project. This will be the name of the folder where all your current project files will be kept. We recommend to create a new project for each session, e.g. Session_1.
* Class Name - the name of the class in the code stored in the src (source) folder within your IntelliJ project. NB! Class names must be unique, without spaces, and the first letter of each word must be capitalised. We recommend to create a new class for each task, e.g. HelloWorld.
* Java File Name - the name of the file with extension of .java. IntelliJ sets the file name to be exactly as the class name. The source file holds the code.
Step 3. Type the following source code that defines a class called HelloWorld:
// This application program prints Hello, World! public class HelloWorld { public static void main(String[] args) { System.out.println("Hello, World!"); } }
DO NOT COPY the code because your mind and hands need time to get used to a new programming language!
PS! Pay attention:
* Java is case-sensitive;
* the file name must be the same as the class name;
* punctuation: commas (,), dots (.), quotes ("), parentheses (), braces {} and brackets []
* each Java statement ends with a semicolon
;
(do not use semicolons after method and class declarations that are followed by curly braces);
* the highlighted "keywords" - IntelliJ does it to make it easier for you to read your code.
Step 4. Compile/build the project and run the program (use the top menu):
PS! Pay attention to:
* Java source file must have the extension .java;
* compiled source code is Java bytecode with extension .class.
Question:
Check the out folders of the project. Has a new file with extension of .class been added?
Chapter 1 |