Learn Java and Master Writing Code

Look inside

Download on Amazon
 Source code   Info   About this book   Required Knowledge 

If you are a beginner and struggle to understand a simple Java code, this step by step explanation will help you how to write and read code. I will explain most of the concepts you need to start with the easy Java exercises.

1.Java IDE (Integrated Development Environment)

You cannot learn programming by only reading books, because by reading you cannot communicate with the code, while by working with it you can change it continually and run it over and over again to see the effect of your changes. Besides you can test the results of your changes in seconds. To create a Java development environment you need to install one of the free IDE that available for Java. We use as Java editor, Eclipse including SDK (Java Development Kit) or JSE (Java standard editon);

Download and install Java SE 10 from the site of Oracle.
Eclipse is a free Java IDE. You can download it here!

Notice: You have to download the right version of Eclipse for your computer and your operating system.

Once you installed Java and Eclipse, here below is a step by step explanation to compile and run your first simple Java program.

By Installing Eclipse a folder called workspace would be created in which you can find all your projects.

 

  • 1. When you run Eclipse for the first time, a window like below is opened. Close the Welcome message by clicking on the cross as shown below.

 

  • 2. When you close the Welcome message, the following window is shown. The tab Package Explorer appears automatically as shown below.

 

  • 3. Click on Windows -> Show View -> Navigator to open the Navigator tab as shown below.

  • 4. Once you open the Navigator tab try to create a Java project. Click on
    File -> New -> Java Project.

 

  • 5.  In  the following window write the name of the project. Call the project my_project and click on the Finish button.

 

  • 6. Select the Navigator tab and open the map my_project.  Right mouse click on the sub map src as shown  below then  choose  ->  New  ->  Class to create a new Java class.

 

  • 7. In the following window write the name of the class which we call Student and click on the button Finish

 

  • 8. Copy and paste the following class code of Student inside the class Student that you created in Eclipse. Now you can run the program by clicking on Run – Run or click directly on the green arrow button above. In the standard output below you see that the output is Name: Jack Age: 21
  • So far you have created the development environment for your Java code, but now I am going to explain every line of the following code.

 

public class Student // 1
{
  String name; // 2
  int age; // 3
  
  public static void main(String[] args) // 4
  {
    Student student1 = new Student(); // 5
    student1.name = "Jack"; // 6
    student1.age = 21; // 7
    System.out.println("Name: " + student1.name); // 8
    System.out.println("Age: " + student1.age); // 9
  }
}

 

 

How to write a Java class?

A Java class starts always with a capital letter. If you create a class that keeps data of products for example, the most suitable name that you can choose for such a class is Product. If you create a class Product, the content of the class is stored in a file with the exact name of the class and the extension .java. You don’t need to worry about that, because any class you create in Eclipse is automatically stored in a file with the name of the class and the extension java.
In our example the class product will be stored in a file calls: Product.java. You can find your classes in the map workspace that is created by Eclipse during the installation.

Line 1:

public class Student // 1
{
  // code
}
  • public: public is a Java keyword means that the access of the class Student is public.
  • class:  every class in Java starts with the Java keyword class.
  • Student: Suppose that you want to write a program that keeps the data of students. To write such a program in object oriented programming, you create a class with a name that you choose. A clas is a kind of template or a blue print that allows you to create unlimited number of students.
  • {} : Classe bodies in Java are within a start and an end brace. This is called a block of code.

How to declare variables?

Line 2 & 3:

public class Student // 1
{
  String name; // 2
  int age; // 3
}
  • In this example we need to collect data of students. The class Student of our example allows only to keep the name and the age of the students. If you want more attributes of the students you need to declare more variables. For example, if you need to keep the email addresses of the students, you have to add another String type variable email.
  • String: String is a type of variable that we chose, because we expect more than one letter for the name of the students. Usually String is used for texts.
  • name: We chose the variable name as name, because we use this for the names of the students.
  • int: int is a type of variable that we use when we expect a number. In this case we expect a number for the age of the students.
  • age: age is the name of the variable that we use to store the ages of the students.

Why do we use Java main method?

Line 4

public class Student // 1
{
  public static void main(String[] args) // 4
  {
    // code
  }
}

The main method in Java is a method that is needed to execute a Java program. Methods in Java are written within classes and a body method is within a start and an end brace, just like classes. This is called block of code. The main method begins as follows.

public static void main(String [] args)

For the moment it is important to learn the following about the main method.

All executable programs must have a main method such as below. The statements within the main method are executed. The execution of the statements is done from top to bottom. In our example statement 5 is first executed, then statement 6, 7, 8 and at last statement 9.

 

public class Student // 1
{
  String name; // 2
  int age; // 3
  
  public static void main(String[] args) // 4
  {
    Student student1 = new Student(); // 5
    student1.name = "Jack"; // 6
    student1.age = 21; // 7
    System.out.println("Name: " + student1.name); // 8
    System.out.println("Age: " + student1.age); // 9
  }
}

 

How to create objects from a class?

Line 5

  • Student student1 = new Student(); To create objects from a class in Java we write a statement that starts with the name of the class then the name of the object that you create, which is student1. We created The student object student1 for the student “Jack”. If you want to create another object for a student Emma for example, then you need to use the statement. Student student2 = new Student();. You should choose different names for the objects. Therefore, we use the name student2 for our second object. It is important to know that the names of objects unlike classes start with small letters

How to assign values to the variables?

Line 6 & 7

  • Once you create an object, you need to assign values to the variables. as shown in the code. Notice that names of variables(attributes) start with small letters in Java.
  • For the student “Jack” we have assigned the name Jack and the age 21 to the variables name and age respectively.
  • To assign values to the variables we use the name of the object and the name of the variable separated by a dot as shown below.
  • student1.name = “Jack”; // 6
  • student1.age = 21; // 7

How to write values to the standard output?

Line 8 & 9

  • We have created a class, the object student1 and assigned values to the attributes of the student1. If we now compile and run the program, nothing is printed to the standard output, because that is the task of the statements 8 en 9, which writes the values of the variables to the standard output.
  • System.out.println(“Name: ” + student1.name); // 8
    System.out.println(“Age: ” + student1.age); // 9

The statement System.out.println(); is used to test your program. It is not necessary for now to understand all the details about this statement, but it is important to know what you can do with this.

You can use this statement to write values of variables and texts to the standard output as shown in the following example.

public class Student
{
  String name;
  int age;
  
  public static void main(String[] args)
  {
    Student student1 = new Student();
    Student student2 = new Student();
    student1.name = "Emily";
    student1.age = 21;
    student2.name = "Justin";
    student2.age = 22;
    System.out.println("Name is: " + student1.name); // 1
    System.out.println("Age:     " + student1.age); // 2
    System.out.println("Name is: " + student2.name); // 3
    System.out.println("Age:     " + student2.age); // 4
  }
}

If you compile and run the above code, the following is written to the standard output:

/*
Name is:   Emily
Age:       21 years
Name is:   Justin
Age:       22 years
*/
  1. To write a text to the standard output, it must be between quotation marks, but for the value of variables that is not required.
  2. To write a combination of texts and variables to the standard output, you need to use the plus (+) sign.
  3. To write the texts and the variables on one line to the standard output you need to use print instead of println.

How to write comments in Java?

Comments are ignored by the compiler. Below are two ways to add comments to your code.

  1. Comment of a single line begins with two slashes //. Everything on the right side of these characters, see Java as comments: // comment of a single-line
  2. Comment of multiple lines starts with /* and ends with */. Everything between these two characters sees Java as comments: /* here is a comment of multiple lines */
public class Student
{
  String name;
  int age;
  
  public static void main(String[] args)
  {
    Student student1 = new Student();
    // Assign the value Emma to the variable name
    student1.name = "Emma";
    student1.age = 22;
    /*
      The following statements write the name and 
      the age of the student1 to the standard output
     */
    System.out.println("Name is: " + student1.name);
    System.out.println("Age:     " + student1.age);
  }
}
/*
Name is: Emma
Age:     22
*/

 

Are you ready to test whether you have learned from this article? Click here to try some Java quizzes!

  Quizzes     Assignments     Puzzles     Easy exercises     Required knowledge  


Sar Maroof is a professional software development teacher, gives master classes and publishes technical articles. He is also an expert software developer and worked for several big as well as small companies and later as a freelancer.

 Subscribe to Sar Maroof's RSS feeds!
 Mail this page to a friend!
 Bookmark this page!