Flylib.com

Books Software

 
 
 

Chapter 2. Moving from Fortran to Java


Chapter 2. Moving from Fortran to Java

If you are moving from Fortran to Java, there will be a learning curve. Java is a C and C++ based language, and the basic syntax used by Java is different than Fortran. What's more, Java is an object-oriented language. You will have to learn to think and program in an object-oriented manner. The good news is that simplicity was one of the design goals of the developers who created Java. It's much easier moving from Fortran to Java than it is to move from Fortran to a language like C++.

You will quickly realize that Java is more powerful and versatile than Fortran. There are more programming constructs and more ways of customizing programming elements. Java is a more comprehensive language as well. It offers a lot more features and can be used for a wider range of programming disciplines. The Fortran libraries are limited in scope, consisting mainly of mathematical functions. Java has enormous libraries covering almost every conceivable programming need. You will find when developing complicated multidisciplinary applications that a lot of the programming work has already been done for you.

This chapter will shed some light on the things you will have to learn and consider when moving to the Java world. It will focus not only on the basic syntactical differences but also touch briefly on object-oriented programming concepts that you will need to know. This chapter is not meant to be a compre hensive tutorial for either Fortran or Java. For a detailed description of the core elements of the Java language, look to Chapters 5 “16.

The specific topics we will discuss in this chapter are ”

  • Program structure

  • Basic syntax

  • Variables

  • Subroutines, functions, and methods

  • Arrays

  • Dynamic memory allocation

  • Pointers

  • Exception handling

  • Libraries

  • Built-in math functions

  • Input/Output capabilities

  • GUIs and web-based applications


Program Structure

Java is an object-oriented, C-based language. Fortran is neither . While some advocates of the more recent versions of Fortran (Fortran 90 and Fortran 95) have tried to pass the language off as being almost object oriented, in truth it models only some object-oriented features but does not directly support such important concepts as inheritance and dynamic dispatching. In addition, Fortran 90 and 95 are built upon legacy versions (Fortran 77 primarily) that aren't object oriented in the least. Java was designed from the ground up to be a true object-oriented language.

One of the big differences between Fortran and Java is the basic program structure of the two languages. A Fortran program consists of a main program section and zero or more subroutines or functions. Program execution starts at the top of the main program section. The main program section is terminated with stop and end statements. Subroutines and functions are terminated with end statements. A Fortran program usually consists of variable declarations and a series of subroutine and/or function calls.

Java is an object-oriented programming language. The fundamental building block of a Java program is a named block of code called a class. Classes are usually designed to be self-contained. They declare fields (i.e., variables ) and methods (the Java equivalent of subroutines) that are used to access and manipulate these fields and perform other functions as well. All statements in a Java program must be placed inside a class or interface.

Classes define the structure of the objects that make Java object oriented. An object is an instance of a class. Java arrays are also objects although no explicit class defines them. An object will have its own copy of the nonstatic data members and methods defined by its class. Java programs will usually declare at least one variable that refers to an object. A Java application can contain more than one class, but one of the classes will declare a main() method. The main() method is the entry point for program execution. The system calls the main() method when the program is run.