- Forward


An Introduction to Java
for FORTRAN 90 Programmers


Prof. David Bernstein
James Madison University

Computer Science Department
bernstdh@jmu.edu

Print

Basic Style Issues
Back SMYC Forward
  • FORTRAN:
    • Is line oriented (with a maximum of 132 characters per line); lines can be extended using an & character
    • "Formatting" is important in some versions of FORTRAN
    • Everything on a line after a ! is a comment (i.e., is ignored)
  • Java:
    • Is statement-oriented and every statement ends with a ;
    • "Formatting" tends to be ignored (e.g., extra spaces, tabs)
    • Everything on a line after a // is a comment as is everything betweem a /* and */
    • Block statements are contained between a { and a }
Basic Language Issues
Back SMYC Forward
  • FORTRAN:
    • Is procedural and has derived types
    • Is modular
    • Supports operator overloading
    • Supports POINTERs and TARGETs
  • Java:
    • Is object-oriented
    • Is modular
    • Doesn't support operator overloading
    • Objects are reference types; primitives are value types
Naming Conventions
Back SMYC Forward
  • FORTRAN:
    • Names (i.e., identifiers) must start with a letter
    • Names can contain letters, digits and '_'
    • Names are not case-sensitive
  • Java:
    • Names (i.e., identifiers) must start with a letter
    • Names can contain letters, digits and '_'
    • Names are case-sensitive
Primitive Types
Back SMYC Forward
FORTRAN
Type
INTEGER
REAL
CHARACTER
LOGICAL
Java
Type Memory Range
byte 1 byte -128 to 127
short 2 bytes -32,768 to 32,767
int 4 bytes -2,147,483,648 to 2,147,483,647
float 4 bytes
double 8 bytes
char 2 bytes Unicode
boolean 1 bit true or false
Declarations
Back SMYC Forward
  • FORTRAN
    • Syntax:
      type [,attribute] :: variable[=value] [, variable[=value]]...
    • Examples:
      REAL :: temperature, pressure
      INTEGER :: month=1, year=2007
    • Supports implicit declarations (e.g., variables that start with i,j,k,l,m,n are implicitly integers)
  • Java:
    • Syntax:
      type variable[=value] [, variable[=value]]...
    • Examples:
      double temperature, pressure;
      int month=1, year=2007;
    • All variables must be explicitly declared
"Constants"
Back SMYC Forward
  • FORTRAN:
    • Can be specified using the PARAMETER attribute
    • Examples:
      REAL, PARAMETER :: pi=3.1415926
  • Java:
    • Can be specified using the final qualifier
    • Examples:
      final double pi=3.1415926;
Arithmetic Operators
Back SMYC Forward
  • FORTRAN:
    • Addition (+)
      Subtraction (-)
      Multiplcation (*)
      Division (/)
      Exponentiation (**)
  • Java:
    • Addition (+)
      Subtraction (-)
      Multiplcation (*)
      Division (/)
      Integer Division (/)
      Modulo (%)
      Increment (++)
      Decrement (--)
Arithmetic Operators (cont.)
Back SMYC Forward
  • FORTRAN:
    • Precedence:
      Parentheses play the "usual role"
      Highest: **
      Next Highest: * /
      Lowest: + -
    • Operators have left-to-right associativity
  • Java:
    • Precedence:
      Parentheses play the "usual role"
      Highest: ++ --
      Next Highest: * / %
      Lowest: + -
    • Binary operators have left-to-right associativity whereas unary operators have right-to-left associativity
Logicals/Booleans
Back SMYC Forward
  • FORTRAN:
    • Values of .TRUE. and .FALSE.
    • Unary Operators: .NOT.
    • Binary Operators: .AND., .OR., .EQV., .NEQV.
  • Java:
    • Values of true and false
    • Unary Operators: !
    • Binary Operators: &&, ||, ==, !=
Relational Operators
Back SMYC Forward
  • FORTRAN:
    • Binary Operators: <, <=, >, >=, ==, //=
  • Java:
    • Binary Operators: <, <=, >, >=, ==, !=
Characters
Back SMYC Forward
  • FORTRAN:
    • Literals can be enclosed in single or double quotes
    • Declared using the CHARACTER type
    • Example:
      CHARACTER :: gender='F'
  • Java:
    • Literals must be enclosed in single quotes
    • Declared using the char type
    • Example:
      char gender='F';
Strings
Back SMYC Forward
  • FORTRAN:
    • Literals can be enclosed in single or double quotes
    • Declared using the CHARACTER type with an additional len
    • Example:
      CHARACTER(len=10) :: title='Prof.'
  • Java:
    • Literals must be enclosed in double quotes
    • Declared using the String type
    • Example:
      String title="Prof.";
Strings (cont.)
Back SMYC Forward
  • FORTRAN:
    • Can be concatenated using the //// operator
    • Substrings can be accessed using
      name( [start]:[stop] )
      which is 1-based (e.g., title(1:2) returns 'P')
    • Can be manipulated with the intrinsic functions LEN(string), INDEX(substring, string) and TRIM(string) (e.g., LEN(title) returns 10)
  • Java:
    • Can be concatenated using the + operator
    • Substrings can be accessed using
      name.substring(start,end)
      which is 0-based (e.g., title.substring(0,1); returns "P")
    • Can be manipulated with a variety of methods including length(), indexOf(substring) and trim() (e.g., title.length() returns 5)
Arrays
Back SMYC Forward
  • FORTRAN:
    • Index bounds can be specified
    • Supports multi-dimensional arrays
    • Array literals are contained within (// and //) and are delimited by ,
    • Example Literal:
      (//1,5,9//)
  • Java:
    • 0-based
    • Supports arrays of arrays
    • Array literals are contained within { and } and are delimited by ,
    • Example Literal:
      {1,5,9}
Arrays (cont.)
Back SMYC Forward
  • FORTRAN:
    • Declaration Syntaxes:
      type, DIMENSION(bound)[, attr] :: variable[, variable]...
      type [, attr] :: variable(bound) [,variable(bound)]...
    • Examples:
      INTEGER, DIMENSION(6) :: a
      INTEGER, DIMENSION(4,2) :: b
      REAL :: c(4:9)
  • Java:
    • Declaration Syntax:
      type[] variable [,variable]...;
    • Initialization Syntax:
      variable = new type[size];
    • Example:
      int[] a;
      a = new int[6];
      int[][] b;
      b = new int[4][2];
Arrays (cont.)
Back SMYC Forward
  • FORTRAN:
    • Access Syntax:
      variable(index[,index...])
    • Examples:
      a(5)
      b(1,1)
  • Java:
    • Access Syntax:
      variable[index]
    • Examples:
      a[5]
      b[1][1]
Derived/User-Defined Types
Back SMYC Forward
  • FORTRAN:
    • Syntax:
      TYPE :: name
      statement
      [statement]...
      END TYPE [name]
    • Example:
      TYPE rectangle REAL :: height, width END TYPE rectangle
  • Java:
    • Syntax:
      public class name
      {
      public type variable [,variable>...;
      [public type variable [,variable>...;]...
      }
    • Example:
      public class rectangle { public double height, width; }
    • Classes can be used in this way but can also be used in much more interesting ways (since Java is object-oriented)
Derived/User-Defined Types (cont.)
Back SMYC Forward
  • FORTRAN:
    • Components are accessed using the % operator
    • Example:
      TYPE(rectangle) :: cage cage%height = 5 cage%width = 10
  • Java:
    • Components are accessed using the . operator
    • Example:
      Rectangle cage; cage = new Rectangle(); cage.height = 5; cage.width = 10;
Control Structures
Back SMYC Forward
  • FORTRAN:
    • Statement Syntax:
      IF (logical_expression) statement
    • Construct Syntax:
      [name: IF (logical_expression) THEN
      block
      [ELSEIF (logical_expression) THEN [name]
      block]
      [ELSE [name] block]
      ENDIF [name]
    • Example:
      IF ( n==1 ) THEN discount = 0.0 ELSEIF ( n<=5 ) THEN discount = 0.1 ELSEIF ( n>5 .AND. n<=10) THEN discount = 0.15 ELSE discount = 0.25 ENDIF
  • Java:
    • Syntax:
      if (logical_expression) statement; [else statement;]
    • Example:
      if ( n==1 ) { discount = 0.0; } else if ( n<=5 ) { discount = 0.1; } else if ( n>5 .AND. n<=10) { discount = 0.15; } else { discount = 0.25; }
Control Structures (cont.)
Back SMYC Forward
  • FORTRAN:
    • Syntax:
      [name:] DO counter=start,stop [, step
      block
      END DO [name]
    • Example:
      DO i=1,10,1 total = total + sales(i) END DO
    • CYCLE can be used to begin another iteration and EXIT can be used to "break out"
  • Java:
    • Syntax:
      for (initialization; boolean; update) statement;
    • Example:
      for (i=0; i<10; i++) { total = total + sales[i]; }
    • exit can be used to "break out"
    • A for loop can be used like a DO loop but can be used in many other ways as well
Control Structures (cont.)
Back SMYC Forward
  • FORTRAN:
  • Java:
    • Syntax:
      while (boolean) statement;
    • Example:
      while (tolerance > 0.001) { approximation = approximation + 0.01; tolerance = target - approximation; }
    • Syntax:
      do statement while (boolean);
    • Example:
      i = 0; do { total = total + sales[i]; i = i + 1; } while (i < 10);
Functions/Subroutines/Methods
Back SMYC Forward
  • FORTRAN:
    • Supports two types of procedures, functions and subroutines
    • Syntax:
      procedure name [(argument [,argument]...)]
      declarations
      statements
      END procedure [name]
    • Example:
      FUNCTION line(m, x, b) REAL :: line REAL :: m, x, b line = m*x + const END FUNCTION line
  • Java:
    • Methods can either return a value or not (in which case they have a type of void)
    • Syntax:
      accessibility [static] type name([type parameter]...)
    • Example:
      public double line(double m, double x, double b) { double result; result = m*x + b; return result; }
Functions/Subroutines/Methods (cont.)
Back SMYC Forward
  • FORTRAN:
    • All arguments are passed copy-in, copy-out
    • The following will work:
      SUBROUTINE swap(a, b) REAL :: a, b, temp temp = a a = b b = temp END SUBROUTINE swap
  • Java:
    • All arguments are passed by value
    • The following will NOT work:
      public void swap(double a, double b) { double temp; temp = a; a = b; b = temp; }
Input/Output
Back SMYC Forward
  • FORTRAN:
    • Involves READ and WRITE statements
    • Involves the FORMAT statement (and edit descriptors including I,F,E,ES,EN,D,G,L,A,H,T,TL,TR,X,P,BN,BZ,SP,SS,S,/,:,')
    • Example:
      READ(*,FMT=200) x,y WRITE(*,200) a,b 100 FORMAT(2I) 200 FORMAT(2F10.6)
  • Java:
    • Involves "streams" for byte-based I/O
    • Involves "readers" for character-based I/O
There's Always More to Learn
Back -