| COBOL | Java | 
|---|---|
| *> Comparison operators: *> = < > <= >= <> display (1 = 1) *> true display (1 > 2) *> false display (1 < 2) *> true display (1 <= 1) *> true display (1 >= 2) *> false display (0 <> 1) *> true *> Arithmetic operators, *> * / + - ** display ( (5 + 1) * (5 - 1) / 2 ** 2 ) *> result 6 display (function mod (5 3 )) *> result 2 *> Local variable declarations declare a, b, c as binary-long declare d = "I'm a string" *> string type inferred from value *> Assignment statements *> move, set, compute set a = 1 move 2 to b compute c = 3 * 6 set a = 3 * 6 *> Type operator declare mystr as string = "string" declare myobj as object = mystr display (myobj instance of string) *> true *> Bitwise *> b-and, b-or, b-xor, b-not, b-left, b-right display (4 b-and 3) display (4 b-or 3) display (b-not 3) display (4 b-xor 3) display (4 b-left 1) *> shift left one place display (4 b-right 2) *> shift right two places *> Logical *> and, or, not display (true and false) *> false display (true or false) *> true display (not true) *> false | public class operators
{   
    
    public static void main(String[] args)
    {
        // Comparison operators 
        // = < > <= >= !=
        System.out.println(1 == 1); // true
        
        System.out.println(1 > 2); // false
        
        System.out.println(1 < 2); // true
        
        System.out.println(1 <= 1); // true
        
        System.out.println(1 >= 2); // false
        
        System.out.println(0 != 1); // true
        
        // Arithmetic operators
        // * / + - 
        // No exponentiation operator - use static method Math.pow(); 
        System.out.println( (5 + 1) * (5 - 1) ); // result 24
        System.out.println( Math.pow(2, 3) ); // result 8
        System.out.println( 5 % 3 ); // result 2
        // Local variable declarations.
        int a, b, c;
        String d = "I'm a string" ; // no declaration type inference 
        
        // Assignment statements
        // all have same format in Java
        a = 1 ;
        b = 2; 
        c = 3 * 6; 
        a = 3 * 6; 
        
        // type operator
        String mystr = "string"; 
        Object o = mystr; 
        System.out.println(o instanceof String); // true
        
        //Bitwise operations
        // & | ^ ~  << >> 
        System.out.println (4 & 3);  
        System.out.println (4 | 3);  
        System.out.println (~3); 
        System.out.println (4 ^ 3);  
        System.out.println (4 << 1);  // shift left one place
        System.out.println (4 >> 2);  // shift right two places
       // Logical
       // && || ! 
        System.out.println (true && false); // false
        System.out.println (true || false); // true
        System.out.println (!true);  // false
    }
} | 
Portions of these examples were produced by Dr. Frank McCown, Harding University Computer Science Dept, and are licensed under a Creative Commons License.