Session 1 |
First program
Once the environment is set up, let's develop our first Java program called Hello World. The program has to type the sentence Hello, World! on the console.
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. OOP.
* 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 a capitalised letter. 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 exactly as the class name, and 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 follow with curly braces);
* the highlighted "keywords" - IntelliJ does it to make it easier for you to read your code.
Line 1 is a comment that documents what HelloWorld.java program is and how it is constructed.
Comments are not executable and are ignored by the compiler. But they provide useful explanation and documentation to you and others. It is recommended to add comments:
- at the top of any Java class to describe the overall function of the class
- at the top of any method to describe the overall function of the method
- to a particularly complicated part of the code
- to deactivate some lines of code while debugging (no need to delete the code :)
There are two kinds of comments:
- multi-line comment: begins with
/*
and ends with*/
, and may span more than one line - end-of-line (single-line) comment: begins with
//
and lasts until the end of the current line
Javadoc is a tool which comes with JDK. It generates a Java code documentation in HTML format from Java source code. The documentation is generated using the comments with the following appearance:
/** This is a documentation comment */ /** This is also a documentation comment */ // This is a single line comment /* This is a multiple line comment*/
Line 2 uses the keyword
class
to define a new class. Every Java application must have at least one class. Each class has a name. By convention, class names start with an uppercase letter. In this application, the class is called HelloWorld. The class definition begins with the opening curly brace {
and ends with the closing curly brace }
.
Line 3 is the header of
main( )
method. It is the entry point where the program begins execution. All Java applications begin execution by calling main( )
. The exact meaning of each word in the header cannot be given now (since it involves a detailed understanding of several Java’s features), but a brief explanation is as follows.
- Keyword
public
is an access modifier. An access modifier determines how other parts of the program can access a variable or a method (a class member). If a class member ispublic
, then it can be accessed even outside the class in which it is declared. (The opposite ofpublic
isprivate
which prevents a member from being used outside of its class.) In our program,main( )
must be declared aspublic
since it must be called from everywhere when the program is started. - The keyword
static
allowsmain( )
to be called before an object of the class has been created. This is necessary because @@main( )@ is called by the JVM before any objects are made. (If the explanation is a bit confusing, do not worry - we will learn about objects in four sessions :) - The keyword
void
shows the compiler this method does not return anything (later we will see that methods can return values the same way as functions return values in Python). - In Python, if we needed to pass any information to a method, we used variables specified between the parentheses followed the name of the method. These variables are called parameters. In Java, there is the same principle. If no parameters are required for a given method, we still need to include the empty parentheses. In
main( )
there is only one parameter,String args[ ]
which declares a parameter namedargs
. This is an array of objects of typeString
. (Arrays are collections of similar objects.) Objects of typeString
store sequences of characters. In our program,args
receives arguments from the command-line when the program is executed. - The last character on the line is the
{
. This indicated the start ofmain( )
’s body. The curly braces{......}
are always paired. A group of programming statements enclosed by a pair of braces is defined as a block. It is quite common that one block of the program is defined inside the other; consequently, there might be a bunch of opening braces in the code. It is important to double check and place the closing braces on the corresponding lines. - In our first program, the largest block is the class itself. The opening brace is on the first line and the closing brace is on the last line. The pair encloses the body of the class. The second pair of braces encloses the main method.
- Remember: all the code we want to run must be placed between these braces.
Line 4 contains the
System.out.println
statement, which is a part of main
method. This line outputs the string Hello, World! followed by a new line on the screen. Output is actually accomplished by the built-in println( )
method. In this case, println( )
displays the string that is passed to it. The other keywords of the line is a bit complicated to explain in detail at this time, but briefly, System
is a predefined class that provides access to the system, and out
is the output stream that is connected to the console. Thus, System.out
is an object that encapsulates console output.
- Notice that the
println( )
statement ends with a semicolon known as terminator. All statements in Java end with a semicolon (except headers of classes and methods).
Step 4. Compile/build the project and run the program (use the top menu):
PS! Pay attention to:
* a Java source file must have the extension of ".java";
* a compiled source code is a Java bytecode with extension of ".class".
Question:
Check the folder with your source code. Has a new file with extension of .class been added?
Session 1 |