On Java and C++

T

The Ghost In The Machine

In comp.lang.java.advocacy, Remon van Vliet
<[email protected]>
wrote
Martin Vejnár said:
The said:
[5] Java doesn't have explicit deletes. The garbage
collection is expected to take care of things. (There are
some exceptions; a FileOutputStream for example will be
left open indefinitely, even if the reference is lost.)

For some reason, you've put the most important statement in parentheses.
RAII is one of the two reasons I stick with C++. I don't know of any other
language that would support such concept. (C# and D both support RAII, but
require the programmer to explicitly mark objects that should be destroyed
when leaving scope. Why?) How do you do RAII in Java?

Have you ever used Java and actually ran into an issue that requires RAII?

http://www.hackcraft.net/raii/

"Resource Acquisition Is Initialisation"

...

[begin excerpt]

Which Languages are Appropriate for RAII

Or rather, which languages is RAII appropriate for. As will soon be
explained RAII depends on the way that constructors and, in
particular, destructors, get called and the predictability of this
happening.

RAII can be used with:

* Languages which can have user-defined types allocated on the
stack (automatic objects in C/C++ terminology) and cleaned up
during normal stack cleanup (whether because of a function
returning, or an exception being thrown). E.g. C++.
* Languages which have reference-counted garbage collection
and hence predictable cleanup of an object for which there
is only one reference. E.g. VB6

RAII cannot generally be used with languages that clean up
objects using an unpredictable garbage collection, such as
Java (however see the Postscript on .NET). If a language
guarantees cleanup of all objects before an application
shuts down then it may be applicable to some problems.

[end excerpt]

I am not certain whether an Object variable going out of scope
in Java is subject to automatic cleanup or not, and if it is,
under what circumstances. Obviously it cannot be cleaned up
if the Object is placed into a Map or Collection. Otherwise,
I don't know.

In Java one can use a try{}catch{}finally{} pattern to
emulate RAII, but it has to be explicitly programmed.
One can also attempt overload of the finalize() method,
but the problem is that one has no idea exactly when
that will be called -- if at all. Some patterns (Swing)
implement a dispose() method as well, which Java does *not*
support directly.
 
T

The Ghost In The Machine

In comp.lang.java.advocacy, Noah Roberts
<[email protected]>
wrote
Well, that is one area where Java *can't* be used then isn't it.

Another can't. Where is the can?

JNI allows for Java to call down to native code. It tends
to be slow and little-used.

That removes the "can't", but replaces it with a "won't".
Other solutions are also possible, such as named pipes,
sockets, and pseudoterminals.
 
C

Chris Smith

Remon van Vliet said:
Have you ever used Java and actually ran into an issue that requires
RAII?

Of course no one has. There are no issues that require RAII. That's
irrelevant to the question of whether it is useful. Certainly,
try/finally gets cumbersome sometimes.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
C

Chris Smith

Noah Roberts said:
Only one incapable of learning very simple techniques to make it a
non-issue.

http://www.hackcraft.net/raii/

There are two questions being considered simultaneously here. One is
what is required to produce useful software in a language. The other
matter is what is required to understand that language. I side with the
position that a language that's hard to understand has a weakness in
this even if it remains possible to write software using that language.
Exception handling and memory management IS tricky in C++. So is the
relationship between function overloading, implicit type conversions,
and templates.

RAII is a different matter. It's a nice feature to have; but it doesn't
make the language any easier to understand.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
N

Noah Roberts

Chris said:
There are two questions being considered simultaneously here. One is
what is required to produce useful software in a language. The other
matter is what is required to understand that language. I side with the
position that a language that's hard to understand has a weakness in
this even if it remains possible to write software using that language.
Exception handling and memory management IS tricky in C++. So is the
relationship between function overloading, implicit type conversions,
and templates.

Lots of silly statements but no backing. "X is hard." Meaningless.
RAII is a different matter. It's a nice feature to have; but it doesn't
make the language any easier to understand.

Actually, RAII is at the HEART of this matter as it is a commonly used
technique to deal with the issue put forth. It is a very simple
concept and it works not only for memory management but also any other
resource that needs to be aquired and released.
 
?

=?ISO-8859-1?Q?Martin_Vejn=E1r?=

Remon said:
Have you ever used Java
No

and actually ran into an issue that requires RAII?

Require is a strong word. You can always do without RAII. You just have
a greater chance of losing precious time trying to figure out where
exactly have you forgotten to release that mutex...

Garbage collector may be good when it comes to releasing memory, but
memory is not the only resource you use. How about files, network
streams, database connections, synchronization primitives, etc?

A reply from "The Ghost In The Machine" has mentioned try..finally.
Whenever you use that construct to safely dispose of a resource, you're
better off using RAII.

Btw, I'm not really sure, but is the finally block executed, when you do
'return' from inside the try block?
 
W

Walter Bright

Remon said:
What point am i missing if i mention the "finally" block in Java?

'finally' works fine for one level of undo. It doesn't work so well (and
neither does RAII) if there are multiple operations that must all
succeed or none have happened. For example, if a transaction consists of
transactions A, B, and C, and A and B succeed but C fails, one must
unwind B and C. The article goes into more depth with this including
examples.

-Walter Bright
www.digitalmars.com C, C++, D programming language compilers
 
W

Walter Bright

Noah said:
Also, you could read that article above which shows some shortcommings
of both RAII and finally. However, the "scope guard" appears to be a D
language particular construct so is rather moot in this discussion.

There's a link at the end of the article with Andrei Alexandrescu's
technique for doing a limited form of scope guard in C++.

http://www.digitalmars.com/d/exception-safe.html

-Walter Bright
www.digitalmars.com C, C++, D programming language compilers
 
B

Bent C Dalager

We're all aware Javascript is completely unrelated to Java right?

Well, "completely unrelated" may be going a bit too far (it's more
like the wayward half brother that comes around to borrow money from
you every now and then, who you don't like to talk about in polite
company) but it is definately a different language.

Cheers
Bent D
 
B

Bent C Dalager

I've programmed both in JAVA and C++

Try writing a OS in JAVA...wait is that even possible?

This is easily possible (and reasonably common) on smart card chips,
many of which support Java bytecode natively.

Java-based CPUs for desktops have been tried I think, but it didn't
take off.

Cheers
Bent D
 
W

Walter Bright

Bent said:
(Personally, I found ECMAScript pretty straight forward up until I
reached the chapter of the spec titled "automatic semicolon
insertion". It went downhill from there <g>)

Automatic semicolon insertion is one of those ideas that perennially
keeps popping back up. The trouble is, it sounds good, and it is only
after experience with it that one eventually realizes what a bad idea it
is. And then a newbie comes along, and goes through the same process
again <g>.

-Walter Bright
www.digitalmars.com C, C++, D programming language compilers
 
B

Bent C Dalager

Garbage collector may be good when it comes to releasing memory, but
memory is not the only resource you use. How about files, network
streams, database connections, synchronization primitives, etc?

One classic leak is putting things into event listening lists in Swing
and forgetting to remove them at the appropriate time.
A reply from "The Ghost In The Machine" has mentioned try..finally.
Whenever you use that construct to safely dispose of a resource, you're
better off using RAII.

Btw, I'm not really sure, but is the finally block executed, when you do
'return' from inside the try block?

The finally block will _always_ be entered before the method returns.
Whether the "finally" runs to completion depends on whether an
unhandled exception gets thrown in the finally-block itself. If that
happens, then the remainder of the finally block is skipped.

Cheers
Bent D
 
N

Noah Roberts

Walter said:
There's a link at the end of the article with Andrei Alexandrescu's
technique for doing a limited form of scope guard in C++.

Well, that is on my list of things to read now; I am always up for
learning new techniques...whether I use them or not is a different
matter.

At any rate, exception safety is not a C++ only problem. Any language
that uses exceptions has exception safety issues and so far I haven't
seen one that did any better than another in this regard.
 
B

Bent C Dalager

Automatic semicolon insertion is one of those ideas that perennially
keeps popping back up.

I do admit to many times having run a C, C++ or Java compiler, had it
stop with a "semicolon expected" type error and cursing inwardly at it
thinking "well why don't you just _insert_ a bloody semicolon you
stupid compiler pos since you obviously realise that one is missing
right there".

In my world, however, there is a long way to go from just projecting
my own shortcomings onto the compiler and to actually implementing
complicated heuristics for trying to guess when the programmer did or
did not want a semicolon in the source code :)
The trouble is, it sounds good, and it is only
after experience with it that one eventually realizes what a bad idea it
is. And then a newbie comes along, and goes through the same process
again <g>.

Yes, but, I used to think that this was a self-solving problem.
Newbies don't write programming languages, after all, and by the time
they have the skill required to do so, one would have thought they
were long past the "let the language just guess what I mean" stage
.. . .

Obviously not though :)

Cheers
Bent D
 
W

Walter Bright

Alf said:
* Walter Bright:

I think you should get the attributions right (sorry, how could I put
this in a more gentle way?): ScopeGuard was Petri Marginean's invention,
and he co-authored the CUJ article with Andrei.

You're right. I apologize for the error.
 
O

Oliver Wong

peter koch said:
I believe most large system out there in the real world relies on C or
C++. Examples are numerous, but I could mention banking,
telecommunication and database management systems. I doubt you will
find any large product of this type in Java. There will be lots of Java
in the "supporting" infrastructure of course - most likely my
homebanking solution is Java. But all the "real" and "heavy" stuff is
most likely C/C++.

You're wrong. I don't know about telecommunication and DBMS, but for
banking, insurance and other "large systems" in the "real world", it's
mostly COBOL, and the businesses are looking to migrate their code to Java
or .NET. The reason I know this is that I work for a porting and migration
company (Castor Technologies) and we often get contracts from banks,
insurance companies, educational institues and the governments asking to
migrate their COBOL stuff to the above two platforms.

For what it's worth, usually for educational institutes they want to
switch to .NET, and everyone else wants to switch to Java.

- Oliver
 
O

Oliver Wong

Noah Roberts said:
Well, that is one area where Java *can't* be used then isn't it.

Another can't. Where is the can?

I think it's a bit silly to complain that you *can't* write platform
dependent code in Java.

If you goal is to intentionally write platform dependent code, maybe
Java isn't for you.

- Oliver
 
A

Andrew McDonagh

Remon said:
I normally dont get involved with pissing contests, but there's only so much
bs in a single post i can take without replying...

I like new/delete. Makes me feel I'm in charge. Just my .02$

So, your entire reasoning behind preferring manual memory managment over
garbage collection is that "you feel in charge"? You should give assembly
language a go. Meanwhile, in the real world most recent garbage collectors
outperform manual memory managment in the vast majority of applications, and
as a bonus you get the complete lack of memory leaks and such.

I'll grant you that it's a matter of taste, but no self respecting developer
will consider standards a bad thing. If you do, draw your conclusions.
no, they _have to be_ the same. Otherwise the compiler pukes.

And the ability to stick tons of classes in a single file with a non-related
name would be a good thing because....? Again, standards -> good
What about freedom of choice?

Can you think of a single instance where having an illogical directory
structure is preferred over a logical one?
f) You don't have to choose between char *, string, CString ... - String
is better (or same) than either of them and it is only choice.
Yeah, and a lot slower in some cases. User std::string where you need
dynamic strings, use char[] where you need static strings. You don't have
to - but you _can_!

When was the last time you benchmarked Java strings vs. C++?
Just a question of style. I use the built-in tpyes for everything.

It's freedom that doesnt add anything but confusion and hurts readability.
?? I don't understand that. You can't define operators in Java, can you?
Defining operators is one of the most important things for OOP IMHO.
He's not claiming you can, he simply says the exact same functionality can
be achieved albeit more verbose (i.e. .add rather than +). There are
certainly instances where operator overloading provides more readable code,
but at the same time it can also be the cause of rather unpredictable code.
On this point my stance is that if used with care operator overloading is a
pretty neat thing.

Unfortunately I once saw one of the senior developers/team leaders
actually overload the comma operator.

That cause no end of problems.

Its a powerful tool granted, so is an electric screw driver - but most
times a simpler approach is better.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,280
Latest member
BGBBrock56

Latest Threads

Top