WEB Programmer Unit 1

ADVANCED LEVEL

Youth Included ZS

UNIT 1: The Java programming language

AIMS & OBJECTIVES

1) The purpose of this Unit is to enhance migrants’ basic soft skills and competencies, which are needed in today’s labor market and help them in their fist steps while in search for a job.

2) The Unit includes four units, focusing on themes such as installing Java, creating variables, creating your first program in Java and other.

3) The main part of this presentation is the compilation of programmers with Java.

4) The Unit aims at helping the reader to understand «сompiling and running», «сreating variables», «data type recognition», «debugging problems », «creating constants».

LEARNING OUTCOMES

LOut1: Understand the concept of Java language

LOut2: Describe the installation steps, regarding the Java language

LOut3: Create your first program in Java

LOut4: Demonstrate knowledge in running programs

LOut5: Contrast data types

LOut6: Formulate constants

KEYWORDS

  • Variables
  • Java
  • Javac
  • JavaDevelopmentKit
  • James Gosling
  • JavaRuntimeEnvironment (JRE)
  • StandardEditionDevelopment Kit (JDK)
  • 1995
  • Constants

TABLE OF CONTENTS

1.1 Introduction

The Java programming language was developed in 1990 by James Gosling, an engineer at SunMicrosystems. He decided to create a new language, since he was not very happy with the C ++ programming language used, and at first named it Oak * – after the oak tree that he could observe from his office window. As the popularity of the World Wide Web grew, Sun suggested that the language developed by Gosling could be used for Internet development. Subsequently, the language was given the name Java (simply because it sounds better), and in 1995 it became freely available. Developers around the world quickly adapted to the beautiful new language and, thanks to its modular design, were able to create new functionality, thereby enriching the language core. Subsequent versions of the language added many impressive features that make Java a very powerful tool today. At the heart of the Java language are libraries of files called classes, each of which contains small chunks of tested, ready-to-run code. Like bricks in a wall, any of these classes can be embedded in a new program, and so there is usually a small amount of code left to write to complete the program.

This technique saves programmers a lot of time and is one of the main reasons for the widespread popularity of Java programming. In addition, such a modular organization simplifies the debugging process: it is much easier to find an error in a small module than in one large program. Java technology is both a platform and a programming language. Java source codes are written in human-readable form in a plain text file with the .java extension, which is then compiled into .class files using the javac compiler.

    The program is then executed by the java interpreter using the Java virtual machine. Because the Java Virtual Machine is available on a variety of platforms, the same .class files can be run on both Windows and Linux, as well as on a Mac. This is the basic principle of the cross-platform language – “written once, works everywhere”. To write Java programs, you must have the class libraries and the javac compiler installed on your computer, and to run, you need to install a JavaRuntimeEnvironment (JRE) that supports the java interpreter. All of the above components are contained in the JavaPlatform package, StandardEditionDevelopment Kit (JDK), which are freely available

1.2 Installation

To install Java on your computer, select the appropriate JavaDevelopmentKit for your operating system from the Oracle download page and follow the steps below.

1.Uninstall any previous JDK and / or JavaRuntimeEnvironment installed on your system.

2. Start the installation process and accept the license agreement.

3. When the settings dialog box appears, either confirm the suggested installation location or specify your preferred location, such as C: \ Java on Windows operating systems or / usr / Java on Linux.

4. Make sure DevelopmentTools and Public JRE are selected in the list. You can also uncheck the other installation options as they are optional for the examples in this book.

5. Click the Next button to install all required Java tools and class libraries to the location of your choice.

Installation 

In Windows 8, 7, Windows Vista, or Windows XP,

click the Start button and open Control Panel ⇒ System ⇒ Advanced System Settings ⇒ Environment Variables (ControlPanel⇒System⇒AdvancedSystemSettings⇒EnvironmentVariables).

Select the system variable named Path and click the Edit button. Add the name of the bin subdirectory to the end of the variable value (for example, C: \ Java \ bin;) and click OK.

  • On Linux, add the path to the bin directory to the .bashrc file from your home directory.

For example, add the line PATH = $ PATH: / usr / Java / bin and save the file. To check the installed environment, open a command prompt, enter java -version in it and press Enter to see the version number of the interpreter. Now type javac -version, hit Enter and you will see the version number of the compiler. The numbers must match – in our case, this is version 1.8.0. You are now ready to start programming in Java

1.3 Creating your first program in Java

All Java programs usually start out as text files, which are then used to create “class” files, which in turn are actually executable programs. This means that Java programs can be written in any basic text editor, such as Notepad. Follow these steps to create a simple Java program that displays a traditional greeting.

1.Open a basic text editor, for example, Notepad and type the following code in it exactly as here – you will create a class named Hello. class Hello {}

2.Between the two curly braces of the Hello class, insert the following code to create the main method of the Hello class. public static void main (String [] args) {}

3.Between the curly braces of the main method, add the following line of code that defines what the program will do. System.out.println (“Hello World!”);

4.Save the file in any convenient place and name it exactly like this: Hello.java – the finished program now looks like this:

Creating your first program in Java


  • For a clearer understanding of each separate part of the program, we will consider them individually. The program container class Hello {} The program name is declared after the class keyword, followed by a pair of curly braces. All program code that will define the Hello class will be placed inside these curly braces. The main method public static void main (String [] args) {} This eerie-looking line is the standard code for defining the starting point of virtually all Java programs.
  • As such, it will be used in almost all of the examples in this book, so it’s worth remembering. The code declares a method named main, which will contain all program instructions inside curly braces. The public static void keywords preceding the method name define how the method should be used and are explained in more detail later. The line of code (String [] args) is used when passing values to the method and will also be explained in more detail later. System.out.println statement (“Hello World!”);
  • Statements are commands that the program must execute and must always end with a semicolon. A method can contain multiple statements within its curly braces, thereby forming a “statement block” that defines a set of tasks to perform. In this case, a single statement instructs the program to output a line of text.

Add Your Heading Text Here

1.4 Compiling and running

PHASE 1:

Before running a Java program for execution, it must be compiled into a class file using the Java compiler, which is located in the bin subdirectory and is named javac. It was previously described how to add the bin subdirectory to the system path so that the javac compiler can be run from anywhere on the system. Follow the steps below to compile the program from the previous page.

1.Open a command prompt (terminal window) and change to the directory where you saved the Hello source file. java.

2.At the prompt, type javac followed by a space and the filename Hello. java with source code and press Enter.

1.4 Compiling and running

PHASE 2:

If the javac compiler finds errors in the code, it stops and displays a message indicating the nature of the error – see below for debugging problems. In case the javac compiler does not find any errors, it creates a new file with the program name and .class extension.

1.4 Compiling and running

PHASE 3:

  • When the compilation process completes, focus returns to the command line without any warning message, and the program is ready to run.
  • The Java program interpreter is an application named java that is also located in the bin subdirectory along with the javac compiler. Since this directory has already been added to the system path, the java interpreter can also be launched from anywhere. Tip If you just type javac in the command line (terminal window) and press Enter, you can display the Java compiler options.

*Tip: You can also start compiling the source code from anywhere, if you give the javac compiler the full path to the file – in this case, it is C: \ MyJava \ Hello.java.

  • Follow the steps below to run the program that was compiled using the procedure on the previous page.

1.Open a command prompt (terminal window) and change to the directory where the Hello.class program file is located.

2.Type java followed by the program name Hello, and then press the Enter key.

1.4 Compiling and running

PHASE 4:

The Hello program starts and executes the commands described in the statements of the main method, in this case it outputs the string Hello, world! Once completed, focus returns to the prompt again.

1.5 Creating variables

Definition: In Java programming, a “variable” is a container in which a value can be stored for later use in a program. The stored value can change as the program is executed – hence the name “variable”. A variable is created using a “declaration” that specifies the type of data contained in the variable and gives it a name.

For example, to declare a String type variable named message that will contain plain text, we write:

1.String message; Programmers must follow some conventions when naming variables. A variable name in Java can begin only with a Latin letter, $ sign, or _ sign. Subsequent characters can be Latin letters, numbers, $ signs and _ signs.

2.All names are case sensitive, so var and Var are completely different variables. Space characters are not allowed in names. It is also prohibited to use the Java keywords presented in the table below, which have special meaning in the language, as variable names:

1.5 Creating variables

Follow these steps to create a program in which a variable is declared, immediately initialized, and then modified:

1.Create a new program named FirstVariable that contains a main method. class FirstVariable {public static void main (String [] args) {}}

2.Inside the curly braces of the main method, add the following code to create, initialize, and then output a variable: String message = “Initial value”; System.out.println (message);

3.Add the following lines where the value of the variable is changed and displayed again: message = “Changed value”; System.out.println (message);

Save the program as FirstVariable.java, then compile and run.

1.6 Data type recognition

The data types most commonly used when declaring variables in Java are listed in the table below with a brief description:

1.6 Data type recognition

  1. Note that variables of type char are enclosed in single quotes, while variables of type String are enclosed in double quotes. Also remember that a float value always has an f suffix.
  2. Create a new program named DataTypes that contains a standard main method. class DataTypes {public static void main (String [] args) {}}
  3. Inside the curly braces of the main method, add the five variable declarations as follows. char letter = ‘M’; String title = “Java in easy steps”; int number = 365; float decimal = 98.6f; boolean result = true;
  4. Add the following lines to output the corresponding variables with string concatenation. System.out.println (“Letter” + letter); System.out.println (“Title” + title); System.out.println (“Number of days” + number); System.out.println (“Temperature” + decimal); System.out.println (“Reply” + result);
  5. Save the program as DataTypes.java, then compile and run

1.7 Debugging problems

Sometimes the javac compiler or java interpreter will report errors. Therefore, it is helpful to understand the cause of these errors and know how to quickly fix the problem. To illustrate some common error messages, here’s an example:

1.class test {public static void main (String [] args) {String text; System.out.println (“Test” + text)}} The first attempt to compile Test.java will throw the following error message/ Cause: The javac compiler was not found/ Solution – edit the system PATH variable as described earlier in this chapter, or use the full path to the compiler to run it.

1.7 Debugging problems

Reason – the Test.java file was not found /Solution – Either change to the directory where the file is located, or use the full path to the file in the command itself.

1.7 Debugging problems

Reason – the statement did not complete correctly/ Solution – In the source code, add a semicolon to the end of the statement, then save the file to accept the changes.

1.7 Debugging problems

Reason – the variable text does not matter/ Solution – in the variable declaration, assign the correct string value to the variable text, for example, text = “Successful”, then save the file.

SYNOPSIS

The Java programming language was developed in 1990 by James Gosling, an engineer at SunMicrosystems. Subsequently, the language was given the name Java (simply because it sounds better),  and in 1995 it became freely available. In the center of Java language are libraries of files called classes, each of which contains small chunks of tested, ready-to-run code.

Like bricks in a wall, any of these classes can be embedded in a new program, and so there is usually a small amount of code left to write to complete the program. This technique saves programmers a lot of time and is one of the main reasons for the widespread popularity of Java programming. All Java programs usually start out as text files, which are then used to create “class” files, which in turn are actually executable programs. This means that Java programs can be written in any basic text editor, such as Notepad. Follow these steps to create a simple Java program that displays a traditional greeting.

In Java programming, a “variable” is a container in which a value can be stored for later use in a program. The stored value can change as the program is executed – hence the name “variable”. A variable is created using a “declaration” that specifies the type of data contained in the variable and gives it a name.

List of references

Further reading