INTRODUCTION TO JAVA
STRUCTURE OF JAVA PROGRAMS
The minimum entities that we can represent within a Java program are Attributes and Methods, which are the basic components of an Object Oriented program. Both do not live a life of their own within a program written in Java but they must always be placed within a class definition. A class can be regarded as the basic logical and autonomous entity of the language. Attributes and methods are also known as class members.
Members of a class are divided into instance members and class members. The definition of a class in Java can contain either instance members or class members or both. Class attributes and methods in Java are called static members.
The definition of a class must always be implemented within a source file having the extension .java
The name of the source file must necessarily be the same as that of the class. Within a single source we can declare multiple classes.
I PACKAGE
Only one will be the main class, which is then the one that provides the name to the source file. A source in Java is always part of a namespace that is used to avoid having collisions between different Java programs that have the same name; they also organize Java code into more coherent logical units. No more than one program with the same name can exist within a namespace. Explicitly or implicitly a Java program is always part of a package.
MODULES
Multiple programs in Java can all belong to the same namespace. Classes that belong to the same package enjoy a privileged relationship, since they are considered parts of a coherent logical unit, thus tighter than classes that are in different packages. Finally, the package may belong to an even higher entity, the module. We will see later how they work, how they are represented, and how forms are created. Modules allow us to structure programs into separate software components that can, however, interact with each other.
A module may contain within itself a set of packages, the modules in turn interact with each other by exporting some packages they contain so that other modules can import them and use them appropriately. This means that some packages are designed to stay within a module, others to be exported. The modules in Java allow us to reason and have private and public packages. It is this feature of modules that enables us to make software developed by components.
MAKE A PROGRAM EXECUTABLE
If we want to make a program executable, we must make the class executable; this is done through a particular static method called
main(). What you see below is the simplest program that can be made in Java.
COMPILING A JAVA PROGRAM
INTERPRETED LANGUAGES
First we need to understand well the difference between an interpreter and a compiler. When we write a program with an interpreted programming language such as Python and Ruby there is no actual compilation phase, the source is passed at run time to a special program called an Interpreter, which examines the source file and executes one instruction at a time. This means that each time we execute the program, the interpreter re-executes one instruction at a time and makes it executable in machine language.
COMPILED LANGUAGES
Conversely, when we compile the program in languages such as C, C++, Go etc. The source is compiled and made executable in binary form in the particular host hardware platform, this executable program can be put into execution from a terminal, for example. Compiled programs have higher performance than interpreted ones, however, they have an underlying problem, which is portability, i.e., running on different hardware platforms, this means that the code produced is suitable for a particular processor and Operating System, to run the same program on different architectures additional compilation steps are needed and sometimes the sources need to be modified.
JAVA
A different path has been taken in Java; Java is a partially interpreted and compiled language; it is a middle ground between interpreters and compilers. Bytecode is a special format, it is not the machine language translation for a specific platform, it is the machine code for a virtual machine the JVM, (Java Virtual Machine). The JVM performs the last compilation step and proceeds to send our program into execution. A Java program written for Mac can be run on a Windows or Linux platform equipped with its own JVM without any need to modify the sources or recompile the code.
Leave A Comment