skip to main content

Converting Java Source Code to Object-oriented COBOL : Known cases of Java syntax that is not directly translatable to COBOL : The instanceof operator

The instanceof operator
The instanceof keyword is a binary operator used to test if an object (instance) is a subtype of a given type.
There is no equivalent statement in COBOL, but you can rely on the isAssignableFrom() method exposed by java.lang.Class.
The following Java source
import java.io.OutputStream;
import java.io.FileOutputStream;
public class InstOfTest {
    public static void main (String[] args) {
            FileOutputStream f = new FileOutputStream("/tmp/foo");
            if (f instanceof OutputStream) {
                System.out.println("The f object is an OutputStream");
        } catch (Exception e){
            e.printStackTrace();
can be translated to
       program-id. InstOfTest.
       configuration section.
       repository.
           class OutputStream      as "java.io.OutputStream"
           class FileOutputStream  as "java.io.FileOutputStream"
           class JClass            as "java.lang.Class"
           class JSystem           as "java.lang.System"
       working-storage section.
       77 f object reference FileOutputStream.
       procedure division.
              set f to FileOutputStream:>new("/tmp/foo")
              if JClass:>forName("java.io.OutputStream")
                       :>isAssignableFrom (f:>getClass())
                     ("The f object is an OutputStream")
           catch exception

Copyright (c) 2017 Veryant
Contact us
Please share your comments on this manual or on any
Veryant product documentation with the email button at the top left