Site Overlay

Important Aspects of the java program

Hits: 125

The Java Compiler

The Java Compiler is a program that translates codes written in java to machine instructions (‘0s and 1s’). A successfully compiled program can be run on any computer machine as an executable file.

Types and Variables

A variable is a symbol that identifies a storage location and can hold different values-Cay S Horstmann. Consider the following example,

Example  If R is a variable, contained in the equation:

Area = p x R x R, (area of a circle)

Then changing the value of R will result in different values of the Area and hence R is a variable that identifies a storage location for the radius of a circle.

In Java, each variable has a type. This means that the variable can only accept values of its own type.  Some examples of type variables include, integers, floating-point variables, double precision floating-point variables and single precision floating-point variables. These variable types are discussed in detail below.


These are numbers like 0,1,4,6,3,8…. etc, they do not have any decimal points. In java an integer variable is declared as int followed by the variable name, see Example 2 below.

 Example Integer variable.

Type Name variable Name;

 If R is an integer, it is declared as; int R; in the java.

  • Floating-point variables

These are variables defined by the number of significant digits and an indication of the position of the decimal point. These variables are floating because if they are divided or multiplied by 10, only the decimal point position changes (floats).

There are Double precision floating-point variables, and these have up to two decimal places e.g. 0.29. There are also Single precision floating-point variables that have up to one decimal place, e.g. 10.1, 100.0. Consider Example 2 below.

 Example Floating-point variables.

R is declared as; double R; in java for a double precision floating-point.

R is declared as; float R; in java for a single precision floating-point.

Methods, Classes And Objects

A Method is a sequence of statements that has a name, may have formal parameters, and may return a value. A method can be invoked any number of times with different values for its parameters- objectsCay S Horstmann.

An Object is a program entity that can be manipulated by calling methods and is contained within a class.  A class on the other hand can be thought of as a ‘factory’ of objectsCay S Horstmann.

A Method describes an object’s characteristics or behavior. In simple terms a method states what an object does, for example if an object is ‘child’, then the methods that describe the behavior of this child would be, cry, crawl, sleep etc. See the figure below

A simple diagram showing the relationship between methods, classes and objects.

A program can have more than one class each having one or more objects and multiple methods describing the objects’ characteristics. Curly ‘{}’ brackets are used to mark the start ‘{‘ and the end ’}’of a class or a method or sometimes used within some program commands such as a ‘for loop’.  A for loop is a statement that repeats itself a number of times, i.e. it ‘loops’ see Example 3.

Example A for loop that draws four rectangles

Note: the (x by y) size of the rectangles is 8 by 8, the x position of the rectangles is 250, and each time a new rectangle is drawn at y positions, 250,251,252 and 253.This is because the y value is incremented by 1, each time we want to draw a new rectangle while the x value remains at 250.

Packages in Java

A package is a collection of classes with related purposes. All classes are contained in a standard library, which is a collection of classes that are predefined and ready to be used in programming.  All the classes in the standard library are contained in packages. To import a class from a package in order to use it in the program, the following command code routine is used:

Example Importing the class Rectangle from the java.awt package

import package Name. Class Name;

Example: import java.awt.Rectangle;

Note: Awt stands for ‘Abstract Windowing Toolkit’. The java.awt toolkit contains many classes for drawing windows and graphical shapes. 

Writing A Program and Algorithm

An Algorithm is a solution technique that is unambiguous, executable and terminating-Cay S Horstmann. These solution techniques are normally used in all programming languages. When writing a particular code that is to perform a specified task, the method used is unambiguous because there are precise instructions set for the task, allowing no room for creativity or guesswork. The term executable means that each step used can be carried out in practice –Cay S Horstmann. In the end a solution has to be reached and hence the term terminating.

The program code can be divided into two parts, the main code and the comments. The main code is what the compiler retrieves when building (compiling) the program, and this affects the behavior of the program if altered. The comments are written as a guide to what the main code is representing and this doesn’t affect the functioning of the program if changed. This is normally done to make programs easier to understand and to debug when need arises. A comment is written as an aside with two slashes “//” before it or it is enclosed between two slashes and two stars as in  /*comment*/. Comments are normally written in a normal English language while the code is restricted to the programming language instructions.  At the end of each line of code, there is a statement terminator, which is symbolized by a semicolon (;), this indicates that the particular statement has ended and any statements written after are independent and new to the terminating statement.

Leave a Reply

Your email address will not be published. Required fields are marked *

Please support the site
By clicking any of these buttons you help our site to get better
Social PopUP by Timersys