comp.lang.ada
 help / color / mirror / Atom feed
From: nabbasi@pacbell.net
Subject: Ada and Java. different behaviour. casting long to int problem.
Date: 1999/06/12
Date: 1999-06-12T00:00:00+00:00	[thread overview]
Message-ID: <7jt2c0$vrb@drn.newsguy.com> (raw)


This below shows that Ada detected at compile time a basic problem
that the Java compiler missed. (this might be specific problem
with the Java compiler I am using), it also could be a 
mis-understanding on my part of the Java semantics when it 
comes to this, but the bottom line is that Java produced, 
what for me, can only be a wrong result.

Please do not make this thread a language-war, (I happen to
like both Java and Ada), but I am really only interested in 
someone explaining the justification behind this Java behavior.

In the example, I cast a long integer literal, outside the range
of an integer, into an integer variable.  Java allows this,
and produces the wrong result. Ada compiler detect this as an error
at compile time, and it also detects the same thing at run-time.

fyi: a 64 bit signed long range is -9223372036854775808 .. 9223372036854775807 
and an 32 bit signed integer range is  -2147483648 .. 2147483647

In the example, I cast the literal 3200000000, which is clearly outside
the range of an integer, into an integer. I expect the compiler or
the run-time, to detect this. Ada does, Java did not. Is this by 'design'
on the part of Java? i.e. is this how Java supposed to behave? becuase
this can result in very wrong calculations if not detected.

env:
----
OS: linux 2.0.36
Java compiler: free JDK from http://www.blackdown.org 1.2, also 1.1.7B
Ada compiler : free ada95 GNAT compiler from http://www.gnat.com, version 3.11p

---------------- java example 1-----------------------------
class test_cast
{
   public static void main(String args[])
   {
      long n= 3200000000L;
      int  m= (int) n;
      System.err.println("long n =" + n + " int m=" + m);
   }
}

$javac  ./test_cast.java      <-- compile OK

$java test_cast               <--- runs OK and produces 'wrong' result.
long n =3200000000 int m=-1094967296
$
----------------------------- Ada example 2 -----------------
with Ada.Text_Io;         USE Ada.Text_Io;

procedure Test_Cast is
   n : Long_Integer := 3200000000;
   m : Integer      := Integer (N);
begin
      Put_Line (   "long n ="  &  Long_integer'Image(N)
                 & "int  m ="  &  Integer'Image(M) );
end Test_Cast;

$gnatmake test_cast.adb
gnatgcc -c test_cast.adb
test_cast.adb:5:24: value not in range of type "Standard.Long_Integer"
test_cast.adb:5:24: static expression raises "Constraint_Error"
gnatmake: "test_cast.adb" compilation error
$

--------------------------------------------------------------------

Now, lets try the same idea as above, but do not use a literal
number (so to not allow the ada compiler the chance to detect this
at compile time) and try to do the same at run-time.


------------------- ada example 2 -----------------------------

with Ada.Text_Io;         USE Ada.Text_Io;

procedure Test_Cast is
   N  : Long_Integer;
   M  : Integer;
   package Long_Int_Io  is new Ada.Text_Io.Integer_Io(Long_Integer);
begin
   Long_Int_Io.Get(N);
   M := Integer (N);

   Put_Line (   "long n ="  &  Long_integer'Image(N)
              & "int  m ="  &  Integer'Image(M) );
end Test_Cast;

$gnatmake -gnatf test_cast.adb    <-- compile OK
gnatgcc -c -gnatf test_cast.adb
gnatbind -x test_cast.ali
gnatlink test_cast.ali

$./test_cast                  <--- run
3200000000

raised ADA.IO_EXCEPTIONS.DATA_ERROR   <-- exception raised
-------------------------------------------------------------


comments?

Nasser





             reply	other threads:[~1999-06-12  0:00 UTC|newest]

Thread overview: 120+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
1999-06-12  0:00 nabbasi [this message]
1999-06-12  0:00 ` Ada and Java. different behaviour. casting long to int problem Tucker Taft
1999-06-12  0:00   ` PPAATT
1999-06-12  0:00   ` Keith Thompson
1999-06-12  0:00     ` kirck
1999-06-13  0:00       ` Robert Dewar
1999-06-12  0:00         ` Fred
1999-06-14  0:00           ` Mark Hood
1999-06-15  0:00             ` mike
1999-06-15  0:00               ` Samuel Mize
1999-06-15  0:00                 ` jerry
1999-06-16  0:00                   ` Richard D Riehle
1999-06-16  0:00                     ` jerry
1999-06-15  0:00               ` D'Arcy Smith
1999-06-16  0:00                 ` George W. Bayles
1999-06-16  0:00                   ` D'Arcy Smith
1999-06-17  0:00                   ` Aidan Skinner
1999-06-17  0:00                   ` Matthew Heaney
1999-06-15  0:00               ` Marin David Condic
1999-06-15  0:00                 ` Mike Silva
1999-06-15  0:00                   ` rich
1999-06-15  0:00                     ` Marin David Condic
1999-06-15  0:00                       ` D'Arcy Smith
1999-06-15  0:00                         ` Keith Thompson
1999-06-16  0:00                           ` D'Arcy Smith
1999-06-16  0:00                           ` bill
1999-06-16  0:00                             ` George W. Bayles
1999-06-16  0:00                               ` Fraser Wilson
1999-06-17  0:00                               ` Chris Dollin
1999-06-17  0:00                               ` Aidan Skinner
1999-06-17  0:00                                 ` David Botton
1999-06-18  0:00                                   ` Dale Stanbrough
1999-06-18  0:00                                     ` David Botton
1999-06-18  0:00                                       ` Pascal Obry
1999-06-18  0:00                                     ` Matthew Heaney
1999-07-20  0:00                             ` Geoff Bull
1999-06-16  0:00                         ` Marin David Condic
1999-06-16  0:00                         ` Mike Silva
1999-06-16  0:00                           ` D'Arcy Smith
1999-06-16  0:00                             ` kirk
1999-06-16  0:00                               ` D'Arcy Smith
1999-06-17  0:00                                 ` Markus Kuhn
1999-06-17  0:00                                   ` john
1999-06-17  0:00                                     ` Ed Falis
1999-06-18  0:00                                     ` Aidan Skinner
1999-06-17  0:00                                   ` D'Arcy Smith
1999-06-16  0:00                               ` Hyman Rosen
1999-06-17  0:00                                 ` Robert I. Eachus
1999-06-17  0:00                                   ` Hyman Rosen
1999-06-17  0:00                                     ` Marin David Condic
1999-06-17  0:00                                     ` bob
1999-06-18  0:00                                       ` Hyman Rosen
1999-06-18  0:00                                         ` mike
1999-06-18  0:00                                           ` Hyman Rosen
1999-06-19  0:00                                             ` Dale Stanbrough
1999-06-21  0:00                                               ` Marin David Condic
1999-06-19  0:00                                             ` Samuel Mize
1999-06-21  0:00                                               ` Marin David Condic
1999-06-21  0:00                                             ` Mike Silva
1999-06-17  0:00                                 ` Jean-Pierre Rosen
1999-06-17  0:00                                   ` Marin David Condic
1999-06-17  0:00                                     ` Samuel Mize
1999-06-17  0:00                                       ` Marin David Condic
1999-06-22  0:00                                         ` Hyman Rosen
1999-06-22  0:00                                           ` Keith Thompson
1999-06-23  0:00                                             ` Marin David Condic
1999-06-24  0:00                                               ` Robert A Duff
1999-06-24  0:00                                                 ` Marin David Condic
1999-06-23  0:00                                           ` Marin David Condic
1999-06-18  0:00                                       ` Aidan Skinner
1999-06-17  0:00                                 ` Markus Kuhn
1999-06-20  0:00                                 ` Sera Hirasuna
1999-06-19  0:00                                   ` Kio
1999-06-20  0:00                                   ` Vladimir Olensky
1999-06-21  0:00                                   ` Samuel T. Harris
1999-06-22  0:00                                     ` Robert I. Eachus
1999-06-23  0:00                                       ` Aidan Skinner
1999-06-23  0:00                                       ` Richard D Riehle
1999-06-22  0:00                                     ` Richard D Riehle
1999-06-21  0:00                                   ` Hyman Rosen
1999-06-17  0:00                           ` Jean-Pierre Rosen
1999-06-16  0:00                         ` George W. Bayles
1999-06-16  0:00                           ` D'Arcy Smith
1999-06-16  0:00                           ` Tucker Taft
1999-06-17  0:00                             ` George W. Bayles
1999-06-17  0:00                               ` Tucker Taft
1999-06-17  0:00                                 ` bob
1999-06-16  0:00                           ` D'Arcy Smith
1999-06-17  0:00                           ` Larry Kilgallen
1999-06-22  0:00                       ` Robert Dewar
1999-06-23  0:00                         ` Marin David Condic
1999-06-23  0:00                           ` Vladimir Olensky
1999-06-23  0:00                             ` Roedy Green
1999-06-23  0:00                               ` Marin David Condic
1999-06-23  0:00                                 ` Keith Thompson
1999-06-24  0:00                                   ` Marin David Condic
1999-06-24  0:00                                   ` Mike Silva
1999-06-23  0:00                             ` Marin David Condic
1999-06-15  0:00                     ` tmoran
1999-06-15  0:00                       ` David Botton
1999-06-16  0:00                       ` Richard D Riehle
1999-06-16  0:00                       ` Samuel Mize
1999-06-15  0:00                     ` Samuel Mize
1999-06-16  0:00                 ` Mark Hood
1999-06-17  0:00                   ` Jean-Pierre Rosen
1999-06-17  0:00                 ` Robert I. Eachus
1999-06-17  0:00                   ` Marin David Condic
1999-06-17  0:00             ` Markus Kuhn
1999-06-17  0:00               ` David Botton
1999-06-13  0:00   ` Robert Dewar
1999-06-14  0:00     ` tmoran
1999-06-30  0:00       ` John Merryweather Cooper
1999-07-01  0:00         ` Chad R. Meiners
1999-07-02  0:00           ` Robert Dewar
1999-07-02  0:00             ` John Merryweather Cooper
1999-07-03  0:00               ` Robert Dewar
1999-06-12  0:00 ` nabbasi
1999-06-12  0:00   ` jerry
1999-06-12  0:00     ` Robert Dewar
1999-06-14  0:00       ` Marin David Condic
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox