Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Strong/static typing in Java and C/C++

Reply
Thread Tools

Strong/static typing in Java and C/C++

 
 
Digital Puer
Guest
Posts: n/a
 
      08-31-2003
I'm a bit confused by the terms {strong vs. weak} typing and
{static vs. dynamic} typing. Are they orthogonal terms? ie.
are there 4 combinations of these types of typing?

From my understanding, static typing means that a variable's type
is always known at compile-time and that the compiler checks for
improper operations on a type at compile-time.

Also, AFAICT, strong typing means that improper operations on a type
are not allowed to happen. Does strong typing occur at compile time
or runtime? If a language is statically typed, thereby preventing
illegal type operations at compile time, would you even need
strong typing at runtime?

I've read that C and C++ are statically typed but not strongly
typed. I've also read that Java is statically and strongly typed.
Is this correct?

Suppose I have a simple program:
int i;
double j;
int result;

i = 2;
j = 3.0;

result = i+j;


This will compile in C/C++ (albeit with a warning). This will not
even compile in Java. What does this show about each language's
type-ness? Thank you for any help.
 
Reply With Quote
 
 
 
 
James Rogers
Guest
Posts: n/a
 
      08-31-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Digital Puer) wrote in
news:(E-Mail Removed) om:

> I'm a bit confused by the terms {strong vs. weak} typing and
> {static vs. dynamic} typing. Are they orthogonal terms? ie.
> are there 4 combinations of these types of typing?


Yes.

>
> From my understanding, static typing means that a variable's type
> is always known at compile-time and that the compiler checks for
> improper operations on a type at compile-time.


Static typing means that the type is fixed when the variable is
declared. It does not specifically imply any set of correctness
tests.

Dynamic typing means a variable has no fixed type. In fact, the
variable may have no explicit declaration expression. It may
contain any type.

>
> Also, AFAICT, strong typing means that improper operations on a type
> are not allowed to happen. Does strong typing occur at compile time
> or runtime? If a language is statically typed, thereby preventing
> illegal type operations at compile time, would you even need
> strong typing at runtime?


Strong typing does mean a separation of operations on types.
Strong statically typed languages can perform some checks at
compile time. Some checks can be performed at run time.
Strong dynamically typed languages are only checked at run time.

>
> I've read that C and C++ are statically typed but not strongly
> typed. I've also read that Java is statically and strongly typed.
> Is this correct?


Strong typing is not a binary state. There are degrees of strong
typing. C has some elements of strong typing. C++ has more elements
of strong typing (mostly related to the use of classes). Java
claims to be strongly typed, but is not much different from C++.

One of the most strongly typed languages is Ada.

>
> Suppose I have a simple program:
> int i;
> double j;
> int result;
>
> i = 2;
> j = 3.0;
>
> result = i+j;
>
>
> This will compile in C/C++ (albeit with a warning). This will not
> even compile in Java. What does this show about each language's
> type-ness? Thank you for any help.
>


The warning in C and C++ indicates that you are attempting something
that is not correct. Java compilers provide a stronger error message.
Both languages retain some of the weak typing features of C relating
to implicit conversions between numeric types. As an example I will
slightly modify your code above. The following example will compile
without warning or error in C, C++, and Java:

int i;
float j;
double result;

i = 2;
j = 3.0;

result = i + j;

All three languages allow you to mix two different types to create
a value of a third type. By comparison, such mixing cannot be done
implicitly using Ada.

i : integer;
j : float;
result : long_float;

i := 2;
j := 3.0;

result := i + j; <- Compiler will issue error message here

The GNAT Ada compiler complains that the operator "+" has improper
operands.

If the example is changed to:

i : float;
j : float;
result : long_float;

i := 2.0;
j := 3.0;

result := i+j;

The same GNAT Ada compiler complains that the right hand side
of the argument expected type long_float but found type float.

Jim Rogers

 
Reply With Quote
 
 
 
 
Christopher Barber
Guest
Posts: n/a
 
      09-02-2003
James Rogers <(E-Mail Removed)> writes:

> > From my understanding, static typing means that a variable's type
> > is always known at compile-time and that the compiler checks for
> > improper operations on a type at compile-time.

>
> Static typing means that the type is fixed when the variable is
> declared.


Not quite. It means that declarations have a static type, known at compile
time. The variable's exact type does not need to be identical to the static
type at runtime, but it must be a subtype.

> Dynamic typing means a variable has no fixed type. In fact, the
> variable may have no explicit declaration expression. It may
> contain any type.


This is only true for languages which do not mix dynamic and static typing.

 
Reply With Quote
 
Peter Ammon
Guest
Posts: n/a
 
      09-02-2003
Digital Puer wrote:

> I'm a bit confused by the terms {strong vs. weak} typing and
> {static vs. dynamic} typing. Are they orthogonal terms? ie.
> are there 4 combinations of these types of typing?


Yes. Note that I seem to have a different understanding than some other
posters.

>
> From my understanding, static typing means that a variable's type
> is always known at compile-time


Yes.

> and that the compiler checks for
> improper operations on a type at compile-time.


No. There's yet another distinction: a language can be type safe or
type unsafe (or somewhere in between). In a type safe language, type
errors (performing an operation that the object does not support) are
impossible. They may be blocked at compile time, which is typical but
not required of statically typed languages, or they may be blocked at
runtime. Java uses a mixed approach, while C and C++ have no runtime
checking. As a result, Java is type safe while C and C++ are not.

>
> Also, AFAICT, strong typing means that improper operations on a type
> are not allowed to happen.


No, that is type safe. Strong typing means that every expression has a
well defined type.

> Does strong typing occur at compile time
> or runtime?


Yes. (It can be either.)

> If a language is statically typed, thereby preventing
> illegal type operations at compile time,


Thus you mean statically typed *and* typesafe.

> would you even need
> strong typing at runtime?


No (except possibly for debugging purposes), and this is a strong
advantage of statically typed languages. However, there are reasons to
want to avoid statically typed languages; see below.

>
> I've read that C and C++ are statically typed but not strongly
> typed.


This is wrong. C and C++ are strongly typed because every expression
has a well defined type (or has type void, which is technically an
incomplete type and so counts). They are statically typed because the
types are known at compile time, which is why sizeof can do what it does.

C++ has some (weak) dynamic typing features, in addition.

> I've also read that Java is statically and strongly typed.
> Is this correct?


Yes, for the same reason. It also has dynamic typing features.

>
> Suppose I have a simple program:
> int i;
> double j;
> int result;
>
> i = 2;
> j = 3.0;
>
> result = i+j;
>
>
> This will compile in C/C++ (albeit with a warning). This will not
> even compile in Java. What does this show about each language's
> type-ness?


This demonstrates two things.

In Java the binary + operator is very heavily overloaded. It can accept
the following pairs of types.

int, int
long, long
float, float
double, double

But that's just for arithmetic. When you add in the String type, things
get much uglier. In C, it's even worse; the rules are very complicated
and I won't try to give them all here. (C++ is a downright Gordian knot
in this regard.) Overloading of operators is not strictly related to
type checking or type safety, but languages that place a large emphasis
on static type safety prefer to not overload operators (e.g. Ada or ML).

It also shows that, in both languages, there is the possibility for
implicit type conversions. In C, C++, and Java, integral types can be
implicitly converted to either float or double, and that is what is
happening here: the value of i is promoted to double, and then the
double, double form of the + operator is invoked.

In Java, however, there is no implicit type conversion from double to
int, and that is why it fails to compile. C and C++ do allow implicit
type conversions (that is, without a typecast) from double to int, which
is why the code compiles.

Implicit type conversions weaken the strong-typing aspect of a language:
the more implicit type conversions, the weaker the typing. They do not
affect type safety. So in this regard, Java is more strongly typed than
C or C++.

> Thank you for any help.


To be very sure:

In a statically typed language, the types are known at compile time.
In a dynamically typed language, the types are discovered at runtime.

In a strongly typed language, every expression has a well defined type.
In a weakly typed language, the types of expressions are undefined, or
malleable, or determined by context, etc..
Languages can also be totally untyped. Assembly is an example.

In a type safe language, operations that a type does not support are
impossible.
In a type unsafe language, operations that a type does not support are
possible.

A few examples:

SML is strongly typed, statically typed, and type safe.
C is strongly typed, statically typed, and type unsafe.

Smalltalk is strongly typed, dynamically typed, and type safe.
____ is strongly typed, dynamically typed, and type unsafe. (I can't
think of an example, since dynamically typed languages are typically
type safe).

Perl is weakly typed, dynamically typed, and type safe.
Assembly is untyped and type unsafe.

And a few weirdos:

Objective-C is weakly typed (expressions can have a well defined compile
time type, but this type is permitted to be wrong!), has *optional*
static typing, and is typesafe.
Dylan has *optional* strong typing, *optional* static typing, and is
type safe.

As an interesting factoid, type checking is related to the Halting
Problem, and it's known that no static type checker can determine
exactly which programs will produce a type error: they will either
reject some correct ones or admit some illegal ones. Dynamic type
checking does not have that problem.

I'm sure it's much clearer now.

-Peter

 
Reply With Quote
 
Peter Ammon
Guest
Posts: n/a
 
      09-02-2003
Peter Ammon wrote:

>
> Objective-C is weakly typed (expressions can have a well defined compile
> time type, but this type is permitted to be wrong!), has *optional*
> static typing, and is typesafe.
> Dylan has *optional* strong typing, *optional* static typing, and is
> type safe.
>


Sorry, I got entangled in my own definitions. Both Objective-C and
Dylan should be considered strongly typed, as every expression has a
well defined type and there are not type conversions; whether the type
is known at compile time or runtime is orthogonal.

-Peter

 
Reply With Quote
 
Digital Puer
Guest
Posts: n/a
 
      09-03-2003
Thank you for writing such a detailed write-up. I'll have to read
it a few more times to digest all of it, though.

Have you considered putting this in a FAQ or webpage on this subject?
I'm sure others will find it helpful.
 
Reply With Quote
 
Gary Labowitz
Guest
Posts: n/a
 
      09-03-2003
"Peter Ammon" <(E-Mail Removed)> wrote in message
news:bj33au$q5s$(E-Mail Removed)...
> Digital Puer wrote:

<<snip of very nice post>>
> It also shows that, in both languages, there is the possibility for
> implicit type conversions. In C, C++, and Java, integral types can be
> implicitly converted to either float or double, and that is what is

^^^^^^^
> happening here: the value of i is promoted to double, and then the

^^^^^^^
> double, double form of the + operator is invoked.


Here I have a "quibble" question (say five times fast).
You appear to use convert and promote interchangeably. Are they?
I have always taught conversion implies changing the internal coding
structure (as in double converts to int) and promotion implies extending the
range of values available (as in int promotes to long). Promotion is the
opposite of truncation with this idea. There is no opposite of conversion.
What say you?
--
Gary


 
Reply With Quote
 
Roedy Green
Guest
Posts: n/a
 
      09-03-2003
On Wed, 3 Sep 2003 09:48:56 -0400, "Gary Labowitz"
<(E-Mail Removed)> wrote or quoted :

>You appear to use convert and promote interchangeably. Are they?
>I have always taught conversion implies changing the internal coding
>structure (as in double converts to int) and promotion implies extending the
>range of values available (as in int promotes to long). Promotion is the
>opposite of truncation with this idea. There is no opposite of conversion.


Conversion requires something explicit to make it happen, where
promotion is implicit.


--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
Peter Ammon
Guest
Posts: n/a
 
      09-03-2003
Gary Labowitz wrote:
> "Peter Ammon" <(E-Mail Removed)> wrote in message
> news:bj33au$q5s$(E-Mail Removed)...
>
>>Digital Puer wrote:

>
> <<snip of very nice post>>
>
>>It also shows that, in both languages, there is the possibility for
>>implicit type conversions. In C, C++, and Java, integral types can be
>>implicitly converted to either float or double, and that is what is

>
> ^^^^^^^
>
>>happening here: the value of i is promoted to double, and then the

>
> ^^^^^^^
>
>>double, double form of the + operator is invoked.

>
>
> Here I have a "quibble" question (say five times fast).
> You appear to use convert and promote interchangeably. Are they?


A conversion is any change from one type to another, and a promotion is
a particular sort of conversion among primitive types. It happens with
some binary operators so that they can operate on two of the same type,
which keeps the language simpler. It also occurs with some unary
operators, I suspect because that's how processors work.

> I have always taught conversion implies changing the internal coding
> structure (as in double converts to int)


Well, a cast from String to Object is a conversion, but no internal
structures need be changed; in fact, nothing needs to be done at runtime
at all.

> and promotion implies extending the
> range of values available (as in int promotes to long).


This is my understanding too. But note that a promotion can also change
the internal structure. For example, with 3.5 == 5, the value 5, an
int, is promoted to the type of 3.5, which is a double. Converting an
int to double requires bit fiddling and internal structure changing.

> Promotion is the
> opposite of truncation with this idea. There is no opposite of conversion.
> What say you?


Agreed!

-Peter

 
Reply With Quote
 
Peter Ammon
Guest
Posts: n/a
 
      09-03-2003
Roedy Green wrote:
> On Wed, 3 Sep 2003 09:48:56 -0400, "Gary Labowitz"
> <(E-Mail Removed)> wrote or quoted :
>
>
>>You appear to use convert and promote interchangeably. Are they?
>>I have always taught conversion implies changing the internal coding
>>structure (as in double converts to int) and promotion implies extending the
>>range of values available (as in int promotes to long). Promotion is the
>>opposite of truncation with this idea. There is no opposite of conversion.

>
>
> Conversion requires something explicit to make it happen, where
> promotion is implicit.


No, I don't think that's quite right. For example, in

"My age is " + 139;

The particular conversion is from the int 139 to the String "139", which
is classed under string conversion (not a promotion). But in

125.7 + 139;

we have a conversion from the int 139 to the double of the same value,
and that is classed under numeric promotion. I would argue that both
examples are equally explicit, but promotion only occurs in the second.

-Peter

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Typing to Nullable (Of Date) vs typing to Date JimLad ASP .Net 0 01-26-2010 07:54 PM
typing location changes when I am typing. Ed Computer Support 5 11-11-2006 12:51 AM
Static Typing Where Possible and Dynamic Typing When Needed vladare Ruby 0 07-11-2005 11:54 AM
MSWord and typing in English and French. Toolman Tim Computer Support 7 11-21-2004 02:49 PM
How to run Java program without typing class name from DOS command line? tony lincoln Java 1 07-15-2003 01:01 PM



Advertisments