Comparing Ada and Java (Under construction)

Ada Java
-- to compile and run foo.adb
gnatmake foo
foo
-- to compile and run foo.java
javac foo.java
java foo
NOT case sensitive Case sensitive
-- comment // comment
/* another kind of comment  */
/** a java doc comment */
Statements
assignment: i := 1; i = 1;
; at end of statements ; at end of statements
i := i + 1; i = i + 1;
i += 1; 
i++;    // postincrement
++i;    // preincrement
// i = i++;    // what does this do?
begin    -- or if or for or loop
   statement 1
   statement 2
end;      -- if or for or loop
{
     statement 1
     statement 2
}
if condition  then statements end if; if (condition)  statement
if condition  then statements
                        else statements
end if;
if (condition)  statement
else                   statement
if       condition  then statements
elsif  condition  then statements
else                                statements
end if;
if           (condition)  statement
else  if  (condition)  statement
else                              statement
No problem: unmatched if can not occur Unmatched else goes with closest unmatched if
for i in 1 .. 10 loop statements end loop for (i=1;  i<=10;  i++) statement
for i in 0 ..   9 loop statements end loop for (i=0;  i<  10;  i++) statement
while i < 10 loop statements end loop while (i<10)statement
exit;    -- leave a loop break;    // leave a loop
exit when condition; if   (condition) break;
case i of 
   when 3 => statement
   when 4 => statement
   when others => statement
end case;
switch (i){
    3: statement
          break
    4: statement
         break
   else: statement    // not required
}
i = getit;  -- parameterless function i = getit();  // All methods need parens
overloading considers return type overloading does not consider return type
null;     ; 
OPERATORS
operators can be overloaded operators can not be overloaded
arithmetic: +, -, *, /
relational: <, <=, >, >=
same
int/int returns int and float/float returns float
int/int gives int and float/float gives float
int/int truncates toward 0
Same
Relational: =, /= ==, !=
boolean: and, or, xor, not
short circuit: and then, or else
boolean: &, |,  ^, !
short circuit: &&, ||
Can compare arrays with rel ops Can not compare arrays with rel ops
div
mod
%
/
String concatenation: & +
with package all classes in classpath searched automatically
use MyPackage1 import MyPackage1.* // All classes in MyPackage1
import MyPackage2.Class ;  // Just this class
//  All classes in java.lang are
//    automatically imported
TYPES
Integer

Size not specified by language
Size specified by language
byte (8 bits)
short (16 bits)
int (32 bits)
long (64 bits)
Natural unsigned
float float (32 bits)
double (64 bits)
123_456_789 123456789
123L  // Long integer constant
character  -- 8 bit ascii
wide_character -- 16 bit unicode
char  // 16 bit unicode
String  -- predefined type String   // built in class
boolean: true, false boolean: true, false
subrange: 1 .. 10 none
enumerated: (red, blue) None: use integers 
type ... is ... Classes are the only new types
Float(i) (float) i
DECLARATIONS
i, j: Integer; int i, j;
i: Integer := 3;
j, k: Integer := 4; -- j is 4
int i=3;
int j, k = 4; // does not initialize j
c: constant integer := 5; final int c = 5;
a: array [0..9] of Integer; int a[] = new int[10];
int[] a = new int[10];  // either works
// all arrays begin at 0
// array values initialized to 0
a: array [0..2] of Integer := (5,6,7); int a[] = {5,6,7};
No others clause
Trailing comma allowed:
int a[] = {5,6,7,};
Array attributes:
'Length
'First
'Last
'Range
Array members:
.length()
0
.length()-1
no range
type MyArray is array [0..9] of Integer; No new types except classes
type MyRec is record 
    i: integer; 
    c: character;
end record;
R: MyRec;
class MyClass{
    int i;
    char c;

MyClass X = new MyRec();
 //  X is a pointer
Only get pointers when specified All class types are reference types
All declarations before begin Declarations allowed anywhere
declare  foo: integer;
begin   statements end;
{int i;
   statements
}
    i := j;   -- if j not initialized
                -- no compiler error
i = j;   //  if j not initialized
          //  compiler error
Scope: to end matched with following begin local vars: to first enclosing right brace
class members: entire class
nested scope can have variables with 
  same name as in outer scope
nested scope can not have variables with
  same name as outer scope (but class 
  members can be overridden and hidden)
Variables and objects have type
(which can be a classwide type)
Variables have type
Objects have class
FUNCTIONS, PROCEDURES METHODS
function foo(i, j: integer) return integer is
    statements 
end foo;
int foo(int i, int j){
   statements
}
procedure foo(i: integer) is void foo(int i){
procedure foo(i: out integer) is No out parameters in java, but
there are reference type
return required in function return required in method with 
   non-void return type
when calling a function,
return value can not be ignored
when calling a method,
return value can be ignored,
ie a function can be called as a procedure
nested routines allowed nested methods not allowed
EXCEPTIONS
begin
     statements1
exception
  when AnException => statements2
   when others => statements3
end
try{
   statements1
} catch (AnException  p){
   statements2
} catch (Exception e){
   statements3
}
int foo()  throws SomeException{
   statements   // without try/catch
}
OBJECT ORIENTED
Terminology:
parent type
extended derived types
Terminology:
supertype
subtypes
Only tagged types can be extended:
  type P is tagged record 
     fields
  end record;
type E is new P with record
   new fields
end record;
All types can be extended (ie inherited from):
class P{members}
class D extends P{new members}
single inheritance single inheritance
Can distinguish between 
    - parent type: P
    - parent and all extensions:  P'Class
Can not (directly) specify parent type 
separately from parent and all extensions
X in Person
X'Tag = Y'Tag
x instanceof Person
x.getClass() == y.getClass()
No root class in inheritance hierarchy Class Object is root class
MISC
File name must match name of
   package or procedure in file
File name must be lower case (in gnat)
File name must match name of  
   single public class in file
file name not case sensitive file name is case sensitive
access types reference types are similar
no garbage collection (in gnat) garbage collection
generic packages None. Can have collections of Objects, but
no type checking and casting required
on removal
packages and subpackages packages and classes are similar
separate specification interfaces are similar
privacy by groups privacy by member
PITFALLS
No check for undefined variable Definite assignment
while (); for ();
if or while ()
    statement 1
    statement 2
in parameters only
but remember reference types
CREDITS: idea and overall structure from P. Chew
Last modified on