string(175) "LC_CTYPE=C.UTF-8;LC_NUMERIC=en_US.utf8;LC_TIME=C;LC_COLLATE=C;LC_MONETARY=C;LC_MESSAGES=C;LC_PAPER=C;LC_NAME=C;LC_ADDRESS=C;LC_TELEPHONE=C;LC_MEASUREMENT=C;LC_IDENTIFICATION=C" string(6) "PART1D" Objektorienteeritud programmeerimine (Narva Kolledž) - Kursused - Arvutiteaduse instituut
Arvutiteaduse instituut
  1. Kursused
  2. 2017/18 kevad
  3. Objektorienteeritud programmeerimine (Narva Kolledž) (P2NC.01.083)
EN
Logi sisse

Objektorienteeritud programmeerimine (Narva Kolledž) 2017/18 kevad

  • Home
  • Materials
  • Grading
  • Source Example
  • Links
Session 1

Brief explanation of the program

Class

The first line says that this source program defines a class called HelloWorld.

 public class HelloWorld {
   ......
 }

The name of classes must follow the upper camel case notation.

Braces

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 second line and the closing brace is on the last line. The pair encloses the body of the class. All code you want to run must be placed between these braces. The second pair of braces encloses the main method.

The main method

Classes can contain different structures. In out first program, the class contains only one method (an analogy in Python would be a function). The method is called main. The main method means that this is the main method :) This is the place where the Java virtual machine (JVM) starts running the program.

The main method must be declared exactly as shown below:

 public static void main(String[] args) {
   ......
 }
  • public - anyone can access the method
  • static - it is a class method (not the method of an instance)
  • void - method does not return any value
  • main - the name of the main method (the entry point)
  • args - this array can be used for the input of arguments from the console (the name of the array can be anything)
  • String[] - the arguments entered via the console are considered as an array of strings

Again, the method has braces { } to enclose the body of the method; whereas, the method contains programming statements.

Statements and Semicolons

A statement in a programming language is a command for the computer to do something. A statement in Java is followed by a semicolon ;.

 System.out.println("Hello, world!");

This statement is used to print out the string "Hello, world!".

Comments

Comments are not executable and are ignored by the compiler. But they provide useful explanation and documentation to you and others. It is recommended that you 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 */

Indentation

Pay attention that indentation is not compulsory in Java, but it is a good tradition to use it to improve the readabily of the program (e.g. the indentation between the main method and the class shows that the main method is inside the class). IntelliJ follows these traditions :)

Self-assessment

Session 1
  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused