Starting with Java

As we are starting with Java, the first thing we will have to do is download the official version of the JDK (Java Development Kit). For this, we are going to go to the Oracle website, which is the owner of Java, and we download the JDK.

Oracle recommends NetBeans as IDE (Integrated development environment) for programming with Java. So in this course, we will use said IDE that you can download together with Java from this URL.

However, for the first lessons of the course, we will not perform any installation, we will use an online Java compile, with which we can practice from our browser.

Compiling and running Java Online.

There are several Java compilers online. Just type in Google: “java online compiler” and we will list several actions.

In this course, we are going to use Jdoodle, which we can access from here.

Activity 3.1: Java compiler online

  • Open your browser and go to the online Java compiler link indicated above. By default, there is a code that we will modify for our practices.
  • Press the “Execute” button and see the result.
  • Try to understand this basic piece of code. Can you understand it?

Basic syntaxis

As we are starting with Java, the first thing we have to learn is the basic syntax of this programming language. We will start by modifying the default code that the online compiler shows us:

public class MyClass {
    public static void main(String args[]) {
      int x=10;
      int y=25;
      int z=x+y;

      System.out.println("Sum of x+y = " + z);
    }
}

Some considerations before analyzing the code:

  • Every Java program needs at least one Class. I’ll explain in detail what a Class is when we get to that topic, for now, let’s just notice that the code begins with the declaration of a Class: public class.
  • This is followed by a series of instructions contained in square brackets.
  • A class has attributes and methods. An attribute can be understood as data and a method as a function. Attributes and methods we will see in detail in the chapter dedicated to Classes.
  • Java, like many other languages, uses reserved words (keywords). These have a special meaning and cannot be used for any other purpose.

Now let’s analyze the code:

  1. Any Java program begins with the statement:
class ClassName{
}
  1. It continues with a method called main which is the entry point to the Java program. When we execute a Java program, we go to this method to execute the instructions that are in it.
  1. The syntax of the main method is always the same:
 public static void main(String []args){

}
  1. class is the keyword that indicates that the block of instructions that follows is a Class. Java is case sensitive, that is, it is case sensitive. Try typing Class instead of class and it will throw an error.
  2. public is another keyword that indicates that the element that follows must have public access, that is, it will be accessible from other Classes.
  3. main is the name of the initial method that is called when the program runs. There can only be one main method.
  4. Braces ({}) are used to indicate the start and end of a block. The main method contains four lines of code and the MyClass Class contains the main method.
  5. The instructions:
int x=10; int y=25;

Declare two variables and assign values to those variables

  1. The instruction:
int z=x+y;

Perform the sum of the values of the two previous variables and store the result in another variable.

  1. The instruction:
System.out.println("Sum of x+y = " + z);

It paints the text that appears between quotes on the screen and shows the result of the previous operation.

  1. In java, any statement must end with “;”

Activity 3.2: Getting started with the Java online compiler

  1. Open the online compiler:
  2. Change the name of the MyClass Class to something else and run, for example: FirstClass. The result will be the same as before.
  3. Remove the public keyword from before class. The result will give us the following message:
No «public class» found to execute
  1. Change the text of the argument of the println method and run the program. The new text should appear on the screen.
  2. Now, change the static keyword to Static and run the program. We will get an error because Java is case sensitive, that is, it is case sensitive.
/FirstClass.java:2: error: <identifier> expected

public Static void main(String args[]) {

^

1 error
  1. Remove the semicolon at the end of the println statement in the main method. You will get the error:
/FirstClass.java:7: error: ‘;’ expected

System.out.println(«Sum of x+y = » + z)


^

1 error

Java has a very strict syntax. Any statement must end with a “;”.

Commenting on the code

Commenting on the code is good practice. It is very useful if we have to review the Code after some time. Comments are simply ignored by the compiler.

There are two ways to enter comments in your code:

  • Using “//” at the beginning of the line we want to be a comment.
  • All the characters that we place between “/ ” and “ /” will be considered comments. Then the first is a comment start mark and the second is a comment end mark.

Activity 3.3: Commenting on the code

  1. Open the online compiler:
  2. Enter two comments using the two options seen above to write comments. An example could be:
public class NuevaClass {
    //main method. The fist executed
    public static void main(String args[]) {
      int x=10;
      int y=25;
      int z=x+y;

      System.out.println("Sum of x+y = " + z);
    }  /*in any case, 
    we´re just getting started and 
    this is a comment example*/
}

NOTE:

This post is part of the “Java” collection that reproduces the notes of the class that I teach on the subject at ESIC University. You can see the index of this collection here.