By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,635 Members | 2,085 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,635 IT Pros & Developers. It's quick & easy.

C++: "Type Checking"

P: n/a
Hi,

I come across the term "type checking" very often in my readings on C++, and
have never heard it in Java. Besides the simplistic answer that it checks
the "type", what more does it mean?
WD

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.504 / Virus Database: 302 - Release Date: 24/07/2003
Jul 19 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a

"Web Developer" <no****@hotmail.com> wrote in message
news:3f********@news.iprimus.com.au...
Hi,

I come across the term "type checking" very often in my readings on C++, and have never heard it in Java. Besides the simplistic answer that it checks
the "type", what more does it mean?
WD


I think its a question of timing. All languages check types, you aren't
allowed to multiply two strings in any language (AFAIK). But some languages
delay type checking until the program runs. C++ does most of its type
checking at compile type. This results in faster code, smaller data and
quicker detection of type errors, but it also means a more complex language.

We say C++ is a strongly typed language. In general I would say strong
typing is a good thing but there are occasions where you want to relax it
and check types at run time. That's where virtual functions and RTTI come
in.

john
Jul 19 '05 #2

P: n/a
On Mon, 11 Aug 2003 05:20:48 -0400, "ghl" <gl*******@comcast.net> wrote:
"John Harrison" <jo*************@hotmail.com> wrote in message
news:bh************@ID-196037.news.uni-berlin.de...

"Web Developer" <no****@hotmail.com> wrote in message
news:3f********@news.iprimus.com.au...
> Hi,
>
> I come across the term "type checking" very often in my readings on C++,

and
> have never heard it in Java. Besides the simplistic answer that itchecks > the "type", what more does it mean?
>
>
> WD
>


I think its a question of timing. All languages check types, you aren't
allowed to multiply two strings in any language (AFAIK). But some

languages
delay type checking until the program runs. C++ does most of its type
checking at compile type. This results in faster code, smaller data and
quicker detection of type errors, but it also means a more complex

language.

We say C++ is a strongly typed language. In general I would say strong
typing is a good thing but there are occasions where you want to relax it
and check types at run time. That's where virtual functions and RTTI come
in.


Java is also a strongly typed language and checks for compatible types for
method arguments and return values at compile time.


Nope. Consider classes A, B and C in separate files, where A uses B uses
C, and A is the "main" program. Compile all. Change C to be incompatible
with B. Compile class A using Sun's compiler: no errors reported. Run.
Runtime error.

That could not happen in C++.

C++ has _global_ static type checking; Java has runtime type checking.

Jul 19 '05 #3

P: n/a
On Mon, 11 Aug 2003 13:34:20 GMT, to********@hotmail.com (tom_usenet) wrote:
On Mon, 11 Aug 2003 10:23:27 GMT, al***@start.no (Alf P. Steinbach)
wrote:
Nope. Consider classes A, B and C in separate files, where A uses B uses
C, and A is the "main" program. Compile all. Change C to be incompatible
with B. Compile class A using Sun's compiler: no errors reported. Run.
Runtime error.

That could not happen in C++.

C++ has _global_ static type checking; Java has runtime type checking.
The Java language has stronger static type checking than C++. e.g.
implicit conversions from double to int, etc. don't exist. C++ however
has better idioms for static type checking, thanks to using generics
rather than an Object base-of-all class. Java encourages use of the
evil instanceof operator.

I think you're talking about the link time safety that C++ has thanks
to name mangling. But C++ will just crash if you replace a DLL with
another one with incompatible types, so I don't get your point.


DLLs are not part of C++.

Java will detect the problem if you recompile
Nope. Java will only detect the problem if you recompile class B.
You have an executable system without recompiling class B (and this is
not theoretical, it has been a major problem in some large Java projects).

just as happens with C++.


Nope. With C++ you have global type-checking. You cannot obtain an
executable with static type inconsistencies.

Jul 19 '05 #4

P: n/a
ghl
"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...
On Mon, 11 Aug 2003 05:20:48 -0400, "ghl" <gl*******@comcast.net> wrote:
"John Harrison" <jo*************@hotmail.com> wrote in message
news:bh************@ID-196037.news.uni-berlin.de...

"Web Developer" <no****@hotmail.com> wrote in message
news:3f********@news.iprimus.com.au...
> Hi,
>
> I come across the term "type checking" very often in my readings on C++, and
> have never heard it in Java. Besides the simplistic answer that it

checks
> the "type", what more does it mean?
>
>
> WD
>

I think its a question of timing. All languages check types, you aren't
allowed to multiply two strings in any language (AFAIK). But some

languages
delay type checking until the program runs. C++ does most of its type
checking at compile type. This results in faster code, smaller data and
quicker detection of type errors, but it also means a more complex

language.

We say C++ is a strongly typed language. In general I would say strong
typing is a good thing but there are occasions where you want to relax it and check types at run time. That's where virtual functions and RTTI come in.


Java is also a strongly typed language and checks for compatible types formethod arguments and return values at compile time.


Nope. Consider classes A, B and C in separate files, where A uses B uses
C, and A is the "main" program. Compile all. Change C to be incompatible
with B. Compile class A using Sun's compiler: no errors reported. Run.
Runtime error.


Wrong. Been there. Tried that.
Created classes as above, all returning String type reference variable.
Changed return type of C to int. Message on recompiling A gives:
..\C.java:3: incompatible types
found : java.lang.String
required: int
public int info = "I'm c";
^
..\C.java:13: incompatible types
found : int
required: java.lang.String
return info;
^
2 errors
--
Gary
Jul 19 '05 #5

P: n/a
ghl
"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...
On Mon, 11 Aug 2003 13:06:42 -0400, "ghl" <gl*******@comcast.net> wrote:
"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...
<<large snip>> "Change" means: change the source code of class C, compile class C.

Then compile class A, or not (it makes no difference), and run:
Exception in thread "main" java.lang.NoSuchMethodError: C.result()Ljava/lang/String; at B.result(B.java:5)
at A.main(A.java:5)
Got it?


Ah, I get what you're driving at. Yes, you get that. Does the same thing
happen in C++ with the linker detecting the change?
The Java error at runtime is detected because there is no linker and the
type checking can only occur when class C is actually loaded and checked.
I get your point.
--
Gary
Jul 19 '05 #6

P: n/a
On Mon, 11 Aug 2003 13:36:19 -0400, "ghl" <gl*******@comcast.net> wrote:
"Alf P. Steinbach" <al***@start.no> wrote in message
news:3f****************@News.CIS.DFN.DE...
On Mon, 11 Aug 2003 13:06:42 -0400, "ghl" <gl*******@comcast.net> wrote:
>"Alf P. Steinbach" <al***@start.no> wrote in message
>news:3f****************@News.CIS.DFN.DE...

<<large snip>>
"Change" means: change the source code of class C, compile class C.

Then compile class A, or not (it makes no difference), and run:
Exception in thread "main" java.lang.NoSuchMethodError:

C.result()Ljava/lang/String;
at B.result(B.java:5)
at A.main(A.java:5)
Got it?


Ah, I get what you're driving at. Yes, you get that. Does the same thing
happen in C++ with the linker detecting the change?


In practice the linker detects the change, yes.

In theory there need not be a linker in a C++ implementation, but the
_language_ (standard) guarantees global static type-checking whatever
execution system is actually used.

There's no runtime error because you don't get an executable.

Jul 19 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.