COBOL/Java Translation

by Ron Zucker

type

COBOL

Java

Sequential

move c to a, [b,...]

a=[b=...]c;

Sequential

add b to a
subtract b from a
multiply b by a
divide b into a
divide b into a giving c remainder d

a=a+b; or a+=b;
a=a-b; or a-=b;
a=a*b; or a*=b;
a=a/b; or a/=b;
c=a/b;
d=a%b;

Sequential

add b to a giving c
subtract a from b giving c
multiply b by a giving c

c=b+a;
c=b-a;
c=b*a;

Sequential

add 1 to a
subtract 1 from a

++a; or a++;
--a; or a--;

Selection

if cond
true stuff
[else
false stuff]
endif

if (cond)
{true stuff}
[else
{false stuff}]
or
cond? true stuff: false stuff;

Selection

evaluate a
when val1 cond1
when val2 cond2
when other defaultcond
end-evaluate

switch (a)
{
case val1: cond1; break;
case val2: cond2; break;
default: defaultcond; break;
}

Repetition, counting

perform varying a from start by inc until cond
loop stuff
end-perform
Note: loops while cond is false

for (a=start;cond;a+=inc)
{
loop stuff
}
Note: loops while cond is true

Repetition,
conditional

perform [test before] until cond
loop stuff
end-perform

while (cond)
{
loop stuff
}

Repetition,
conditional

perform test after until cond
loop stuff
end-perform

do
{
loop stuff
} while (cond);

I/O keyboard

accept a

BufferedReader input=new BufferedReader(new InputStreamReader(System.in));
a= input.readLine();
For JDK1.02 users only:
DataInputStream input=new DataInputStream(new BufferedInputStream(System.in));
a= input.readLine();

I/O screen

display a

System.out.println(a);

I/O file

open input filea

BufferedReader filea=null;
try
{
filea= new BufferedReader(new FileReader("extfile.dat"));
}
catch (Exception e)
{
error handler for invalid open
}
For JDK 1.02 Users Only:
DataInputStream input = new DataInputStream(new FileInputStream("file.ext"));

I/O file

read filea into recorda

recorda=filea.readLine();

I/O file

open output filea

filea=new DataOutputStream(new FileOutputStream("extfile.dat"));

I/O file

write recorda

filea.writeBytes(recorda+"\r\n");

I/O file

close filea

try
{
filea.flush();
filea.close();
}
catch (IOException e)
{
error handler for invalid close
}

Array

05 table occurs n times.





Note: subscripts are in range 1 to n

datatype [ ] table;
table = new datatype[n]
or
datatype [ ] table= new datatype[n];
also may be initialized, for example
datatype [ ] table={val1,val2,val3...};
Note: subscripts are in range 0 to n-1

Comments

asterisk(*) in column seven

// single line comment
/*
multiline comments
*/
/**
javadoc multiline comments
*/



Relational Operators

The operators are used in conditions referenced by cond above

COBOL

Java

equal to
=

= =
stringtype.equals(otherstring)

not equal
<>

!=
!stringtype.equals(otherstring)

greater than

>
stringtype.compareTo(otherstring)>0

greater than or equal

>=

less than

<
stringtype.compareTo(otherstring)<0

less than or equal

<=

Boolean Operators

The operators are used in conditions referenced by cond above and are listed in Java precedence order

COBOL

Java

AND

&

Note: exclusive or (XOR) is not provided in COBOL but may be given as:
(x and not y) or (not x and y)

^

OR

|

AND

&& (short circuit AND, if left side evaluates false right side is not evaluated)

OR

|| (short circuit OR, if left side evaluates true right side is not evaluated)



Java Precedence Table

Prec

Operator

Order

Comments

1

++,--,
+,-,
~
!
(typecast)

R to L

~ is bitwise complement
(typecast) required to demote a variable (used when precision will be lost)

2

*,/,%

L to R

% is mod

3

+,-

L to R

+ may also be used for string concatenation

4

<<,>>,>>>

L to R

>>> is right shift with zero fill, while >> is right shift with sign propogation

5

<.<=
>,>=
instanceof

L to R

instanceof returns true if the object is an instance of the a class or subclass of the object

6

= =
!=

L to R

boolean equality and not equal

7

&

L to R

& maybe a bitwise AND or a boolean value

8

^

L to R

^ maybe a bitwise XOR or a boolean value

9

|

L to R

| maybe a bitwise OR or a boolean value

10

&&

L to R

always a boolean

11

||

L to R

always a boolean

12

?:

R to L

always a boolean

13

=,
*=, /=,%=,
+=,-=,
<<=,>>=,>>>=,
&=,^=,|=

R to L

assignment operators



Java Escape Sequences

Sequence

Char

 

\b

BS

Backspace

\f

FF

Formfeed

\n

LF

Newline

\r

CR

carriage Return

\\

\

backslash

\'

'

single quote

\"

"

double quote

\?

?

question mark


Additions, corrections, comments please email to rzucker@unf.edu(click to send mail)