An Introduction to Java
for FORTRAN 90 Programmers |
Prof. David Bernstein |
Computer Science Department |
bernstdh@jmu.edu |
&
character
!
is a comment
(i.e., is ignored)
;
//
is a comment
as is everything betweem a /*
and */
{
and a }
POINTER
s and TARGET
s
Type |
INTEGER
|
REAL
|
CHARACTER
|
LOGICAL
|
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 |
REAL :: temperature, pressure
INTEGER :: month=1, year=2007
i,j,k,l,m,n
are implicitly integers)
double temperature, pressure;
int month=1, year=2007;
PARAMETER
attribute
REAL, PARAMETER :: pi=3.1415926
final
qualifier
final double pi=3.1415926;
+
)-
)*
)/
)**
)+
)-
)*
)/
)/
)%
)++
)--
)**
* /
+ -
++ --
* / %
+ -
.TRUE.
and .FALSE.
.NOT.
.AND.
, .OR.
, .EQV.
, .NEQV.
true
and false
!
&&
, ||
, ==
, !=
<
, <=
, >
, >=
, ==
, //=
<
, <=
, >
, >=
, ==
, !=
CHARACTER
type
CHARACTER :: gender='F'
char
type
char gender='F';
CHARACTER
type with
an additional len
CHARACTER(len=10) :: title='Prof.'
String
type
String title="Prof.";
////
operator
title(1:2)
returns 'P'
)
LEN(string)
, INDEX(substring, string)
and TRIM(string)
(e.g., LEN(title)
returns 10)
+
operator
title.substring(0,1);
returns "P"
)
length()
, indexOf(substring)
and trim()
(e.g., title.length()
returns 5)
(//
and //)
and are delimited by ,
(//1,5,9//)
{
and }
and are delimited by ,
{1,5,9}
INTEGER, DIMENSION(6) :: a
INTEGER, DIMENSION(4,2) :: b
REAL :: c(4:9)
int[] a;
a = new int[6];
int[][] b;
b = new int[4][2];
a(5)
b(1,1)
a[5]
b[1][1]
TYPE rectangle REAL :: height, width END TYPE rectangle
public class rectangle { public double height, width; }
%
operator
TYPE(rectangle) :: cage cage%height = 5 cage%width = 10
.
operator
Rectangle cage; cage = new Rectangle(); cage.height = 5; cage.width = 10;
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
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; }
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"
for (i=0; i<10; i++) { total = total + sales[i]; }
exit
can be used to "break out"
for
loop can be used like a DO
loop but can be used in many other ways as well
while (tolerance > 0.001) { approximation = approximation + 0.01; tolerance = target - approximation; }
i = 0; do { total = total + sales[i]; i = i + 1; } while (i < 10);
FUNCTION line(m, x, b) REAL :: line REAL :: m, x, b line = m*x + const END FUNCTION line
void
)
public double line(double m, double x, double b) { double result; result = m*x + b; return result; }
SUBROUTINE swap(a, b) REAL :: a, b, temp temp = a a = b b = temp END SUBROUTINE swap
public void swap(double a, double b) { double temp; temp = a; a = b; b = temp; }
READ
and WRITE
statements
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,/,:,'
)
READ(*,FMT=200) x,y WRITE(*,200) a,b 100 FORMAT(2I) 200 FORMAT(2F10.6)