Previous Topic Next topic Print topic


Data Types - in COBOL and Java

COBOL Java
*>Value Types
*>condition-value
*>binary-char (unsigned)
*>character
*>binary-short, binary-long, binary-double (unsigned)
*>float-short, float-long
*>decimal
*>DateTime (a framework type)

*>Reference types
*>object
*>string

*>Initializing
declare correct as condition-value = true
*> Can also infer variable type if the value has a well-defined type
declare incorrect = false  *> automatically a condition-value
declare b as byte = h"2a"  *> hex
declare o as byte = o"52"  *> octal
declare b2 as byte = b"101010" *> binary
declare person as object = null
declare nam as string = "Dwight"
declare grade as character = "B"
declare now as type DateTime = type DateTime::Now
*> No support for date/time literals
declare amount as decimal = 35.99
declare gpa as float-short = 2.9
declare pi as float-long = 3.14159265
declare lTotal as binary-double = 123456
declare sTotal as binary-short = 123
declare usTotal as binary-short unsigned = 123
declare uiTotal as binary-long = 123
declare ulTotal as binary-long unsigned = 123

*>Type Information
declare x as binary-long
display x::GetType            *> Prints System.Int32
display type of binary-long   *> Prints System.Int32
display x::GetType::Name      *> Prints Int32
*>Type Conversion
declare f as float-short = 3.5   *> automatic conversion
declare i = f as binary-long     *> set to 3 (truncates decimal)

end program.

program-id Legacy.
*> COBOL types not supported directly by other languages.
*> Visual COBOL supports these types on all platforms.
*> Only a few examples here
01 displayNumber pic 9(9).99.
01 computeNumber pic 9(9)V99.
01 alphaNumberic pic a(23).
01 binaryStorage pic x(12).
*> Also groups and redefines - a few examples
01 arecord.
   03 aSubRecord pic x(10).
   03 aUnion     pic 9(10) redefines aSubrecord.
end program.
import java.math.BigDecimal;
import java.time.LocalDateTime;

public class DataTypes
{
// Java doesn't have value types, though it does have
// "primitives" avaialble for the common types, which
// are similar in some respects
// boolean
// byte
// char
// short, int, long
// float, double
// BigDecimal   (a framework type)

    @SuppressWarnings("unused")
    public static void main(String[] args)
    {
        boolean correct = true;
        // no mechanism for automatic typing
        byte b = 0x2a; // hex
        byte o = 052; // octal
        byte b2 = 0b101010; // binary (Java 7 onwards)
        Object person = null;
        String nam = "Dwight";
        char grade = 'B';
        LocalDateTime now = LocalDateTime.now();
        // No support for date/time literals
        BigDecimal amount = new BigDecimal(35.99);
        float gpa = 2.9f;
        double pi = 3.14159265;
        long lTotal = 123456;
        short sTotal = 123;

        // Java has no unsigned integer types

        // Java reflection does not work on primitive types.
        Integer x = new Integer(0);
        System.out.println(x.getClass().getCanonicalName());

        // Type conversion
        short f = (short) 3.5 ; // must use cast to convert
        int i = f; // set to 3 (truncation)
    }

}

Portions of these examples were produced by Dr. Frank McCown, Harding University Computer Science Dept, and are licensed under a Creative Commons License.

Previous Topic Next topic Print topic