Starting to Code in Java – Part 1

In this section, we will go through an introduction to Java programming and look at key aspects of this programming language, including:

  • It’s syntax.
  • Variable types and scope.
  • Methods

 

So, after completing this chapter, you will be able to:

  • Understand key concepts related to Java programming.
  • Understand how to use variables and methods.
  • Create and use your own methods.

The code solutions for this chapter are included in the resource pack that you can download by following the instructions included in the section entitled “Support and Resources for this Book“.

What is Java

Java is a high level object-oriented language developed by Sun. It is a multiplatform language which means that once it has been written and compiled, a Java application can run on most platforms.

The fact that it is an object-oriented programming language means that a Java programme consists of a collection of objects that interact with each other.

Java is used for many purposes, including the creation desktop or mobile applications or even games.

Choosing a code editor

In the next sections, you will start to write your own Java code. For this purpose, you will need to use a code editor, and more specifically, an Integrated Development Environment (IDE). These IDEs include all the necessary tools to compile and execute your code, in addition to other options that will speed-up your development and also help you to keep it free from bugs.

Using an IDE will provide you with an easier way to create, organize and check your code, with some of the following features:

  • Text highlighting: so that you code is clearer to read and to modify.
  • Auto-completion: so that you don’t have to remember the name of all the variables in your code, or the name of some common methods.
  • Error highlighting: so that some obvious errors are highlighted before you even test your code.

 

There are many free IDEs available with their advantages and drawbacks, including:

  • BlueJ
  • IntelliJ IDEA.
  • NetBeans

All these IDEs considered, we will be using BlueJ for this book, although you could use any of the other IDEs if you wish. BlueJ is an IDE specially developed for beginners. It won’t overwhelm you with too many features, but instead, help you to get started with Java and provide some very useful features along the way that will make your coding clearer, such as class diagrams, or code split into color-coded blocks.

You can download BlueJ from the following site: https://www.bluej.org/

 

Installing Java

Before you start working with Java, you will need to install the most recent version of the Java Development Kit (JDK). The JDK includes all the libraries that you need to be able to write, compile and run code in Java.

To do so, please do the following:

  • Open the following page:

https://www.oracle.com/technetwork/Java/Javase/downloads/index.html

  • Look for the latest version of Java SE Development Kit.
  • Download the most recent Java SE DK: as this book is being written, the latest version of Java is Java SE 11.0.2.

Figure 1‑1: Downloading the SDK(Part 1)

  • Once you click on the Download button, a new set of options will appear from which you can chose the version that you would like to download, after accepting the license agreement.

Figure 1‑2: Downloading the SDK(Part 2)

  • Once you have downloaded the software please launch it and follow the instructions.

 

Installing BlueJ

Once the Java SDK has been installed, it is time to install BlueJ, which is an IDE that you will use to write your code in this book. BlueJ was designed with Java beginners in mind and includes many features that make it easy to create Java applications. So let’s install this software:

  • Please open the following link: https://www.bluej.org/.
  • Download the BlueJ version that corresponds to your operating system (i.e., for Mac OS, Windows, or Linux).

Figure 1‑3: Installing BlueJ

If you are using a Windows computer:

  • Double-click the install file that you have downloaded.
  • This will run a standard installation. Follow the instructions on screen. The installer will install the executable bluej.exe.
  • If when executing BlueJ you see a dialog box asking you whether you want to block or unblock BlueJ, you may choose not to block BlueJ so that it can be launched seamlessly.
  • The first time you launch BlueJ, and if you have more than one Java version installed on your computer, you will be asked to select your preferred version.

If you are using a Mac:

  • Download the installer for Mac (this is a zip file).
  • Decompress the zip file; this will create a folder called BlueJ.
  • Move the resulting folder to your Applications folder for example.
  • You can then click on the icon called BlueJ, to launch this application.

Figure 1‑4: Installing BlueJ on a Mac

 

Creating your first application

After this brief introduction, it is now your turn to code.

So that you get to experiment with your Java code, please do the following after installing BlueJ:

  • Please launch BlueJ.
  • Once BlueJ has been launched, select: Project | New Project from the top menu to create a new project.
  • In the new window, please select a name and a location for your project and click “OK“.

Figure 1‑5: Selecting a location for the project

  • Once this is done, a new window will appear that will make it possible for you to create your first programme.

Figure 1‑6: Creating your first programme in Java (part 1)

  • Click on the button called “New Class” that is located on the left-hand side of the window, as per the previous figure. This will open a new window.
  • Enter a name of your choice in the text field for the attribute “Class Name“, leave all the other options as default, and press “OK“.

Figure 1‑7: Creating your first programme in Java (part 2)

  • You should see that a new class called MyClass has been created within your project.

Figure 1‑8: Creating your first programme in Java (part 3)

  • You can now double-click on the icon labelled “MyClass“, as per the previous figure.
  • This will open a new window that displays the code that has been created by default for your new class, as follows.

/**

* Write a description of class MyClass here.

*

* @author (your name)

* @version (a version number or a date)

*/

public class MyClass

{

// instance variables – replace the example below with your own

private int x;

 

/**

* Constructor for objects of class MyClass

*/

public MyClass()

{

// initialise instance variables

x = 0;

}

 

/**

* An example of a method – replace this comment with your own

*

* @param  y  a sample parameter for a method

* @return    the sum of x and y

*/

public int sampleMethod(int y)

{

// put your code here

return x + y;

}

}

Since we will not be using all this code, please do the following:

  • Select all the code (CTRL + A).
  • Press delete to delete all the code.
  • Type the following code instead:

public class MyClass

{

public static void main (String [] parameters)

{

System.out.println(“Hello World”);

}

 

}

In the previous code:

  • We declare a class called MyClass. As we will see later, your application will be made of a collection of classes. One of these classes will be the class MyClass which the starting point for your application.
  • We also declare a method called main. This method will be the starting point for our application.
  • In this method, and this is the most important part for now, we write the text “Hello World” in the console window. This window will be displayed as you execute your code.

You can now do the following:

  • Compile and execute this code by pressing the button labelled “Compile” from the top of the window.

Figure 1‑9: Compiling your code

  • Check that your code has been compiled properly by ensuring that the message “Class Compiled – no syntax errors” is displayed at the bottom of the window.

Figure 1‑10: Checking for errors

  • Run your programme: switch to the “Project” window, right-click on the icon labelled “MyClass“, and select the option void main(String [] parameters) from the contextual menu, as illustrated in the next figure.

Figure 1‑11: Running your first lines of code (part 1)

  • A new window will appear asking you whether you want to enter parameters: just press “OK” for now.

Figure 1‑12: Running your first lines of code (part 2)

  • Once this is done, the terminal window should appear as follows.

Figure 1‑13: Your first console application

As illustrated in the previous figure, the text “Hello World” was written and displayed in the terminal window, based on your code.

We could now change our code to display a different message:

  • Please close the console window.
  • Edit your code and modify it as follows (new code in bold). To edit your code, you can double click on the icon called MyClass in the main window.

public static void main(String[] args)

{

System.out.println(“Hello, this is my first line of code”);

}

In the previous code, we just modified the message displayed in the console window to add the text “this is my first line of code”.

  • You can then compile and run your code and see the new message: “Hello, this is my first line of code” in the console window.

 

About compiling and Java bytecode

When you write your Java source code, it is usually saved as .Java file. Once your file is compiled, it is converted (i.e., compiled) into bytecode as a .class file. This code, in turn, is interpreted by the Java Virtual Machine (JVM) that is installed on your computer and directly executed or translated into a code that the computer can understand (i.e., machine code) and execute. So, the JVM acts as a translator between your bytecode and the computer. The advantage of this approach is that given that a Java Virtual Machine can be installed on most computers, your code after being compiled into bytecode, can be executed on any computer where the Java Virtual Machine has been installed. You can therefore compile your code once and run the code virtually anywhere.

You may wonder how the JVM is installed. Well, when you installed the Java SDK at the very start of this book, it installed, amongst other things, a JVA Runtime Environment (JRE) that includes the Java Virtual Machine, as well as the Java compiler.

You can see both the .Java and .class that have been created and compiled in your project, if you navigate to the folder that you have chosen for your project initially. For example, in the next figure, you can see that several files, including MyClass.Java and MyClass.class, that belong to the current project.

Figure 1‑14: Locating the .class and .Java files

Related Articles: