in Informatica

Java programming lesson 2 Our first Java program

Java programming course lesson 2:
Our first Java program

The aim of this lesson is to show you a few lines of Java code in order to get familiar with this language. Don’t worry if some points are not clear, we will explain every single aspect in the next lessons.

import java.util.Scanner; // loads the Scanner class from the package(library) java.util

public class FirstProgram { // name of the Class

	public static void main(String[] args) {
		System.out.println("Hi Guys! \nDigit 2 numbers and I will sum them!");


		int n1, n2; // 2 variables containing integer numbers

		Scanner keyboard = new Scanner(System.in);  // it enables the ability to
													// read from the keyboard

		n1 = keyboard.nextInt(); // reads a number from the keyboard
		n2 = keyboard.nextInt();

		System.out.println("The sum of the numbers is: " + (n1+n2));
	}
}
FirstProgram.java

The first row:

import java.util.Scanner

informs the compiler that our Class is going to use the Scanner Class. For the moment we can think of a class like a code fragment to be used in a program. The Scanner class is defined in the package java.util which stands for “Java utilities”. A package is a library of classes that have already been defined.

Then it follows the class definition:

public class FirstProgram {
    ...
}

FirstProgram is the name of our class. Every Java class definition starts with an open curly bracket { and ends with the corresponding closed one }.
Enclosed by the brackets we can find the definition of the methods. Every Java Class has a method called main and it often contains the definition of other methods.

The definition of the method main starts from the second opened bracket till the correspondent closed parenthesis:

public static void main(String[] args){
    ...
}

The words public static are necessary but we will not talk about them now. Every instruction (statement) in a method defines a specific task; the set of instructions inside a method constitute the body of the method. The first instruction of our method starts with System.out.println, this instructions allow to print on the screen what is contained between the round brackets (i.e. (“Hi guys! …”). The \n is a special keyword that allows to start a new line. In order to execute actions, Java programs use Objects. The actions are defined by methods. System.out is an object used to send an output to the screen; println is the method that executes this action for the object System.out. In other words, println sends to the screen what is specified inside the round brackets. The element/s between brackets are called arguments and they feed the method with the necessary input to fulfill its action. In this case the argument is a string of characters. An object executes an action when one of its methods gets called. In a Java program the method call is obtained writing the object name followed by a dot followed by the name of the method and ended by a couple of round brackets. Inside the brackets there could be specified one or more arguments. The line:

int n1, n2;

says that n1 and n2 are the names of two variables.
A variable can store data. The word int means that the variable can contain an integer number. integer is an example of data type. A data type (or type) specifies the set of possible values and the actions defined for these values. The values of a particular data type are stored in memory in the same format.

The next line:

Scanner tastiera = new Scanner(System.in);

enables the program to accept, or read, the data that the user digits using the keyboard.
This line of code will be explained better in the future.

The next line:

n1 = tastiera.nextInt();

reads the number typed with the keyboard and stores it in the variable n1.

The last (interesting) line:

System.out.println("The sum of the numbers is: " + (n1+n2));

Corresponds to the execution of the following actions:

  1. The expression n1+n2 is evalued
  2. The initial string (up to the : character) is concatenated to the result of n1+n2 using the + sign
  3. The concatenation of the initial string with the integer result obtained from the evaluation we obtained a new string that acts as the argument for the println method
  4. the resulting string is printed on the screen

N.B. note that the expression (n1+n2) is evalued only because we put n1+n2 inside the round brackets; if we simply do String + n1 + n2 we obtain the concatenation of the string with the two variables (e.g. String=”Hello”, n1 = 2, n2 = 3, String + n1 + n2 = “Hello23”).

Now let’s talk about the semicolon, yeah…. it is mandatory to place a semicolon to the end of each instruction. Basically you are telling the compiler where the instruction ends.